Results 1 to 5 of 5
- 06-07-2011, 01:44 AM #1
Member
- Join Date
- Jun 2011
- Posts
- 9
- Rep Power
- 0
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.
That is the code, now what I am currently trying to do is instead of adding JCheckBoxes as so: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; } }I was hoping to do something like this:Java Code:new CheckListItem("1")
And set values later by doing 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);
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.Java Code:for (int i = 0; i < arrayAmount; i ++) { jcheckboxArray[i] = new JCheckBox(textArray[i]); }
- 06-07-2011, 01:48 AM #2
Member
- Join Date
- May 2011
- Location
- Maryland
- Posts
- 38
- Rep Power
- 0
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?
- 06-07-2011, 01:57 AM #3
Member
- Join Date
- Jun 2011
- Posts
- 9
- Rep Power
- 0
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:
I have a problem with these two lines in the CheckListRenderer class:Java Code:JCheckBox jcheckboxArray[] = new JCheckBox[arrayAmount]; JList list = new JList(jcheckboxArray)
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'Java Code:setSelected(((CheckListItem)value).isSelected()); setText(value.toString());
Mainly due to the 2 lines commented out. I think those lines need to be edited to something, but can't think of what
- 06-07-2011, 05:41 AM #4
Declaring an array doesn't initialize its members, you need to assign JCheckBoxes to each element of the array.
db
- 06-07-2011, 02:45 PM #5
Member
- Join Date
- Jun 2011
- Posts
- 9
- Rep Power
- 0
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
-
Requesting a review of my LoginBox!
By aadem in forum New To JavaReplies: 4Last Post: 03-16-2011, 09:01 PM -
Link one JList to another JList
By mib1bee in forum AWT / SwingReplies: 1Last Post: 12-31-2010, 07:10 PM -
Help getting my program to continue requesting info.
By antdilla22 in forum New To JavaReplies: 4Last Post: 09-13-2009, 10:44 PM -
Requesting some tips to implement an interface between a tree structure and table
By Karanam in forum AWT / SwingReplies: 1Last Post: 10-20-2008, 12:58 PM -
Requesting help in copy paste of folder similar to windows.
By selvin_raj in forum Advanced JavaReplies: 1Last Post: 06-23-2008, 06:46 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks