我的错误是我的两个表的表头没有显示。现在我正在使用new JTable(data, columnNames)
设置标题。
以下示例显示了我的问题:
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();
}
}
}
任何建议我做错了什么?
答案 0 :(得分:6)
JTableHeader
需要JScrollPane
作为容器
您必须从JTableHeader
获取JTable
并将其单独放置,例如panel.add(table1.getTableHeader(), "constant, constant, constant");
,使用BorderLayout作为LayoutManager
比JPanel
更好,更简单BoxLayout
,例如panel.add(table1.getTableHeader(), BorderLayout.NORTH);
,然后将JTable
放入CENTER区域
最好是 - 不要在JPanel
中使用JScrollPane
,将JTable
直接放到JScrollPane
,然后JTableHeader
是可见的,否则您必须为Scrollable
实现JPanel
以进行自然滚动
使用MigLayout
整个容器(指定用于),使用此自定义LayoutManager
并非需要混合使用不同的LayoutManagers
使用DefaultTableModel
存储JTable
视图的值
请参阅Oracle tutorial Initial Thread
setPreferredScrollableViewportSize
由JScrollPane
您PreferredSize
的设置会创建**** Swing GUI
tableProperties
包含两个无用的代码行table.repaint();
和table.revalidate();
,订单错误