Shaheedul  Islam
Shaheedul Islam

Reputation: 125

My column names doesn't show up in my JTable

When I run this program, my column names don't show up. I have tried to search the error up on google, but no one seems to have this error. I have made a successful array for the data and column names but it still doesn't work.

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);
    }
}

Upvotes: 2

Views: 82

Answers (2)

Sva.Mu
Sva.Mu

Reputation: 1131

Try using JScrollPane as a container for your table.

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

Upvotes: 1

Bruno Caceiro
Bruno Caceiro

Reputation: 7199

You have to put your JTableinside a ScrollPane:

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

Upvotes: 4

Related Questions