JTable标题仍未显示

时间:2014-05-30 02:03:09

标签: java swing user-interface jtable

我看过并发现许多人没有将他们的桌子放入scrollPane。即使我将我的表嵌入到scrollPane中然后进入框架,它仍然无法显示标题。还有什么我想念的吗?感谢

public class Gui extends JFrame {

AbstractTableModel model;
JTable table; 



public void start(AbstractTableModel model) { 
    this.model = model;
    table=new JTable(model){
        @Override 
        public boolean isCellEditable(int arg0, int arg1) { 
            return false; 
        }

    }; 


    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    TableColumn column = null;
        for (int i = 0; i < model.getColumnCount(); i++) {
            column = table.getColumnModel().getColumn(i);
            column.setPreferredWidth(120);
            column.setMaxWidth(300);
            column.setMinWidth(50);
        }    

        JScrollPane pane = new JScrollPane(table);  
        pane.setPreferredSize(new Dimension(900,900));

        add(pane); 
        setLayout(new FlowLayout()); 
        setVisible(true); 
        setDefaultCloseOperation(EXIT_ON_CLOSE); 
        pack();
}

1 个答案:

答案 0 :(得分:3)

根据您的代码,然后必须添加缺少的功能,您可以编写代码......

enter image description here

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

public class Gui extends JFrame {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                Gui frame = new Gui();
                frame.start(new DefaultTableModel(new Object[]{"A", "B", "C"}, 10));
            }
        });
    }

    AbstractTableModel model;
    JTable table;

    public void start(AbstractTableModel model) {
        this.model = model;
        table = new JTable(model) {
            @Override
            public boolean isCellEditable(int arg0, int arg1) {
                return false;
            }

        };

        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        TableColumn column = null;
        for (int i = 0; i < model.getColumnCount(); i++) {
            column = table.getColumnModel().getColumn(i);
            column.setPreferredWidth(120);
            column.setMaxWidth(300);
            column.setMinWidth(50);
        }

        JScrollPane pane = new JScrollPane(table);
        pane.setPreferredSize(new Dimension(900, 900));

        add(pane);
        setLayout(new FlowLayout());
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        pack();

    }
}

这导致了两个广泛的假设......

  1. TableModel没有任何列
  2. TableModel没有任何列名......
  3. 例如,TableModel没有列名...

    No column names

    ...个人

    • 我不会使用FlowLayoutBorderLayout会提供更好的结果
    • pack在您看到之前的框架
    • 在添加组件之前设置布局,有时会出现问题......

    例如......

    setLayout(new FlowLayout()); // But I'd use a `BorderLayout`
    add(pane);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    pack();
    setVisible(true);