multiple JComboBox listeners - java

I am new to JComboBox
I have 4 JComboBoxes: specialite, etudiant, annee, and semestre.
I need to get the selected item from the 4 of them each time I change the selected item and add the result to the ScrollPane (groupe des matieres ouvertes)

Please look into the below JComboBox basic example given by Java. Use getSelectedItem() of JComboBox in actionPerformed() method of each combo box whenever an item got selected in it.
public void actionPerformed(ActionEvent e) {
JComboBox cb = (JComboBox)e.getSource();
String petName = (String)cb.getSelectedItem();
updateScrollpane(petName); // Update your scrollpane
}
Do the same thing for all of your 4 dropdowns and update the scrollpane

Related

JComboBox<BeanObject> not displaying edited values

I have a Jtable, whose first column uses a JComboBox as editor. The combobox model contains data objects fetched from a sql database.
If I manually enter a value inside the combobox and then leave the editor, the entered value is lost. This doens't happen if the value is selected from the popup, or if the JComboBox's model is instantiated with simple Strings instead of bean objects.
Note that I need to add row dinamically, but this seems irrelevant since the issue appears both in the default row and in the added rows.
This is a working NetBeans sample project that reproduces my issue: https://drive.google.com/open?id=0B89FsS48-Yy4V09YRVozRzJGMkk
Here is the relevant code:
public NewJFrame() {
initComponents();
//bean objects used to populate the combobox:
Item item1 = new Item("one", 1);
Item item2 = new Item("two", 2);
Item item3 = new Item("three", 3);
JComboBox<Item> comboBox = new JComboBox<>(new Item[]{item1, item2, item3});
comboBox.setEditable(true);
DefaultCellEditor defaultCellEditor = new DefaultCellEditor(comboBox);
defaultCellEditor.setClickCountToStart(1);
jTable1.getColumnModel().getColumn(0).setCellEditor(defaultCellEditor);
}
private void addRowButtonActionPerformed(java.awt.event.ActionEvent evt) {
((DefaultTableModel) jTable1.getModel()).addRow(new Object[]{null, null});
}
Update: when the JComboBox in the table is populated via a model instead of the constructor like this,
items = new Item[]{item1, item2, item3};
JComboBox<Item> comboBox = new JComboBox<>();
comboBox.setModel(new DefaultComboBoxModel<>(items));
a manually inserted value is kept displayed, only if no selection has been made before; that is, if I first select a choice and then edit the choice, when leaving the combobox the previously selected item reappears.
None of the reported behaviours occur in a JComboBox outside the table, so this led me to think it's something related to the CellEditor.
Update 2: here's a bug report of this issue from the year 2000! They said they solved it back then but this is far from solved after 15 years.

How to recreate element (SWING)?

In my form I've created JSpinner and JComboBox elements. Depending on the changing of JComboBox I have to use the different Spinner models. So in ComboBox listener I write spinner = new JSpinner(newModel), but it change nothing on form.
How to recreate element to see the difference?
// Create default Spinner
count = new JSpinner();
// Trying to replace spinner
product.addActionListener(e -> {
JComboBox source = (JComboBox) e.getSource();
String selectedItem = (String) source.getSelectedItem();
...
SpinnerNumberModel numberModel = getNewNumberModel(...)
count = new JSpinner(numberModel);
count.setModel(numberModel);
// repaint(); revalidate() - also don't working
});
You should not reallocate the spinner every time. Just replace its model. You are allocating a new instance of JSpinner in you action listener and change its model. But this new instance is not part of your panel and not visible. Remove count = new JSpinner(numberModel); from the action listener. And change the model of the existing spinner.

Is there another way to remove all items of a JComboBox then removeAllItems()?

Is there another way to remove all items of a JComboBox then removeAllItems()? I use 2 JComboBoxes in mij app and when you select an item from the first combobox the related items should then be shown in the second combobox. When I do this, the items just keep appending after the ones that were already there. When I then first try to clear the combobox by using removeAllItems(), the second combobox is empty and stays empty whenever I change the first combobox... The first combobox keeps all its values... Does anyone see my problem?
festival is the JComboBox:
private JComboBox festival;
private JComboBox zone;
...
public void fillFestivalList(){
festival.removeAllItems();
List festivals = OP.fillFestivalList();
for(Object fest: festivals)
festival.addItem(fest.toString());
}
public void fillZoneList(String festival){
zone.removeAllItems();
List zones = OP.fillZoneList(festival);
for(Object zoneItem: zones)
zone.addItem(zoneItem.toString());
}
Regarding,
Is there another way to remove all items of a JComboBox then removeAllItems()?
Simply give the JComboBox a new model.
I would create a new DefaultComboBoxModel<T>, fill it with the newest entries, and then call setModel(...) on my JComboBox, passing in the new model when desired.
You can also Remove all the items in this way ,
but better to Give JCombobox a new DefaultComboBoxModel like the way #Hovercraft Full Of Eels said
int itemCount = combo.getItemCount();
for(int i=0;i<itemCount;i++){
combo.removeItemAt(0);
}

Sort a Java JTable by one column [duplicate]

How do i sort jtable column using radio button?
my jtable is defaultTableModel not vectors.
I have already achieve when user press on column header, it will sort, now i have to implement using radio button..
What would be the best way to achieve this?
To do a sort programmatically you add code like the following to your listener:
DefaultRowSorter sorter = ((DefaultRowSorter)table.getRowSorter());
ArrayList list = new ArrayList();
list.add( new RowSorter.SortKey(2, SortOrder.ASCENDING) );
sorter.setSortKeys(list);
sorter.sort();
Add an actionlistener to the radiobutton, sort and set the tableModel.
The Vector argument is an input to defaultTableModel.
final JTable table = new JTable();
JRadioButton button = new JRadioButton();
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//sort your data here
table.setModel(new DefaultTableModel(sortedDate));
table.repaint();// maybe revalidate too
}
});

Changing elements of a JComboBox according to the selection from another JComboBox

I have a small app that generates statistic charts from a MySQL DB via JPA. To select which DB components to include in the statistic I have installed 2 JComboBoxes. First JComboBox is populated with the elements of Category1, second JComboBox with elements from Category2, which is a subcategory of Category1. What i want to do is populate JComboBox2 only with the elements of Category2 that are linked to the selection in JComboBox1.
Example: Category1 is car brands, Category2 is models; I want JComboBox2 to show only the models for the selected brand, right now it shows every available model of every brand.
First, add a listener on the Combobox1 :
private void comboBox1ItemStateChanged(java.awt.event.ItemEvent evt) {
if (java.awt.event.ItemEvent.DESELECTED == evt.getStateChange()) {
String valueBeforeDeselection = evt.getItem().toString();
// Do something if needed
} else if (java.awt.event.ItemEvent.SELECTED == evt.getStateChange()) {
String valueAfterSelection = evt.getItem().toString();
// Set the values of the ComboBox2
}
}
In order to fill the ComboBox2, you should empty it first
comboBox2.removeAllItems();
comboBox2.addItem("Value 1");
comboBox2.addItem("Value 2");

Categories