Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Swing / AWT / SWT
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Tim Cooke
paul wheaton
Paul Clapham
Ron McLeod
Sheriffs:
Jeanne Boyarsky
Liutauras Vilda
Saloon Keepers:
Tim Holloway
Carey Brown
Roland Mueller
Piet Souris
Bartenders:
Forum:
Swing / AWT / SWT
JTable Columns
francis varkey
Ranch Hand
Posts: 169
posted 18 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Respected Sirs,
How to insert different JComboBoxes ,in different rows in a same column ?
regards francis
Craig Wood
Ranch Hand
Posts: 1535
posted 18 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.table.*; public class ComboColumn { private JScrollPane getContent() { int[][] values = { { 1, 19, 7 }, { 16, 4, 32, 12 }, { 6, 9, 4 } }; // wrap values for editor comboBoxes Object[][] options = new Object[values.length][]; for(int j = 0; j < values.length; j++) { options[j] = new Object[values[j].length]; for(int k = 0; k < values[j].length; k++) { options[j][k] = Integer.valueOf(values[j][k]); } } // wrap all items for renderer comboBox int itemCount = 0; for(int j = 0; j < values.length; j++) for(int k = 0; k < values[j].length; k++) itemCount++; Object[] allItems = new Object[itemCount]; for(int j = 0, count = 0; j < values.length; j++) for(int k = 0; k < values[j].length; k++) allItems[count++] = Integer.valueOf(values[j][k]); Object[][] rowData = { { "John", "12 ElmGrove", options[0][0] }, { "Sara", "97 BayShore", options[1][0] }, { "Kyle", "18 Stone Ave", options[2][0] } }; Object[] colNames = { "name", "address", "options" }; CustomModel model = new CustomModel(rowData, colNames); JTable table = new JTable(model); Dimension d = table.getPreferredSize(); d.width = 360; table.setPreferredScrollableViewportSize(d); table.setCellSelectionEnabled(true); TableColumn col = table.getColumnModel().getColumn(2); col.setCellRenderer(new ComboRenderer(allItems)); col.setCellEditor(new ComboEditor(options)); return new JScrollPane(table); } public static void main(String[] args) { JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setContentPane(new ComboColumn().getContent()); f.pack(); f.setLocation(200,200); f.setVisible(true); } } class CustomModel extends DefaultTableModel { CustomModel(Object[][] data, Object[] colNames) { super(data, colNames); } public void setSelection(Object value, int row) { setValueAt(value, row, 2); fireTableCellUpdated(row, 2); } } class ComboRenderer implements TableCellRenderer { JComboBox comboBox; ComboRenderer(Object[] items) { comboBox = new JComboBox(items); comboBox.setEditable(false); } public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { if(isSelected) { comboBox.setForeground(table.getSelectionForeground()); comboBox.setBackground(table.getSelectionBackground()); } else { comboBox.setForeground(table.getForeground()); comboBox.setBackground(table.getBackground()); } comboBox.setSelectedItem(value); return comboBox; } } class ComboEditor extends DefaultCellEditor { JComboBox[] combos; JTable table; ComboEditor(Object[][] itemArrays) { super(new JComboBox()); combos = new JComboBox[itemArrays.length]; for(int j = 0; j < combos.length; j++) { combos[j] = new JComboBox(itemArrays[j]); combos[j].setEditable(false); } } public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { if(this.table == null) this.table = table; JComboBox comboBox = combos[row]; if(isSelected) { comboBox.setForeground(table.getSelectionForeground()); comboBox.setBackground(table.getSelectionBackground()); } else { comboBox.setForeground(table.getForeground()); comboBox.setBackground(table.getBackground()); } comboBox.setSelectedItem(value); return comboBox; } public boolean stopCellEditing() { int row = table.getEditingRow(); Object value = combos[row].getSelectedItem(); fireEditingStopped(); ((CustomModel)table.getModel()).setSelection(value, row); return true; } }
With a little knowledge, a
cast iron skillet
is non-stick and lasts a lifetime.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
JTable
JTable
How to populate different values into JComboBox in JTable
JComboBOx in a Jtable
JTable with different objects (or components) in a column
More...