Celleditor (JComboBox) in a specific JTable series

I do not know how to do this to install jcombobox on a specific line ... at the moment I have jcombobox for all lines, but I want it to be on only one line:

JComboBox cc = new JComboBox(); cc.addItem(jComboBox5.getSelectedItem()+"/"+jComboBox6.getSelectedItem()+"/"+jComboBox7.getSelectedItem()+" "+jComboBox1.getSelectedItem()+"."+jComboBox2.getSelectedItem()); jTable1.getColumnModel().getColumn(3).setCellEditor(new DefaultCellEditor(cc)); DefaultTableCellRenderer renderer = new DefaultTableCellRenderer(); renderer.setToolTipText("CLICCA PER LE DATE"); jTable1.getColumnModel().getColumn(3).setCellRenderer(renderer); 
0
source share
2 answers

Update:. When I tested my (possibly incomplete) answer, I came across a very good SO question, which I think will help a lot better than I could: Enabling JComboBox in JTable

Another update: I read your question again, and realized that you asked a specific line . The only way I can do this is to create a custom CellEditor, something like:

 private static class MyCellEditor extends AbstractCellEditor implements TableCellEditor { DefaultCellEditor other = new DefaultCellEditor(new JTextField()); DefaultCellEditor checkbox = new DefaultCellEditor(new JComboBox(new Object[] {"abc"})); private DefaultCellEditor lastSelected; @Override public Object getCellEditorValue() { return lastSelected.getCellEditorValue(); } @Override public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { if(row == 0) { lastSelected = checkbox; return checkbox.getTableCellEditorComponent(table, value, isSelected, row, column); } lastSelected = other; return other.getTableCellEditorComponent(table, value, isSelected, row, column); } } 

In this example, the custom CellEditor is actually two Editors, and depending on the selected row, the particular editor will receive a call (both figuratively and literally). I admit that lastSelected seemed a bit hokey, but I honestly couldn't find an easier way to find out what value the editor needs to return (since getCellEditorValue has no arguments).

To make your table look β€œright,” you probably have to do something with Renderer (because Renderer may or may not know to show the selected JComboBox value as the initial value). It depends on how you initialize the data in the actual table.


To complete my initial answer below:

You can add the JComboBox component to the line using addRow in TableModel as below: How to add a row in JTable?

See also: http://docs.oracle.com/javase/tutorial/uiswing/components/table.html

I think the main problem is that you are mixing the idea of ​​Column Editors / Renderers with the actual data that will be stored on each row.

+2
source

but I want it in only one line:

Override the getCellEditor (...) method to return a specific editor for a given line:

 import java.awt.*; import java.awt.event.*; import java.util.List; import java.util.ArrayList; import javax.swing.*; import javax.swing.border.*; import javax.swing.table.*; public class TableComboBoxByRow extends JFrame { List<TableCellEditor> editors = new ArrayList<TableCellEditor>(3); public TableComboBoxByRow() { // Create the editors to be used for each row String[] items1 = { "Red", "Blue", "Green" }; JComboBox comboBox1 = new JComboBox( items1 ); DefaultCellEditor dce1 = new DefaultCellEditor( comboBox1 ); editors.add( dce1 ); String[] items2 = { "Circle", "Square", "Triangle" }; JComboBox comboBox2 = new JComboBox( items2 ); DefaultCellEditor dce2 = new DefaultCellEditor( comboBox2 ); editors.add( dce2 ); String[] items3 = { "Apple", "Orange", "Banana" }; JComboBox comboBox3 = new JComboBox( items3 ); DefaultCellEditor dce3 = new DefaultCellEditor( comboBox3 ); editors.add( dce3 ); // Create the table with default data Object[][] data = { {"Color", "Red"}, {"Shape", "Square"}, {"Fruit", "Banana"}, {"Plain", "Text"} }; String[] columnNames = {"Type","Value"}; DefaultTableModel model = new DefaultTableModel(data, columnNames); JTable table = new JTable(model) { // Determine editor to be used by row public TableCellEditor getCellEditor(int row, int column) { int modelColumn = convertColumnIndexToModel( column ); if (modelColumn == 1 && row < 3) return editors.get(row); else return super.getCellEditor(row, column); } }; JScrollPane scrollPane = new JScrollPane( table ); getContentPane().add( scrollPane ); } public static void main(String[] args) { TableComboBoxByRow frame = new TableComboBoxByRow(); frame.setDefaultCloseOperation( EXIT_ON_CLOSE ); frame.pack(); frame.setVisible(true); } } 
+5
source

Source: https://habr.com/ru/post/1491870/


All Articles