Putting JComboBox into JTable

Extend JTable with this code: @Override public TableCellEditor getCellEditor(int row, int column) { Object value = super.getValueAt(row, column); if(value != null) { if(value instanceof JComboBox) { return new DefaultCellEditor((JComboBox)value); } return getDefaultEditor(value.getClass()); } return super.getCellEditor(row, column); } This will create a unique JComboBox cell editor for each combo box you get the a value for.

JComboBox setting label and value

You can put any object inside of a JComboBox. By default, it uses the toString method of the object to display a label navigate in the combo box using the keyboard. So, the best way is probably to define and use appropriate objects inside the combo : public class ComboItem { private String value; private … Read more

how to add different JComboBox items in a Column of a JTable in Swing

example on java2s.com looks like as works and correctly, then for example (I harcoded JComboBoxes for quick example, and add/change for todays Swing) import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.UIManager.LookAndFeelInfo; import javax.swing.table.*; public class EachRowEditorExample extends JFrame { private static final long serialVersionUID = 1L; public EachRowEditorExample() { super(“EachRow Editor Example”); try { for … Read more

Binding comboboxes in swing

I have problems starting my application it loads the list of countries but not the other lists It seems like you have to specifically set the selected index to invoke the listener. jComboBoxCountries.setModel(…) jComboBoxCountries.setSelectedIndex(0); And by selecting a country is charged the list of states but not the list of cities. I would guess this … Read more

Dynamically adding items to a JComboBox

How about using ComboBoxModel? Something like this…. JFrame frame = new JFrame(“Combo Box Demo”); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(200, 200); frame.setLayout(new FlowLayout()); Vector comboBoxItems=new Vector(); comboBoxItems.add(“A”); comboBoxItems.add(“B”); comboBoxItems.add(“C”); comboBoxItems.add(“D”); comboBoxItems.add(“E”); final DefaultComboBoxModel model = new DefaultComboBoxModel(comboBoxItems); JComboBox comboBox = new JComboBox(model); frame.add(comboBox); JButton button = new JButton(“Add new element in combo box”); frame.add(button); button.addActionListener(new ActionListener() { @Override public … Read more

Filling combobox from database by using hibernate in Java

I don’t use Hibernate, but given a JPA entity named Customer and a JPA controller named CustomerJpaController, you can do something like this. Update: Code updated to reflect a switch to EclipseLink (JPA 2.1) as the persistence library. import java.awt.EventQueue; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.List; import javax.swing.JComboBox; import javax.swing.JFrame; /** * @see http://stackoverflow.com/a/2531942/230513 */ … Read more