Reputation: 165
I have a working program using a Model
composed by a list of Item
.
public class Model {
private List<Item>;}
public abstract class Item{}
public class A extends Item{}
public class B extends Item{}
public class C extends Item{}
But now i need to create a view with some action buttons (add,del,edit) with a JTable on the center showing Specific Item (A,B,C). Here is my first problem because each specific item will have a column for one of its field, so i need a different table for each item.
A solution could be to have a cardlayout with one table for each type of item, but it comes a new problem how can my view determine how many type of item there is in the model without using instanceof()
?
Moreover i will have others problems after this one, if i have x jtable in my view how will my view get the model of this table ? i can implements for each item an interface like that :
public interface MyModel{
AbstractTableModel getModel();
}
but i can only give to this function a List, so how each item will fill the data array with only its type of item?
PS : if i go further in my reflexion i have a bonus question, i'm wondering how the listener of my action button can simply know which JTable is currently being modify, should i put the listener in the view for simplicity of access to the cardlayout ?
If something is needed to improve my question, ask for it ! Not sure the question is currently clear.
EDIT : adding SSCCE, this is what i'm aiming but it currently doesn't use items of my program and doesn't implement the TableModel/TableModelListener.
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
public class View extends JPanel implements TableModelListener{
private static final long serialVersionUID = 1L;
private JTabbedPane card;
public View() {
Object rowData[][] = { { "1", "one", "I" }, { "2", "two", "II" }, { "3", "three", "III" } };
String columnNames[] = { "#", "English", "Roman" };
Object rowData2[][] = { { "1", "B" } };
String columnNames2[] = { "#", "type" };
setLayout(new BorderLayout());
JPanel actionbutton = new JPanel();
JButton but = new JButton("fire");
but.addActionListener(new ButtonListener());
actionbutton.add(but);
add(actionbutton,BorderLayout.SOUTH);
card = new JTabbedPane();
//something should determine how many type of object in a List<Item> w/o using instanceof
//should fill jtable with a specific TableModel for each item type
JTable card1 = new JTable(rowData,columnNames);
JTable card2 = new JTable(rowData2,columnNames2);
card.addTab("Item A",new JScrollPane(card1));
card.addTab("Item B",new JScrollPane(card2));
add(card,BorderLayout.CENTER);
}
private class ButtonListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if ("fire".equals(cmd)) {
//do something on the model
}
}
}
public static void main(String[] args) {
JFrame f = new JFrame();
f.setSize(800, 600);
f.add(new View());
f.setVisible(true);
}
@Override
public void tableChanged(TableModelEvent e) {
//update jtable with the model's modification
}
}
Upvotes: 1
Views: 1006
Reputation: 205885
TableModel
supports generic types using Class Literals as Runtime-Type Tokens. Your implementation of getColumnClass()
(below) defines the types available to the table's columns, and it defines the selection of any non-default renderers and editors. While DefaultTableModel
offers considerable convenience, AbstractTableModel
is only slightly more difficult and substantially more flexible. As a concrete example, this EnvTableTest
illustrates a table modeled on a Map<String, String>
.
@Override
public Class<?> getColumnClass(int columnIndex) {
// return a token for the specified column
}
Upvotes: 3