Results 1 to 2 of 2
- 11-23-2009, 04:31 AM #1
Member
- Join Date
- Oct 2009
- Posts
- 88
- Rep Power
- 0
Add diffrent components in jtable
how to add different component like jcombobox, jcheckbox, jformattedtextfield all components in single column in different rows
like
row column
Row1 JCombobox
Row2 JFormattedField
Row3 JCheckBox
....
.
.
.
plz give sample code.
so that i can understand.
plz dont post link of
below
sun java forum
i already observed i cant understand it.
- 11-23-2009, 05:03 AM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
You need to override the getCellRenderer() and getCellEditors() to return the appropriate renderer/editor for the data in the given cell. How you do this is up to you.
Here is a generic solution that may (or may not) help get you started:
Java Code:import java.awt.*; import java.util.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.table.*; public class TablePropertyEditor extends JFrame { public TablePropertyEditor() { String[] columnNames = {"Type", "Value"}; Object[][] data = { {"String", "I'm a string"}, {"Date", new Date()}, {"Integer", new Integer(123)}, {"Double", new Double(123.45)}, {"Boolean", Boolean.TRUE} }; JTable table = new JTable(data, columnNames) { private Class editingClass; public TableCellRenderer getCellRenderer(int row, int column) { editingClass = null; int modelColumn = convertColumnIndexToModel(column); if (modelColumn == 1) { Class rowClass = getModel().getValueAt(row, modelColumn).getClass(); return getDefaultRenderer( rowClass ); } else return super.getCellRenderer(row, column); } public TableCellEditor getCellEditor(int row, int column) { editingClass = null; int modelColumn = convertColumnIndexToModel(column); if (modelColumn == 1) { editingClass = getModel().getValueAt(row, modelColumn).getClass(); return getDefaultEditor( editingClass ); } else return super.getCellEditor(row, column); } // This method is also invoked by the editor when the value in the editor // component is saved in the TableModel. The class was saved when the // editor was invoked so the proper class can be created. public Class getColumnClass(int column) { return editingClass != null ? editingClass : super.getColumnClass(column); } }; table.setPreferredScrollableViewportSize(table.getPreferredSize()); JScrollPane scrollPane = new JScrollPane( table ); getContentPane().add( scrollPane ); } public static void main(String[] args) { TablePropertyEditor frame = new TablePropertyEditor(); frame.setDefaultCloseOperation( EXIT_ON_CLOSE ); frame.pack(); frame.setLocationRelativeTo( null ); frame.setVisible(true); } }
Similar Threads
-
The code isnt working unless I add print statements at diffrent points!:confused:
By Addez in forum New To JavaReplies: 6Last Post: 11-12-2009, 10:50 AM -
log4j - multiple logs with diffrent contents
By ld09 in forum Java AppletsReplies: 0Last Post: 10-08-2009, 11:36 AM -
diffrent page redirection in struts.
By rakesh_n_mehta in forum Web FrameworksReplies: 0Last Post: 12-11-2008, 06:34 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 -
Gui Components
By Marty in forum New To JavaReplies: 1Last Post: 05-28-2007, 04:04 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks