Table header is not shown
Posted
by
Vivien
on Stack Overflow
See other posts from Stack Overflow
or by Vivien
Published on 2014-08-25T10:12:09Z
Indexed on
2014/08/25
10:20 UTC
Read the original article
Hit count: 297
My error is that the table headers of my two tables are not shown. Right now I am setting the header with new JTable(data, columnNames)
.
Here is an example which shows, my problem:
public class Test extends JFrame {
private static final long serialVersionUID = -4682396888922360841L;
private JMenuBar menuBar;
private JMenu mAbout;
private JMenu mMain;
private JTabbedPane tabbedPane;
public SettingsTab settings = new SettingsTab();
private void addMenuBar() {
menuBar = new JMenuBar();
mMain = new JMenu("Main");
mAbout = new JMenu("About");
menuBar.add(mMain);
menuBar.add(mAbout);
setJMenuBar(menuBar);
}
public void createTabBar() {
tabbedPane = new JTabbedPane(JTabbedPane.TOP);
tabbedPane.addTab("Settings", settings.createLayout());
add(tabbedPane);
tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
}
private void makeLayout() {
setTitle("Test");
setLayout(new BorderLayout());
setPreferredSize(new Dimension(1000, 500));
addMenuBar();
createTabBar();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
public void start() {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
makeLayout();
}
});
}
public static void main(String[] args) {
Test gui = new Test();
gui.start();
}
public class SettingsTab extends JPanel {
public JScrollPane createLayout() {
JPanel panel = new JPanel(new MigLayout(""));
JScrollPane sp = new JScrollPane(panel);
sp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
panel.add(table1(), "growx, wrap");
panel.add(Box.createRigidArea(new Dimension(0,10)));
panel.add(table2());
// panel.add(Box.createRigidArea(new Dimension(0,10)));
return sp;
}
public JPanel table1() {
JPanel panel1 = new JPanel();
String[] columnNames = {"First Name",
"Last Name"};
Object[][] data = {
{"Kathy", "Smith"},
{"John", "Doe"},
{"Sue", "Black"},
{"Jane", "White"},
{"Joe", "Brown"},
{"John", "Doe"},
{"Sue", "Black"},
{"Jane", "White"},
{"Joe", "Brown"}
};
final JTable table = new JTable(data, columnNames);
tableProperties(table);
panel1.add(table);
panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS));
return panel1;
}
public JPanel table2() {
JPanel panel1 = new JPanel();
String[] columnNames = {"First Name",
"Last Name"};
Object[][] data = {
{"Kathy", "Smith"},
{"John", "Doe"},
{"Sue", "Black"},
{"Jane", "White"},
{"Joe", "Brown"},
{"John", "Doe"},
{"Sue", "Black"},
{"Jane", "White"},
{"Joe", "Brown"}
};
final JTable table = new JTable(data, columnNames);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);
tableProperties(table);
panel1.add(table);
panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS));
return panel1;
}
public void tableProperties(JTable table) {
table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
table.repaint();
table.revalidate();
}
}
}
Any recommendations what I am doing wrong?
© Stack Overflow or respective owner