1

I have problems regarding implementing JTable, specifically the AbstractTableModel. I can create and show data from JTable but my current scenario is different from my usually scenario.

What I want to do is to make my JTable accept an input from user and I want to put other components inside the JTable

Lets say for every row there is a JComboBox, JTextField and a JCheckBox, however when I start to implement the JTable model (AbstractTableModel) I don't know how can I put those components to my JTable?

Since the method getValue from AbstractTableModel return type is of object data types. Do you have any workaround for this kind of problems and by the ways I hate using DefaultTableModel since it lacks flexibility as much as possible I want an AbstractTableModel :) help is really appreciated.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Heidi Lilybeth
  • 127
  • 1
  • 7
  • 17
  • 1
    `TableModel` has a method called `getColumnClass` which returns a `Class` type of the specified column. The `JTable` uses this information to look up the required editor and renderer to use (based on the class type). Check out [How to use Tables](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html) for more details – MadProgrammer Feb 19 '13 at 04:48
  • thanks I'll read this, well you look at that actually this is also what I've search for still thanks for the fast answer :) – Heidi Lilybeth Feb 19 '13 at 04:55

1 Answers1

2

You should definitely start with How to Use Tables and study the examples there. While a JTable ins't a spreadsheet, you can calculate derived values in your table's model. This example shows how to use a JComboBox to update other cells in the same row, and these examples similarly contrast the two TableModel implementations you mention. Finally, you can use an available ScriptEngine to evaluate simple arithmetic expressions. For example, the example below prints 42.0.

ScriptEngine engine = mgr.getEngineByExtension("js");
try {
    System.out.println(engine.eval("5 * 8 + 2"));
} catch (ScriptException ex) {
    ex.printStackTrace(System.err);
}
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045