DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 3 of 3

Thread: JList problem

  1. #1
    Markos Guest

    JList problem


    here is what i have but i get errors.
    I get the errors when i select an element from a list. Can you just help
    me to explain why i get this ? Just in words please , no code . Thanks again.

    list.addMouseListener(new MouseAdapter() {
    public void mouseClicked(MouseEvent e) {
    int index = list.locationToIndex(e.getPoint());
    Object value = list.getSelectedValue().toString();
    myList lst = new myList();
    list = (JList)lst.getListCellRendererComponent(list,value,index,true,true);

    }
    });



    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;


    class myList extends JLabel implements ListCellRenderer {

    public myList() {
    setOpaque(true);
    }


    public Component getListCellRendererComponent(
    JList list,
    Object value,
    int index,
    boolean isSelected,
    boolean cellHasFocus){


    if (isSelected) {
    setText(value.toString());
    setBackground(Color.white);
    setBorder(BorderFactory.createLineBorder(Color.green));
    }
    return this;
    }
    }


  2. #2
    Paul Clapham Guest

    Re: JList problem

    Looks like the part of your post where you told us what errors you get was
    lost. Can you post it again?

    Markos <MarkosPolo@aol.com> wrote in message
    news:39f6ffde$1@news.devx.com...
    >
    > here is what i have but i get errors.
    > I get the errors when i select an element from a list. Can you just help
    > me to explain why i get this ? Just in words please , no code . Thanks

    again.
    >
    > list.addMouseListener(new MouseAdapter()


    > public void mouseClicked(MouseEvent e)


    > int index = list.locationToIndex(e.getPoint());
    > Object value = list.getSelectedValue().toString();
    > myList lst = new myList();
    > list =

    (JList)lst.getListCellRendererComponent(list,value,index,true,true);
    >
    > }
    > });
    >
    >
    >
    > import java.awt.*;
    > import java.awt.event.*;
    > import javax.swing.*;
    >
    >
    > class myList extends JLabel implements ListCellRenderer


    >
    > public myList()


    > setOpaque(true);
    > }
    >
    >
    > public Component

    ListCellRendererComponent(
    > JList list,
    > Object value,
    > int index,
    > boolean isSelected,
    > boolean cellHasFocus){
    >
    >
    > if (isSelected) {
    > setText(value.toString());
    > setBackground(Color.white);
    > setBorder(BorderFactory.createLineBorder(Color.green));
    > }
    > return this;
    > }
    > }
    >





  3. #3
    markos Guest

    Re: JList problem


    the problem is solved now. I found a way to do it. It was very simmple. When
    label was redrawing it didn't know which one was selected or not. So now
    this works.


    list.addMouseListener(new MouseAdapter() {
    public void mouseClicked(MouseEvent e) {
    int index = list.locationToIndex(e.getPoint());
    Object value = list.getSelectedValue().toString();
    Rectangle rect = list.getCellBounds(index,index);
    myList lst = new myList(rect);
    list.setCellRenderer(
    (ListCellRenderer)lst.getListCellRendererComponent(list,
    value,index,true,true));
    }
    });

    class myList extends JLabel implements ListCellRenderer {

    Rectangle rect;

    public myList(Rectangle rect) {
    this.rect = rect;
    }

    public Component getListCellRendererComponent(
    JList list,
    Object value,
    int index,
    boolean isSelected,
    boolean cellHasFocus) {

    setText(value.toString());
    setBounds(rect);

    if (isSelected) {
    setBorder(BorderFactory.createLineBorder(Color.black));
    }
    else {
    setBorder(BorderFactory.createLineBorder(Color.white));
    }
    return this;
    }
    }



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links