Ann*_*lee 1 java swing jtable jtableheader
我的错误是我的两个表的表头没有显示.现在我正在设置标题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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我有什么建议我做错了吗?
JTableHeader需要JScrollPane作为容器
你必须得到JTableHeader来自JTable并把它,把它separatelly例如panel.add(table1.getTableHeader(), "constant, constant, constant");,有使用BorderLayout的更好,更简单LayoutManager的JPanel比BoxLayout是,例如panel.add(table1.getTableHeader(), BorderLayout.NORTH);,然后把JTable该中心区域
更好的应该是-不要使用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();并且排序错误