1

I have the following JDialog class:

public class VHSInformationPanel extends JDialog {

    private static final long serialVersionUID = 1L;
    private JTable vhsTable;
    private String[][] data;
    private String[] columnNames;
    public VHSInformationPanel(String[][] data, String[] columnNames) {
        this.data = data;
        this.columnNames = columnNames;
        setLocationRelativeTo(null);
        setLayout(new FlowLayout());
        vhsTable = new JTable(data, columnNames);
        add(new JScrollPane(vhsTable,
                JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED));
        setVisible(true);
    }
}

The data for the JTable is correctly transmitted when the class is iniatilezed, however, when I press to show the JDialog it displays an empty JDialog. Why isn't the JTable contained?

The code that initializes the JDialog is:

public void showVHSInformationPanel(String[][] data, String[] columnNames) {
  vhsInformationPanel = new VHSInformationPanel(data, columnNames);
  vhsInformationPanel.setVisible(true);
}

Is it initialized wrong?

6
  • Could you indent your code sensibly please?
    – khelwood
    Jan 9, 2015 at 23:58
  • Can we assume you have some data that is passed in? Have you tried debugging this? Are you looking at the console to determine whether there is an exception of any sort?
    – arcy
    Jan 10, 2015 at 0:01
  • I have debugged this. The information is correctly passed and no exception is thrown.
    – FanFan
    Jan 10, 2015 at 0:04
  • OK. Can you reproduce this problem without data coming in?
    – k_g
    Jan 10, 2015 at 0:05
  • In fact, I have tried adding a simple label to the JDialog. It still didn't show up.
    – FanFan
    Jan 10, 2015 at 0:06

1 Answer 1

2

Ok, I made few modifications in your code:

import java.awt.BorderLayout;

import javax.swing.JDialog;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class VHSInformationPanel extends JDialog {

    private static final long serialVersionUID = 1L;
    private JTable vhsTable;

    public VHSInformationPanel(String[][] data, String[] columnNames) {
        setLocationRelativeTo(null);
        setLayout(new BorderLayout());
        vhsTable = new JTable(data, columnNames);
        add(new JScrollPane(vhsTable, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED),
                BorderLayout.CENTER);
        setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        // setModalityType(ModalityType.APPLICATION_MODAL);
        pack();
        setVisible(true);
    }
}

I changed FlowLayout to BorderLayout and added JScrollPane with JTable on CENTER of JPanel. Also I set proper default close operation for JDialog which is dispose on close. And finally I called pack so your JDialog will adjust its size based on preferred size of JTable. Optionally, you can set modality type.

Initialization was not a problem at all. Test code:

String[][] data = { { "bla", "bla" } };
String[] cols = { "col1", "col2" };
new VHSInformationPanel(data, cols);
1
  • Indeed, I just figured out that the problem was that I did not set the size of the JDialog. The table was inserted, but it wasn't shown due to the small size of the JDialog. Thank you for your help!
    – FanFan
    Jan 10, 2015 at 0:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.