我的专栏名称没有显示在我的JTable中

时间:2015-08-29 18:15:44

标签: java swing jtable jscrollpane

当我运行此程序时,我的列名称不会显示出来。 我试图在谷歌搜索错误,但没有人似乎有这个错误。 我已经为数据和列名创建了一个成功的数组,但它仍然无法正常工作。

public class graphProgram extends JFrame {

    private JPanel inputPanel, graphPanel, buttonPane;
    private JTextField equationInput;
    private JLabel equationLabel;
    private JTable x_yValues;
    private JButton calculateButton;

    public graphProgram() {
        super("Graph Calculator");
        setSize(500, 500);
        setLayout(new BorderLayout());
        addComponents();
        setVisible(true);
    }
    public void addComponents() {
        String[] columnNames = {"X Values", "Y Values"};


        Object[][] data = {{new Integer(-2), ""},
                {new Integer(-1), ""},
                {new Integer(0), ""},
                {new Integer(1), ""},
                {new Integer(2), ""},
                {new Integer(3 ), ""}};

        equationInput = new JTextField();
        calculateButton = new JButton("Calculate");
        equationLabel = new JLabel("Equation: ");
        x_yValues = new JTable(data, columnNames);

        inputPanel = new JPanel(new GridLayout(0, 2));
        graphPanel = new JPanel(new GridLayout(0, 2, 5, 0));
        buttonPane = new JPanel(new GridLayout(0, 1));

        add(inputPanel, BorderLayout.NORTH);
        add(graphPanel, BorderLayout.CENTER);
        add(buttonPane, BorderLayout.SOUTH);

        inputPanel.add(equationLabel);
        inputPanel.add(equationInput);

        graphPanel.add(x_yValues);

        buttonPane.add(calculateButton);
    }
}

2 个答案:

答案 0 :(得分:4)

您必须将JTable放入ScrollPane

x_yValues = new JTable(data, columnNames);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setViewportView(x_yValues);

答案 1 :(得分:1)

尝试使用JScrollPane作为您桌子的容器。

JScrollPane scrollPane = new JScrollPane(x_yValues);
graphPanel.add(scrollPane); // replaces graphPanel.add(x_yValues);