Results 1 to 5 of 5
  1. #1
    benjamin.b is offline Member
    Join Date
    Jun 2011
    Posts
    9
    Rep Power
    0

    Default Requesting help on tweeking a JCheckBox JList

    Hi, I have a problem with a JCheckBox JList I found on the web. I don't normally ask for help since I found that any question I seem to have usually gets solved by some intense Google searching. but here is my dilemma:

    I was searching for a JCheckBox JList that I could implement into my script, I found one ont he web that someone had produced. It works good I just need something tweeked on it.
    Java Code:
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    
    public class CheckBoxInList
    {
       public static void main(String args[])
       {
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JList list = new JList(new CheckListItem[] {new CheckListItem("1"), new CheckListItem("2"), new CheckListItem("3"), new CheckListItem("4"), new CheckListItem("5")});
    
            list.setCellRenderer(new CheckListRenderer());
            list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            list.addMouseListener(new MouseAdapter()
            {
                public void mouseClicked(MouseEvent event)
                {
                    JList list = (JList) event.getSource();
                    int index = list.locationToIndex(event.getPoint());
                    CheckListItem item = (CheckListItem)
                    list.getModel().getElementAt(index);
                    item.setSelected(! item.isSelected());
                    list.repaint(list.getCellBounds(index, index));
                }
            });
    
            frame.getContentPane().add(new JScrollPane(list));
            frame.pack();
            frame.setVisible(true);
            }
        }
    
        class CheckListItem
        {
            private String  label;
            private boolean isSelected = false;
    
            public CheckListItem(String label)
            {
                this.label = label;
            }
    
            public boolean isSelected()
            {
                return isSelected;
            }
    
            public void setSelected(boolean isSelected)
            {
                this.isSelected = isSelected;
            }
    
            public String toString()
            {
                return label;
            }
        }
    
        class CheckListRenderer extends JCheckBox  implements ListCellRenderer
        {
            public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean hasFocus)
            {
                setEnabled(list.isEnabled());
                setSelected(((CheckListItem)value).isSelected());
                setFont(list.getFont());
                setBackground(list.getBackground());
                setForeground(list.getForeground());
                setText(value.toString());
                return this;
            }
        }
    That is the code, now what I am currently trying to do is instead of adding JCheckBoxes as so:
    Java Code:
    new CheckListItem("1")
    I was hoping to do something like this:

    Java Code:
    int arrayAmount = 20;
    String textArray[] = {"Text", "More Text", "Even More Text", "Etc."};
    JCheckBox jcheckboxArray[] = new JCheckBox[arrayAmount];
    JList list = new JList(jcheckboxArray);
    And set values later by doing this:

    Java Code:
    for (int i = 0; i < arrayAmount; i ++)
            {
                jcheckboxArray[i] = new JCheckBox(textArray[i]);
            }
    Its just that my textArray[i] is forever changing in size and wording. Any help is appreciated since this portion is out of my league. But I would appreciate an explanation if not asking to much so I could further learn from this and possibly Render Cells efficiently myself someday.

  2. #2
    Join Date
    May 2011
    Location
    Maryland
    Posts
    38
    Rep Power
    0

    Default

    maybe i overlooked something, but i'm not sure if i really see what your problem is here? can you tell me more specifically what's not working or give me an example of something that the program didn't do correctly?

  3. #3
    benjamin.b is offline Member
    Join Date
    Jun 2011
    Posts
    9
    Rep Power
    0

    Default

    Well Whomever programmed that JCheckBox JList programmed it in a way in which the user had to Add items to a JList that would call another class "CheckListItem" and it would work off its constructors according. When implementing my idea:
    Java Code:
    JCheckBox jcheckboxArray[] = new JCheckBox[arrayAmount];
    JList list = new JList(jcheckboxArray)
    I have a problem with these two lines in the CheckListRenderer class:
    Java Code:
    setSelected(((CheckListItem)value).isSelected());
    setText(value.toString());
    By commenting those two lines out I can see a list of checkboxes, with no text and if I were to click on a box It produces a 'Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException'
    Mainly due to the 2 lines commented out. I think those lines need to be edited to something, but can't think of what

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default

    Declaring an array doesn't initialize its members, you need to assign JCheckBoxes to each element of the array.

    db

  5. #5
    benjamin.b is offline Member
    Join Date
    Jun 2011
    Posts
    9
    Rep Power
    0

    Default

    I have all of these initialized upon loading the script(Reads it from a file, not this particular script this is just an example of the main problem). But i did try via initializing them as well, the problem is there is no text that shows up which should be assigned to the script, and clicking on the Check Box creates a nullpointerexception error
    Last edited by benjamin.b; 06-07-2011 at 03:52 PM.

Similar Threads

  1. Requesting a review of my LoginBox!
    By aadem in forum New To Java
    Replies: 4
    Last Post: 03-16-2011, 09:01 PM
  2. Link one JList to another JList
    By mib1bee in forum AWT / Swing
    Replies: 1
    Last Post: 12-31-2010, 07:10 PM
  3. Help getting my program to continue requesting info.
    By antdilla22 in forum New To Java
    Replies: 4
    Last Post: 09-13-2009, 10:44 PM
  4. Replies: 1
    Last Post: 10-20-2008, 12:58 PM
  5. Replies: 1
    Last Post: 06-23-2008, 06:46 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
  •