2

I'd like to get some ordering into my JTable. Currently I have one large JTable containing all items. But since I'm dealing with (sub)selections I'd like to make some grouping.

Expressing this in textual form:

+---------------------------------------------------------------+
|  Heading1       Heading2      Heading3       Heading4         |
+---------------------------------------------------------------+
|+-------------------------------------------------------------+|
|| Subheading with a button to collapse/expand                 ||
|+-------------------------------------------------------------+|
|  Itemx          Itemx         Itemx          Itemx            |
|  Itemx          Itemx         Itemx          Itemx            |
|  Itemx          Itemx         Itemx          Itemx            |
|  Itemx          Itemx         Itemx          Itemx            |
|+-------------------------------------------------------------+|
|| Subheading with a button to collapse/expand                 ||
|+-------------------------------------------------------------+|
|  Itemx          Itemx         Itemx          Itemx            |
|  Itemx          Itemx         Itemx          Itemx            |
|  Itemx          Itemx         Itemx          Itemx            |
|  Itemx          Itemx         Itemx          Itemx            |
|+-------------------------------------------------------------+|
|| Subheading with a button to collapse/expand                 ||
|+-------------------------------------------------------------+|
|  Itemx          Itemx         Itemx          Itemx            |
|  Itemx          Itemx         Itemx          Itemx            |
|  Itemx          Itemx         Itemx          Itemx            |
|  Itemx          Itemx         Itemx          Itemx            |
+---------------------------------------------------------------+

Expressing this in HTML5: jsfiddle

Experimental SSCCE:

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

public class Main extends JPanel {
    public class TestGroup extends JPanel
    {
        public TestGroup()
        {
            super(new GridBagLayout());

            int i;

            for (i = 0; i < 50; i++) {
                GridBagConstraints c1 = new GridBagConstraints();
                c1.fill = GridBagConstraints.HORIZONTAL;
                c1.weightx = 1;
                c1.gridx = 0;
                c1.gridy = i;
                add(new TestGroupContainer(), c1);
            }

            Component glue = Box.createGlue();
            GridBagConstraints glueConstraints = new GridBagConstraints();
            glueConstraints.gridx = 0;
            glueConstraints.gridy = i;
            glueConstraints.weighty = 1;
            add(glue, glueConstraints);
        }
    }

    public class TestGroupContainer extends JPanel
    {
        public TestGroupContainer()
        {
            super(new GridBagLayout());

            TestGroupBody container = new TestGroupBody();

            GridBagConstraints headerConstraints = new GridBagConstraints();
            headerConstraints.fill = GridBagConstraints.HORIZONTAL;
            headerConstraints.anchor = GridBagConstraints.NORTH;
            headerConstraints.weightx = 1;
            headerConstraints.gridx = 0;
            headerConstraints.gridy = 0;
            add(new TestGroupHeading(container), headerConstraints);

            GridBagConstraints bodyConstraints = new GridBagConstraints();
            bodyConstraints.fill = GridBagConstraints.HORIZONTAL;
            bodyConstraints.anchor = GridBagConstraints.NORTH;
            bodyConstraints.weightx = 1;
            bodyConstraints.weighty = 1;
            bodyConstraints.gridx = 0;
            bodyConstraints.gridy = 1;
            add(container, bodyConstraints);
        }
    }

    public class TestGroupBody extends JPanel implements ActionListener
    {
        private boolean isVisible;

        private class TestItemModel extends AbstractTableModel {

            private final Object[][] rowData;
            private final String[] columnNames = { "Test", "Scenario", "Test mode", "Time [min]", "Selected", "State", "Timer" };

            public TestItemModel(final Object[][] data) {
                rowData = data;
            }

            @Override
            public int getRowCount() {
                // TODO Auto-generated method stub
                return this.rowData.length;
            }

            @Override
            public String getColumnName(int column) {
                return columnNames[column]; 
            }

            @Override
            public int getColumnCount() {
                // TODO Auto-generated method stub
                return columnNames.length;
            }

            @Override
            public Object getValueAt(int rowIndex, int columnIndex) {
                // TODO Auto-generated method stub
                return rowData[rowIndex][columnIndex];
            }

            @Override
            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return false;
            }

        }

        public TestGroupBody()
        {
            super(new BorderLayout());

            isVisible = true;

            Object[][] data = {
                    {"Kathy", "Smith",
                     "Snowboarding", "a", "a", "a", "a"}
                };
            TestItemModel model = new TestItemModel(data);
            JTable table = new JTable(model);

            add(table.getTableHeader(), BorderLayout.PAGE_START);
            add(table, BorderLayout.CENTER);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            setVisible(isVisible = !isVisible);
        }
    }

    public class TestGroupHeading extends JPanel
    {
        public TestGroupHeading(ActionListener btnActionListener)
        {
            super(new GridBagLayout());

            GridBagConstraints hoi1Constraints = new GridBagConstraints();
            hoi1Constraints.anchor = GridBagConstraints.WEST;
            hoi1Constraints.gridx = 0;
            hoi1Constraints.gridy = 0;
            hoi1Constraints.weightx = 0;
            final JButton button = new JButton("-");
            button.addActionListener(btnActionListener);
            button.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    if (button.getText().equals("+")) {
                        button.setText("-");
                    } else {
                        button.setText("+");
                    }
                }

            });
            add(button, hoi1Constraints);

            GridBagConstraints hoi2Constraints = new GridBagConstraints();
            hoi2Constraints.fill = GridBagConstraints.HORIZONTAL;
            hoi2Constraints.anchor = GridBagConstraints.WEST;
            hoi2Constraints.gridx = 1;
            hoi2Constraints.gridy = 0;
            hoi2Constraints.weightx = 1;
            hoi2Constraints.insets = new Insets(0, 10, 0, 0);
            add(new JLabel("hoi2"), hoi2Constraints);
        }
    }

    public static void main(String[] args) {
        // Schedule a job for the event-dispatching thread:
        // creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }

    public Main() {
        super(new BorderLayout(0, 0));

        JScrollPane scrollPane = new JScrollPane(new TestGroup());

        add(scrollPane, BorderLayout.CENTER);
    }

    /**
     * Create the GUI and show it. For thread safety, this method should be
     * invoked from the event-dispatching thread.
     */
    private static void createAndShowGUI() {
        // Create and set up the window.
        JFrame frame = new JFrame("SimpleTableDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Create and set up the content pane.
        Main newContentPane = new Main();
        newContentPane.setOpaque(true); // content panes must be opaque
        frame.setContentPane(newContentPane);

        // Display the window.
        frame.pack();
        frame.setVisible(true);
    }
}

My guess is that it'll be really hard to use out of the box JTable. Therefore I'm looking for any other suitable options. My SSCCE described a possible solution, but a big drawback is that a multiple of JTableHeader's are all across my application.

Does anyone have a smart idea to easily show my data as was seen in my HTML5 example? With or without JTable, I don't really mind.

Velth
  • 1,108
  • 3
  • 15
  • 29

1 Answers1

0

Turned out that a JXTreeTable suits my needs, this tip was given by @mrKorbel.

See http://javanbswing.blogspot.nl/2013/08/swing-treetable-example-using.html for a good tutorial on how to implement a basic JXTreeTable.

Velth
  • 1,108
  • 3
  • 15
  • 29