0

i´m building a Swing-Gui and have an JTable object in an JScrollPane with my own TableModel. The Models nucleus is a LinkList that contains the data. I did it as LinkedList be able to insert rows as Object[] any time without replacing an 2D-Array. I wrote a method that inserts a Object[] in my LinkedList but i can´t call it from the JTable by calling the TableModel in the main-method and i don´t know why. Somebody has an idea?

public class TableModel extends AbstractTableModel {    

    String[] columnNames = { "Kundennummer","Firma","Nachname","Vorname","Telefonnummer","Emailadresse","Adresszeile 1","Adresszeile 2", "PLZ", "Ort", "Geburtsdatum"};

    int anzahlAttribute = 11;
    LinkedList<Object[]> data;

    public TableModel(LinkedList<Object[]> rows){
        this.data = rows;
    }

    @Override
    public String getColumnName(int col) {
        return columnNames[col].toString();
    }
    /**
     * @see javax.swing.table.TableModel#getColumnCount()
     */
    @Override
    public int getColumnCount() {
        // TODO Auto-generated method stub
        return columnNames.length;
    }

    /**
     * @see javax.swing.table.TableModel#getRowCount()
     */
    @Override
    public int getRowCount() {
        // TODO Auto-generated method stub
        return data.size();
    }

    /**
     * @see javax.swing.table.TableModel#getValueAt(int, int)
     */
    @Override
    public Object getValueAt(int arg0, int arg1) {
        // TODO Auto-generated method stub
        try{
        return data.get(arg0)[arg1];
        }catch(ArrayIndexOutOfBoundsException e){
            return "";
        }
    }

     public Class getColumnClass(int c) {
            return getValueAt(0, c).getClass();
     }

     @Override
     public void setValueAt( Object val, int row, int column ){
       data.get(row)[column] = val;
       this.fireTableDataChanged();
     }

     public void insertRow(Object[] rowData){
         this.data.add(rowData);
         this.fireTableRowsInserted(data.size()-1, data.size()-1);
     }





public class PaneCenter extends JScrollPane implements Panelsetter {

    private static PaneCenter c;
    private static JTable kundenTabelle;
    private TableModel tm;

    private PaneCenter(){
        super();
    }

    /**@  Constructor
     * 
     *
     *
     *
     *@param arg0
     */
    public PaneCenter(Component arg0) {
        super(arg0);
        // TODO Auto-generated constructor stub
    }

    public static PaneCenter getInstance(){
        if(c == null){
            c = new PaneCenter(kundenTabelle = new JTable());
        }
        return c;
    }

    public void setPanel(LinkedList<Object[]> data){
        c.setBackground(Color.GRAY);
        c.setBorder(new LineBorder(Color.black));
        c.setPreferredSize(new Dimension(500,350));
        c.setMinimumSize(new Dimension(500,350));
        c.setMaximumSize(new Dimension(500,350));
        kundenTabelle.setModel(tm = new TableModel(data));
        kundenTabelle.setAutoResizeMode(0);
        kundenTabelle.setSelectionMode(0);
    }

    public JTable getKundenTabelle() {
        return kundenTabelle;
    }

    public TableModel getTableModel(){
        return tm;
    }

    /**
     * @see interfaces.Panelsetter#setPanel()
     */
    @Override
    public void setPanel() {
        // TODO Auto-generated method stub

    }





public class Run {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Object[] aa = {"1", "2", "3", "4", "5", "1", "2", "3", "4", "5", "11"}
        PaneCenter.getInstance().setPanel(Bank.getCustomerDataArray())
        PaneCenter.getInstance().getKundenTabelle().getModel().insertRow(aa);


        Frame.getInstance().addPanels(PaneCenter.getInstance());
        Frame.getInstance().setVisible(true);

    }

}
Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
Severus0191
  • 39
  • 1
  • 8
  • _"i can´t call it from the JTable by calling the TableModel in the main-method and i don´t know why"_ -- What does this mean? – Jim Garrison Jan 02 '18 at 21:02
  • 1
    Don't call you model `TableModel`. `TableModel` is an interface so calling a class the same name as an interface is confusing. Your model name should be more descriptive. – camickr Jan 02 '18 at 21:03
  • 1
    *"The Models nucleus is a LinkList that contains the data*" - Since the `TableModel` needs random access, you'd be better off using an `ArrayList` instead – MadProgrammer Jan 02 '18 at 21:10
  • @JimGarrison that means i call my JTable -> the JTable calls it´s model (my own) -> the Model should call the method but it don´t. – Severus0191 Jan 02 '18 at 21:11
  • @matoni thanks!! had seen your post before it was deleted(??). It was that little thing i didn´t mind. Thanks a lot – Severus0191 Jan 02 '18 at 21:33

1 Answers1

0

I did it as LinkedList be able to insert rows as Object[] any time without replacing an 2D-Array

You don't need a custom table model. Just use the DefaultTableModel. The DefaultTableModel copies the data from a 2D Array to a Vector of Vectors.

You can then use the insertRow(...) method to insert a row of data anywhere in the model.

i can´t call it from the JTable by calling the TableModel in the main-method

You really shouldn't be trying to call it from the main() method. The main method should just create your frame and add a panel to the frame. The code that creates the frame should be responsible for add data to the table model.

Other comments:

  1. Don't extend JScrollPane. You have not changed the functionality of the scroll panel. So just create an instance of the scrollpane and add your panel to it.

  2. Variable names should not start with upper case characters.

  3. Don't use static variables in your class.

Look at the demo code from the Swing tutorial on How to Use Tables for examples of how to better structure your code to solve your main problem and all the other smaller problems.

it´s a university task and it says "...use a abstract data model...."

Then you would typically use an ArrayList to hold the data to you can insert directly where you want.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • thanks, but i know this... it´s a university task and it says "...use a abstract data model...." – Severus0191 Jan 02 '18 at 21:09
  • i can´t insert where i want if i don´t be able to call my own written method. That´s the main problem – Severus0191 Jan 02 '18 at 21:24
  • And I told you the problem is with the structure of your code. If you define your JTable in the panel where you add the table to the panel. Then you can easily define an instance variable for the JTable. Then you just get the model from the table and cast the model to your custom model (when you use a proper name). The tutorial has plenty of examples showing this type of design. – camickr Jan 02 '18 at 23:56