Results 1 to 2 of 2
Thread: CheckBox in JTable
- 08-05-2009, 03:51 AM #1
Member
- Join Date
- Nov 2008
- Posts
- 20
- Rep Power
- 0
CheckBox in JTable
Hello guys, I have a jtable and I want to add check box in the first column so that I can give the user the option to select some of the rows he wants.
in the table I add this code to render the cell from the String value to boolean value but when I want to select some of the rows it displays back the string value.
the code is
can you help me in what to change so that I can do what I want. I am newbie in programming. I know that I have to create an array of checkboxes but don't know how to integrate them in this code. any help will be much appreciated!!Java Code:table.getColumnModel().getColumn(0).setCellRenderer(new TableCellRenderer() { // the method gives the component like whome the cell must be rendered public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,boolean isFocused, int row, int col) { boolean marked = (Boolean) value; JCheckBox rendererComponent = new JCheckBox(); if (marked) { rendererComponent.setSelected(true); } return rendererComponent; } });
regards,
nickLast edited by Fubarable; 08-05-2009 at 04:58 AM. Reason: code tags added to retain formatting
-
Don't set the cell renderer to do this but instead use a custom table model (I usually extend the DefaultTableModel) and in your getColumnClass override method return Boolean.class for the columns where you want a check box. For details on this and sample code, please see the Sun JTable tutorial: Sun Java JTable Tutorial
Oh, I added code tags to your posted code above to improve readability. If you'd like to do this in the future, just highlight your code and press the "CODE" button.
Best of luck and hope this helps.
Similar Threads
-
Jtree with checkbox
By msankar.ravi in forum AWT / SwingReplies: 1Last Post: 07-09-2009, 11:25 PM -
Checkbox to list
By msixty in forum New To JavaReplies: 5Last Post: 05-07-2009, 08:10 PM -
checkBox in Jtable renderining problem
By pothraj in forum AWT / SwingReplies: 1Last Post: 12-18-2008, 11:39 AM -
Jtable duplicates through Hashtable (JTable condition problem) my assignment plz help
By salmanpirzada1 in forum Advanced JavaReplies: 2Last Post: 05-15-2008, 10:15 AM -
checkbox
By Alan in forum AWT / SwingReplies: 3Last Post: 05-18-2007, 10:10 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks