Reputation: 5345
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:
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.ScrollPaneConstants;
import net.miginfocom.swing.MigLayout;
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?
Upvotes: 1
Views: 112
Reputation: 109813
JTableHeader
required the JScrollPane
as container
you have to get JTableHeader
from JTable
and put it, laid it separatelly e.g. panel.add(table1.getTableHeader(), "constant, constant, constant");
, there to use BorderLayout as better, simpler LayoutManager
for JPanel
than BoxLayout
is, e.g. panel.add(table1.getTableHeader(), BorderLayout.NORTH);
, then put JTable
to the CENTER area
better should be - don't to use JPanel
in JScrollPane
, put JTable
directly to the JScrollPane
, then JTableHeader
is visible, otherwise you have to implements Scrollable
for JPanel
for natural scrolling
use MigLayout
for whole container (is designated for), by using this custom LayoutManager
isn't required mixing different LayoutManagers
use DefaultTableModel
for storing value for JTable
s view
see Oracle tutorial Initial Thread
setPreferredScrollableViewportSize
is used by JScrollPane
your setting for PreferredSize
creates **** Swing GUI
tableProperties
contains two useless code lines table.repaint();
and table.revalidate();
and with wrong orderingUpvotes: 6