1

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

    }
}
Julien Breuil
  • 165
  • 1
  • 2
  • 15
  • do you mean that each Item class will have different number of attributes that you would like to show in columns of the JTable? – Randy Jan 08 '13 at 16:54
  • 1
    there are bunch of thread about `AbstractTableModel` based on `util.List`, see Related (left bottom), rest of question might be un_rellated, without posting an [SSCCE](http://sscce.org/), short, runnable, compilable, – mKorbel Jan 08 '13 at 17:00
  • yes, each item have different number of attributes, so i need to make a different JTable for each of them, making a different model too – Julien Breuil Jan 08 '13 at 17:01
  • @mKorbel i have checked lot of them and still checking for more, but i don't find people creating different AbstractTableModel depending of the type of the object while keeping the generics works. btw the only thing i don't understand with the model is how i will be able to give him specific item (A for example) while the only thing i have is a List which is generic – Julien Breuil Jan 08 '13 at 17:04
  • @mKorbel just read all of their answers, i understand the point about ModelListener but it wasn't a problem that block me actually. I just don't understand what i can put in my SSCCE because i just dunno how to make compilable my idea in fact because i don't want to use instanceof() since i think it will be a misunderstanding of the real problem, i can try to post something but it will be a try of implementation and it will not works like i want . . . (go work on it) – Julien Breuil Jan 08 '13 at 17:33
  • Do you have a list that can only contain A, only contain B or only contain C? Or do you have a list that can contain instances of A, B and C at the same time? – JB Nizet Jan 08 '13 at 18:35
  • @JB Nizet I'm not supposed to know how many type of Item exists so i don't know how to make a function that make separate list like you said. But yes the only thing i have is a list which contains Items so it can contains A,B,C or w/e at the same time. – Julien Breuil Jan 08 '13 at 18:50
  • So, you mean there could also be other subclasses, and the table should dynamically display the additional attributes that might exist on these subclasses? – JB Nizet Jan 08 '13 at 20:00
  • @JB Nizet Yes, the table check how many different subclasses there are in my collection and i must count them to make a JTable in a tab for each subclasses. Currently i'm doing something like : `for(Item i : mp.keySet()){ Object o = i.getClass(); if(!listType.contains(o)){ listType.add(o); } } ` but i think it's ugly and not a good programming – Julien Breuil Jan 08 '13 at 23:04
  • 1
    @Julien Breuil have to look at J(X)TreeTable / Netbeans Outline – mKorbel Jan 09 '13 at 09:53
  • @mKorbel seems really great, i don't know yet how i will use it but it offers lot of possibility ty. – Julien Breuil Jan 09 '13 at 12:28

1 Answers1

3

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
}
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • i'm totally agree with that but i think i didn't explain correctly my problem correctly. When i will create my JTable for A for example, i will give a Default or AbstractTableModel but i need to put data in this model right? but how can i fill this data with only object of the same type of A, if i use instanceof() this supposed that i made a mistake ealier no? For the rest i have already check EnvTableTest and i know through it how to make custom ColumnName ect. – Julien Breuil Jan 08 '13 at 19:37
  • Oops, sorry about that, I meant `getColumnClass()`. Yes, your `TableModel` can expose as much or as little of your application's data model as desired. – trashgod Jan 08 '13 at 19:47
  • Well, that was not what i was searching for but it could help later, thank you. I think my english isn't enough skilled to explain my problem, will just make an ugly code to get the nomber of different instance of item it should unblock me, then i could concentrate on the TableModel implementation but EnvTableTest seems to answers to all the question i could have . – Julien Breuil Jan 08 '13 at 19:56
  • Sorry for any miscommunication. It may help to know that a `JTable` listens to it's `TableModel`, so updating the model _should_ update any listening view; if not, it suggests an error, for [example](http://stackoverflow.com/a/13500910/230513). You may be able to minimize `instanceof` using a [generic interface](http://stackoverflow.com/a/1623494/230513). – trashgod Jan 08 '13 at 22:59