Results 1 to 9 of 9
  1. #1
    darshak_patel is offline Member
    Join Date
    Feb 2009
    Posts
    2
    Rep Power
    0

    Wink JList A real confusion.

    Please have a look on the following code. Try to run it. Check the code and let me know do we have any other good way to achieve the same thing?


    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Graphics2D;
    
    import javax.swing.DefaultListCellRenderer;
    import javax.swing.DefaultListModel;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JList;
    import javax.swing.JScrollPane;
    import javax.swing.ListCellRenderer;
    import javax.swing.border.Border;
    import javax.swing.border.EmptyBorder;
    import javax.swing.border.LineBorder;
    import javax.swing.border.TitledBorder;
    
    public class SamplesList {
    	
    	static int previndex = -1;
    	static JList jlist2 = null;
      public static void main(String args[]) {
        JFrame frame = new JFrame("Samples");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        DefaultListModel model = new DefaultListModel();
        
        
        
        
        model.ensureCapacity(100);
        for (int i = 0; i < 100; i++) {
          model.addElement(Integer.toString(i));
        }
         jlist2 = new JList(model);
        
        
        jlist2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jList1MouseClicked(evt);
                //repaint();
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                jList1MouseExited(evt);
                //repaint();
            }
        });
        jlist2.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                jList1MouseMoved(evt);
                //repaint();
            }
        });
    
        JScrollPane scrollPane2 = new JScrollPane(jlist2);
        frame.add(scrollPane2, BorderLayout.CENTER);
    
        frame.setSize(300, 350);
        frame.setVisible(true);
    
        jlist2.ensureIndexIsVisible(50);
        
        jlist2.removeSelectionInterval(previndex,previndex);
      }
      
      private static void jList1MouseMoved(java.awt.event.MouseEvent evt) {
      	
        Object o = evt.getSource();
        javax.swing.JList oj=(javax.swing.JList)o;
        int indexis = oj.locationToIndex( evt.getPoint());
        System.out.println("Hello ::::::"+previndex+"::::"+indexis);
        if(previndex!=indexis){
            oj.setSelectedIndex(indexis);
            oj.setSelectionBackground(java.awt.Color.BLUE);
        }else{
        	oj.setSelectedIndex(previndex);
            oj.setSelectionBackground(java.awt.Color.GREEN);
        	
        }
    
    }
    
    private static void jList1MouseClicked(java.awt.event.MouseEvent evt) {
        
        Object o = evt.getSource();
        javax.swing.JList oj=(javax.swing.JList)o;
        int indexis = oj.locationToIndex( evt.getPoint());
        previndex = indexis;
        oj.setSelectedIndex(indexis);
        oj.setSelectionBackground(java.awt.Color.GREEN);
        oj.repaint();
        //oj.removeSelectionInterval(previndex,previndex);
        
        changeModel();
        
    }
    
    private static void jList1MouseExited(java.awt.event.MouseEvent evt) {
        // TODO add your handling code here:
        Object o = evt.getSource();
        javax.swing.JList oj=(javax.swing.JList)o;
        if(previndex!=-1){
        oj.setSelectedIndex(previndex);
        oj.setSelectionBackground(java.awt.Color.GREEN);
        }
    }//GEN-LAST:event_jList1MouseExited
    
    private static void changeModel(){
    	
    	
    	DefaultListModel model = new DefaultListModel();
        model.ensureCapacity(100);
        for (int i = 0; i < 100; i++) {
          
          if(i==previndex){
          	model.addElement("<html><body bgcolor=\"#00FF00\" width=\"300\" height=\"20\" "+Integer.toString(i)+"</body></html>");
          }else{
          	
          	model.addElement(Integer.toString(i));
          }
        }
    	jlist2.setModel(model);
    	
    }
    
    }
    Regards,
    Darshak.

  2. #2
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    you should add to listen javax.swing.event.ListSelectionListener

    if(e.getValueIsAdjusting() == false)
    {
    // code...
    }

  3. #3
    darshak_patel is offline Member
    Join Date
    Feb 2009
    Posts
    2
    Rep Power
    0

    Default

    Thanks for your suggestion mtyoung.

    Still open for more suggestions if any.


    Regards,
    Darshak.

  4. #4
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Unless your model is complicated, just put all the values in a Vector and pass the Vector the JList constructor.

    MT is right about the ListSelectionListener; I don't know all the rest of the code is supposed to do, but I'd set it aside (copy the source file and delete the excess code in the copy) and see if our two suggestions don't allow you do everything you need to do.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Still open for more suggestions if any.
    I suggest that you don't post the same darn question in a gazillion forums. That is pretty darn rude.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

  8. #8
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Fubarable- you showed a darn lot of restraint in your comment. Props!

    Eranga-See? We're all behaving ourselves pretty darn well...
    Last edited by Steve11235; 02-07-2009 at 12:07 AM. Reason: 1234567890

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. System.in.read() confusion
    By ankurdh in forum New To Java
    Replies: 5
    Last Post: 01-08-2009, 08:28 AM
  2. real-time validation
    By atomz4peace in forum Eclipse
    Replies: 0
    Last Post: 01-06-2009, 07:31 PM
  3. how get real-time output
    By tOpach in forum New To Java
    Replies: 5
    Last Post: 12-17-2008, 08:41 PM
  4. Need help real bad on 2D array please !
    By TrungTran in forum New To Java
    Replies: 2
    Last Post: 11-23-2008, 04:04 AM
  5. bracket confusion...?
    By gallimaufry in forum New To Java
    Replies: 4
    Last Post: 10-28-2008, 11:17 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •