JTable, JComboBox using custom objects

Hi, if you put a JComboBox in a JTable array and String [] in a JComboBox, everything works fine. Buf, if you put your own data type in a JComboBox, selecting values ​​in one column becomes complicated. Here is the official official example . Try changing the following part:

JComboBox comboBox = new JComboBox(); comboBox.addItem("Snowboarding"); comboBox.addItem("Rowing"); comboBox.addItem("Knitting"); comboBox.addItem("Speed reading"); comboBox.addItem("Pool"); comboBox.addItem("None of the above"); sportColumn.setCellEditor(new DefaultCellEditor(comboBox)); 

IN:

 JComboBox comboBox = new JComboBox(); comboBox.addItem(new Test("Snowboarding")); comboBox.addItem(new Test("Rowing")); comboBox.addItem(new Test("Knitting")); comboBox.addItem(new Test("Speed reading")); comboBox.addItem(new Test("Pool")); comboBox.addItem(new Test("None of the above")); sportColumn.setCellEditor(new DefaultCellEditor(comboBox)); 

And create a new data type:

 public class Test { private String name; public Test(String name) { this.name = name; } @Override public String toString() { return name; } } 

You will see that when you click on a table cell in witch there is a JComboBox with a custom data type. The cell value of the first column is selected automatically. How to fix this problem?

EDIT 1: I added SSCCE.

Main class:

 import java.awt.BorderLayout; public class windw extends JFrame { private JPanel contentPane; private JTable table; public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { windw frame = new windw(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } public windw() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 300); contentPane = new JPanel(); contentPane.setLayout(new BorderLayout(0, 0)); setContentPane(contentPane); table = new JTable(); String[] grupes2 = new String[3]; grupes2[0] = "first"; grupes2[1] = "second"; grupes2[2] = "third"; table.setModel(new DefaultTableModel( new Object[][] { {new JComboBox<String>(grupes2)}, {new JComboBox<String>(grupes2)}, {new JComboBox<String>(grupes2)}, {new JComboBox<String>(grupes2)}, {new JComboBox<String>(grupes2)}, {new JComboBox<String>(grupes2)}, {new JComboBox<String>(grupes2)}, }, new String[] { "Column name" } )); TableColumn sportColumn = table.getColumnModel().getColumn(0); sportColumn.setCellEditor(new DefaultCellEditor(new JComboBox<String>(grupes2))); sportColumn.setCellRenderer(new Renderer(grupes2)); contentPane.add(table, BorderLayout.CENTER); } } 

Renderer:

 import java.awt.Component; import javax.swing.JComboBox; import javax.swing.JTable; import javax.swing.table.TableCellRenderer; public class Renderer extends JComboBox implements TableCellRenderer { public Renderer(String[] items) { super(items); } public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { if (isSelected) { setForeground(table.getSelectionForeground()); super.setBackground(table.getSelectionBackground()); } else { setForeground(table.getForeground()); setBackground(table.getBackground()); } // Select the current value setSelectedItem(value); return this; } } 
+6
source share
1 answer

The problem is that your TableModel stores the String object, and the ComboBox contains the Test object. These objects are not equal, so there is no element to select, and it looks first, is automatically selected.

Change your code to the following and you will see the same problem with an unknown line:

 {"Joe", "Brown", "Pool?????", new Integer(10), new Boolean(false)} 

To fix this problem, I would suggest that you need to do the following:

 {"Joe", "Brown", new Test("Pool"), new Integer(10), new Boolean(false)} 

Then you will need to implement the equals () method in your Test class to compare the name property of both components. In addition, you will need to implement the hashcode () method.

In the future, as Andrew suggested, include your SSCCE with your question, since we don’t have time to copy / paste / edit and test the code, because we never know if we are doing it exactly like you.

+8
source

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


All Articles