0

I am trying to fit data from a three-dimensional matrix into multiple JTables. I am using a layout that consists of multiple panels which are associated in a manner that gives me the layout I target (I'm not too familiar with GridBagLayout, so I'm putting my own layout together). The problem is now that the tables are not displayed on the Frame, and I don't know if the problem are the multiple panels or if it's because I'm using a 3-dimensional matrix. Using JTable works when not using fields of tables, layers, etc. - I assume sth. must go wrong there. I'm very grateful for your help and tipps! Thanks a lot!

Here's my code:

import java.awt.*;
import javax.swing.*;
import javax.swing.JTable;

public class Tabellen extends JFrame{

private static final long serialVersionUID = 7526472295622776147L;
Container c;
JPanel p_tabellen;
JPanel[] p_tab;
JTable[] table;
String[] columnnames={};
String[][][] matrixStr;
double[][][] matrix;

public Tabellen(double [][][] matrix) {
    //create a matrix of Strings from a double-matrix that can be read by the JTable constructor
    this.matrix=matrix;
    matrixStr = new String[matrix.length][matrix[0].length][matrix[0][0].length]; 
    for (int dim=0; dim<matrix.length; dim++){
        for (int zeile=0; zeile<matrix[0].length; zeile++){
            for (int spalte=0; spalte<matrix[0][0].length; spalte++){
                matrixStr[dim][zeile][spalte]= String.valueOf(matrix[dim][zeile][spalte]);
            }
        }
    }

    //create panels and Layouts
    c = getContentPane();
    p_tabellen= new JPanel(new GridLayout(matrix.length,1));
    p_tab= new JPanel[matrix.length];
    for (int p=0; p<matrix.length; p++){
        p_tab[p]= new JPanel(new BorderLayout());
        p_tabellen.add(p_tab[p]);
    }
    c.add(p_tabellen);

    //create one table per panel
    table = new JTable[matrix.length];
    for (int dim=0; dim<matrix.length; dim++){
        for (int zeile=0; zeile<matrix[0].length; zeile++){
            for (int spalte=0; spalte<matrix[0][0].length; spalte++){
                table[dim]= new JTable(matrixStr[dim],columnnames);
                p_tab[dim].add(table[dim], BorderLayout.CENTER);
            }
        }
    }

}
}

Input example:

public class TEST {

public static void main(String[] args) {

    double [][][] matrix = {{{2,4,6},{7,8,9}},{{1,2,3},{3,4,8}},{{1,2,4},{5,7,9}},{{2,4,6},{7,8,9}},{{1,2,3},{3,4,8}},{{1,2,4},{5,7,9}},{{2,4,6},{7,8,9}},{{1,2,3},{3,4,8}},{{1,2,4},{5,7,9}},{{2,4,6},{7,8,9}},{{1,2,3},{3,4,8}},{{1,2,4},{5,7,9}},{{2,4,6},{7,8,9}},{{1,2,3},{3,4,8}},{{1,2,4},{5,7,9}}};

    Tabellen d= new Tabellen(matrix);

    d.setTitle("test");
    d.setSize(1300,720);
    d.setVisible(true);
    d.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

}

1 Answers1

1

Assuming each of dim tables is meant to display zeile rows in spalte columns, create a List<TableModel> having dim entries, one for each table. Create a single JTable and update its model using setModel(). Let the user select the currently displayed model using an adjacent control. This example uses a JComboBox, but JSpinner is a good alternative. More on creating a TableModel may be found here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Thank you for your answer! Your suggestion is definitely the more elegant approach. However, I aim on displaying all charts at the same time so the user can compare them more easily. It works now when I add the following lines: table[dim].setAutoResizeMode( JTable.AUTO_RESIZE_OFF ); & sp[dim]= new JScrollPane(table[dim]); & table[dim].setPreferredScrollableViewportSize(table[dim].getPreferredSize());. I'll keep in mind your suggestions for the next time though. – user2737095 Sep 06 '13 at 08:41
  • Also consider a `GridLayout` of `JPanel`; override `getPreferredScrollableViewportSize()`, as shown [here](http://stackoverflow.com/a/18599978/230513). – trashgod Sep 06 '13 at 10:14