Heidi Lilybeth
Heidi Lilybeth

Reputation: 127

Java Jtable spreadsheet like format

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.

Upvotes: 1

Views: 512

Answers (1)

trashgod
trashgod

Reputation: 205835

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);
}

Upvotes: 2

Related Questions