表头未显示

时间:2014-08-25 10:12:09

标签: 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();
        }
    }
}

任何建议我做错了什么?

1 个答案:

答案 0 :(得分:6)

  • JTableHeader需要JScrollPane作为容器

  • 您必须从JTableHeader获取JTable并将其单独放置,例如panel.add(table1.getTableHeader(), "constant, constant, constant");,使用BorderLayout作为LayoutManagerJPanel更好,更简单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

  • {li>

    setPreferredScrollableViewportSizeJScrollPane

    使用
  • PreferredSize的设置会创建**** Swing GUI


  • void tableProperties包含两个无用的代码行table.repaint();table.revalidate();,订单错误