1

Hello :) I need help with changing a JComboBox in a JTable. I'm new to GUI-Programming and Swing and couldn't solve this problem: I have to change the behavior of a JComboBox.

You can see the ComboBox in the picture below. If "Ja" is selected there should just be "Nein" as an option and the other way around. It would also be cool if "Nein" is set per default. The code was written from one student from last semester and I have difficulties to adjust the combobox like I have to.

Table with the ComboBox

That's the code snippet where the ComboBox gets initialized.

optionsInteger = new JComboBox<String>();
    optionsInteger.addItem("Ja");
    optionsInteger.addItem("Nein");
    optionsInteger.setSelectedItem(optionsInteger.getItemAt(0));
    optionsInteger.setSelectedIndex(1);
    optionsInteger.setName("optionsInteger");

The ComboBox gets inserted to a JTable in this method:

    public void repaintXTable(DefaultTableModel model,JTable table, int xAmount, JScrollPane scrollPane,
                                JComboBox<String> optionsInteger) {

    model.setRowCount(xAmount); 
    th = table.getTableHeader();
    tcm = th.getColumnModel();
    tcs = tcm.getColumns();

    tcs.nextElement().setHeaderValue("");
    tcs.nextElement().setHeaderValue("Lower");
    tcs.nextElement().setHeaderValue("Upper");
    tc = tcs.nextElement();
    tc.setHeaderValue("Integer");

    tc.setCellEditor(new DefaultCellEditor(optionsInteger));

    for(int i=0;i<xAmount;i++)
    {
        model.setValueAt("X"+(i+1), i, 0);
    }

}

Thank you very much for your help.

RubioRic
  • 2,442
  • 4
  • 28
  • 35
  • please is there some issue to follows working code example from Oracle tutorial - How to use tables, part ComboBox as Editor – mKorbel May 09 '16 at 12:02
  • Are you sure you even want this? It is rather unusual behaviour. – Oebele May 09 '16 at 12:38
  • HI Oebele. Yeah I know but my professor wants it that way. Maybe it would be better to replace the ComboBox with a CheckBox. But the Checkbox has to return a String "Ja" if checked and "Nein" if unchecked. :/ –  May 09 '16 at 12:58
  • You can change text of a checkbox inside an `ItemListener`. http://www.java2s.com/Tutorial/Java/0240__Swing/CheckifaJCheckBoxisselectedinitsitemchangelistener.htm – rdonuk May 09 '16 at 13:18
  • Hi @rdonuk thanks for your answer. I would like to replace the comboBox with a textfield which changes its value if it gets clicked on from "Ja" to "Nein" and the other way around. How could I implement this on the code above? Im sorry to ask this way but I have to get finished till tomorrow. –  May 09 '16 at 13:26

2 Answers2

0

In your code, this line

optionsInteger.setSelectedItem(optionsInteger.getItemAt(0));

sets the default selection to the zeroth element (Ja). This line

optionsInteger.setSelectedIndex(1);

sets the default selection to the first element (Nein).

Either set the selected item or the selected index. There's no need to do both.

A JComboBox does not remove the selected element by default. If the selected element is removed, how would the selected element display in your JTable?

If you really want to do this, you'll have to create your own version of a JComboBox.

Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111
  • Hi. Thank you for your answer :). Maybe I can replace the ComboBox with a Textfield. And by clicking on the field it should change it's value. Is this possible? –  May 09 '16 at 13:03
  • @Tolgi: Yes, it's possible to use a JTextField to display the selected value. Basically, you're creating a JComboBox again. Just use a JComboBox and don't worry about the selected item showing in the list. The user expects to see the selected item in the list of the JComboBox. – Gilbert Le Blanc May 09 '16 at 15:09
  • `If the selected element is removed, how would the selected element display in your JTable? ` - The render knows nothign about the editor, it just gets the data from the model and displays it in the table so that is not an issue. – camickr May 09 '16 at 15:26
  • Usual convention for a "Ja/Nein" selection (or any selector with only two options) would be a CheckBox or RadioButtons, not a ComboBox. – FredK May 09 '16 at 15:34
  • @FredK Hi. Thanks for the answer. I tried to replace the ComboBox with a checbox but it doesn't get displayed on the table. –  May 09 '16 at 20:01
0

. If "Ja" is selected there should just be "Nein" as an option and the other way around.

So then you need two separate ComboBoxModels, one model containing "Nein" and the other containing "Ja". Then when you start to edit the cell you check the current value and use the model containing the other value.

Check out How to add unique JComboBoxes to a column in a JTable (Java). This example shows how you can dynamically change the model at the time the cell is about to be edited.

It would also be cool if "Nein" is set per default.

This has nothing to do with the editor. You just add "Nein" to the TableModel when you add the data to the model.

Community
  • 1
  • 1
camickr
  • 321,443
  • 19
  • 166
  • 288