-1

Im having trouble getting my JTable that im using to display either check boxes or combo boxes in my applet.

Here is the code that is not working correctly

 String[] options = {"download", "ignore"};
 Object[] obj = {new JComboBox(options), ((MetadataList)array.get(1)).getMetadata("Filename").getValue()};

 defaultTableModel2.addRow(obj);

The defaultTableModel2 is simply a DefaultTableModel defaultTabelModel2 = new DefaultTableModel() so nothing too dramatic there. The code above is using a JComboBox, but I have also tried using a JCheckBox as well and the same issues were present.

javax.swing.JComboBox[,0,0,0x0,invalid,layout=javax.swing.plaf.basic.BasicComboBoxUI$Handler,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.synth.SynthBorder@8380df,flags=320,maximumSize=,minimumSize=,preferredSize=,isEditable=false,lightWeightPopupEnabled=true,maximumRowCount=8,selectedItemReminder=download] 

is what appears instead of a JComboBox

Any help would be greatly appreciated!

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Matthew Pigram
  • 1,400
  • 3
  • 25
  • 65
  • You don't add components to the table model - you need to use a custom cell renderer that *looks like* a check box. – Greg Kopff Jun 29 '12 at 03:05
  • That code is way off base for how JTables work. There's a big separation between data held by a JTable and its rendering, and you will want to read the tutorial section on JTable cell rendering and cell editing. You can't guess at this stuff and expect it to work, and that's what the tutorials are for. For a check box, all you need to do is have a column of Booleans and the `getColumnClass()` should return `Boolean.class`. For combo boxes, you'll have to read more into rendering and editing to get it to work. – Hovercraft Full Of Eels Jun 29 '12 at 03:06
  • 2
    Here's the [JTable Tutorial link](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html). Good luck. – Hovercraft Full Of Eels Jun 29 '12 at 03:11
  • Edit: and using a combobox as an editor is actually even easier than I thought. Again the table tutorial explains all: just call `setCellEditor(new DefaultCellEditor(comboBox));` on the Column object from the ColumnModel, passing in a filled JComboBox. Please read it and then come back with specific questions if you are confused. Consider closing this question though. – Hovercraft Full Of Eels Jun 29 '12 at 03:35

1 Answers1

3

For example:

import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;

@SuppressWarnings("serial")
public class TableJunk extends JPanel {
   enum Day {MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY}
   MyTableModel tableModel = new MyTableModel();
   private JTable myTable = new JTable(tableModel);

   public TableJunk() {
      setLayout(new BorderLayout());
      add(new JScrollPane(myTable));

      Object[] rowData = {Day.MONDAY, Boolean.TRUE};
      tableModel.addRow(rowData );
      rowData = new Object[]{Day.TUESDAY, Boolean.TRUE};
      tableModel.addRow(rowData );
      rowData = new Object[]{Day.WEDNESDAY, Boolean.TRUE};
      tableModel.addRow(rowData );
      rowData = new Object[]{Day.THURSDAY, Boolean.TRUE};
      tableModel.addRow(rowData );
      rowData = new Object[]{Day.FRIDAY, Boolean.TRUE};
      tableModel.addRow(rowData );
      rowData = new Object[]{Day.SATURDAY, Boolean.FALSE};
      tableModel.addRow(rowData );
      rowData = new Object[]{Day.SUNDAY, Boolean.FALSE};
      tableModel.addRow(rowData );

      // create the combo box for the column editor          
      JComboBox<Day> comboBox = new JComboBox<TableJunk.Day>(Day.values());
      myTable.getColumnModel().getColumn(0).setCellEditor(new DefaultCellEditor(comboBox));
   }

   private static void createAndShowGui() {
      TableJunk mainPanel = new TableJunk();

      JFrame frame = new JFrame("TableJunk");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }


   private static class MyTableModel extends DefaultTableModel {
      private static final String[] COLUMN_NAMES = {"Day", "Work Day"};

      public MyTableModel() {
         super(COLUMN_NAMES, 0);
      }

      // this method will allow the check box to be rendered in the 2nd column
      public java.lang.Class<?> getColumnClass(int columnIndex) {
         if (columnIndex == 0) {
            return Day.class;
         } else if (columnIndex == 1) {
            return Boolean.class;
         } else {
            return super.getColumnClass(columnIndex);
         }
      };
   }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373