1

This type of post has been dealt with before, but I am having issues based on how my code is structured.

I am just simply trying to add a JComboBox to all rows in my last column. The code is below.

//Return Person objects from a method
ArrayList<Person> people = getPersonList();

String[] columnNames {"Name", "Age", "English Speaker?" };

DefaultTableModel model = new DefaultTableModel();
model.setColumnIdentifiers(columnNames);

JTable table = new JTable(model);

//Create JComboBox for last column (English Speaker?)                       
JComboBox<Integer> englishCombo = new JComboBox<>();

int count = 1;

//For loop to add each Person to there rows
//Also add a boolean value to determine check box
for(Person p: people)
{
    boolean english =false;

    if(p.isEnglishSpeaker() == true)
    {
        english = true;
    }
    else
    {
        english = false;
    }
    questionCombo.addItem(count);

    model.addRow(new Object[]{p.getName(), p.getAge(), english);
}

//Get 3rd column (English Speaker)
TableColumn englishColumn = table.getColumnModel().getColumn(2);
//Add JComboBox to English Speaker
englishColumn.setCellEditor(new DefaultCellEditor(englishCombo));

When I run this code, it only displays true of false in the 3rd column, not the JcomboBox? Could anyone identify the problem? Thanks much appreciated

Douglas Grealis
  • 599
  • 2
  • 11
  • 25
  • `boolean english =false; if(p.isEnglishSpeaker() == true) { english = true; } else { english = false; }` can also be written: `boolean english = p.isEnglishSpeaker();`. Or even simpler, drop everything written before and simply call: `model.addRow(new Object[]{p.getName(), p.getAge(), p.isEnglishSpeaker());` – Guillaume Polet May 05 '13 at 10:27
  • Check out this [answer](http://stackoverflow.com/questions/11226926/java-jtable-with-jcombobox/11227034#11227034). – Guillaume Polet May 05 '13 at 10:35
  • Thanks for the reply. I just changed to that easier way (dont know how I missed that). But it still returns only true of false, not the actual JComboBox? Thanks – Douglas Grealis May 05 '13 at 10:36
  • 1
    For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson May 05 '13 at 10:57
  • see this [answer](http://stackoverflow.com/questions/13699467/add-different-combobox-for-each-row-of-a-column-in-a-jtable/13703143#13703143) – Amarnath May 05 '13 at 14:07
  • Read the JTable API. Follow the link to the Swing tutorial on `How to Use Tables`, where you will find a working example. Start with the tutorial. – camickr May 05 '13 at 21:41

1 Answers1

2

You've specified a custom editor; now you need to address the renderer. I see two possibilities:

  1. Use JComboBox<String> with the desired true and false values, as shown here.

    image1

  2. Use the default renderer and editor, JCheckBox, for values whose type is Boolean.class, as shown here and here.

image2

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Thanks alot for you answer. It was my mistake. I meant I needed a JCheckBox, not a JComboBox. But its the exact same approach when adding a JCheckBox, right? – Douglas Grealis May 06 '13 at 10:47
  • `JCheckBox` is even easier, as that's the default for `TableModel` values of type `Boolean.class`. The second example under point 2. above shown how to override `getColumnClass()`. – trashgod May 06 '13 at 11:18