Binding Comboboxes in Swing

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 is the same problem, once you reset the model of the states combobox you would need to select its index as well.

Another approach is to not select a default state or city, but instead prompt the user to select one. Here is some code that uses this approach:

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class ComboBoxTwo extends JFrame implements ActionListener
{
private JComboBox mainComboBox;
private JComboBox subComboBox;
private Hashtable subItems = new Hashtable();

public ComboBoxTwo()
{
String[] items = { "Select Item", "Color", "Shape", "Fruit" };
mainComboBox = new JComboBox( items );
mainComboBox.addActionListener( this );

// prevent action events from being fired when the up/down arrow keys are used
// mainComboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
getContentPane().add( mainComboBox, BorderLayout.WEST );

// Create sub combo box with multiple models

subComboBox = new JComboBox();
subComboBox.setPrototypeDisplayValue("XXXXXXXXXX"); // JDK1.4
getContentPane().add( subComboBox, BorderLayout.EAST );

String[] subItems1 = { "Select Color", "Red", "Blue", "Green" };
subItems.put(items[1], subItems1);

String[] subItems2 = { "Select Shape", "Circle", "Square", "Triangle" };
subItems.put(items[2], subItems2);

String[] subItems3 = { "Select Fruit", "Apple", "Orange", "Banana" };
subItems.put(items[3], subItems3);
mainComboBox.setSelectedIndex(1);
}

public void actionPerformed(ActionEvent e)
{
String item = (String)mainComboBox.getSelectedItem();
Object o = subItems.get( item );

if (o == null)
{
subComboBox.setModel( new DefaultComboBoxModel() );
}
else
{
subComboBox.setModel( new DefaultComboBoxModel( (String[])o ) );
}
}

public static void main(String[] args)
{
JFrame frame = new ComboBoxTwo();
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible( true );
}
}

Binding JComboBox to a JTable

You load your data to Jtable only once at constructing of that, and then you never do that, because of your Jtable doesn't update while you select another item in JComboBox.

Instead of using ActionListener on your phoneBrandCombo use ItemListener like next:

 phoneBrandCombo.addItemListener(new ItemListener() {

@Override
public void itemStateChanged(ItemEvent arg0) {
if(arg0.getStateChange() == ItemEvent.SELECTED){
Object object = ((JComboBox)arg0.getSource()).getSelectedItem();
//loadDataFromDBToTable(object);
}
}
});

here loadDataFromDBToTable(object); is method for loading new data to your JTable for selected object.

Read about using JTable and JComboBox.

Bind two JComboBox filters

Use RowFilter.andFilter() to allow multiple filter be applied to a single JTable with an AND logic (only if both filters are true, the item will show up) (there is also an OR, NOT,...).

Haven't tested, but I guess something like this could work:

// Collection of filters to be applied to your table
List<RowFilter<DefaultTableModel, Object>> filters = new ArrayList<>();

comboBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
if(filters.isEmpty())
filters.add(RowFilter.regexFilter(comboBox.getSelectedItem().toString(), 2));
else
filters.set(0, RowFilter.regexFilter(comboBox.getSelectedItem().toString(), 2));
// Apply filters
sorter.setRowFilter(RowFilter.andFilter(filters));
}
});

comboBox_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
if(filters.size() < 2)
filters.add(RowFilter.regexFilter(comboBox_1.getSelectedItem().toString(), 3));
else
filters.set(1, RowFilter.regexFilter(comboBox_1.getSelectedItem().toString(), 3));
// Apply filters
sorter.setRowFilter(RowFilter.andFilter(filters));
}
});


Related Topics



Leave a reply



Submit