سؤال

I have this code to create a JTable in my panel and I don't know where is the problem.

public void View()//method to store our view Scroll Pane
    {

        pnlView = new JPanel();
        pnlView.setSize(500,500);
        pnlView.setLayout(null);


        btnVBack = new JButton();
        btnVBack.setBounds(260,180,150,30);
        btnVBack.setText("BACK");
        btnVBack.addActionListener(this);
        pnlView.add(btnVBack);
        pnlView.setVisible(true);
        this.add(pnlView);



    }

So I try to add this code to add a JTable to my panel:

    public void View()
    {

        pnlView = new JPanel();
        pnlView.setSize(500,500);
        pnlView.setLayout(null);

        btnVBack = new JButton();
            btnVBack.setBounds(260,180,150,30);
        btnVBack.setText("BACK");
        btnVBack.addActionListener(this);
        pnlView.add(btnVBack);

        String[] columnNames = {"first name","last name","address"};
        Object[][]data = {{"John","Kane","NY"},{"Nayomi","Writz","NY"}};
        JTable table =  new JTable(data, columnNames);
        JScrollPane scrollPane = new JScrollPane(table);
        JPanel panel = new JPanel(new BorderLayout());

        this.add(table.getTableHeader(), BorderLayout.PAGE_START);
        this.add(scrollPane, BorderLayout.CENTER);
       getContentPane().add(panel);

        pnlView.add(scrollPane);

        pnlView.setVisible(true);
        this.add(pnlView);
}

Nothing happened.

هل كانت مفيدة؟

المحلول

  1. Don't use a null layout!!! Don't use setBounds(). Swing was designed to be used with layout managers.
  2. Don't add the TableHeader to a panel.
  3. You are adding the scrollPane to two different components. It should only be added to one.
  4. Panels are visible by default. You don't need pnlView.setVisible(true)
  5. Why are you creating the "panel" variable and adding an empty panel to the frame?

Read the JTable API and follow the link to the Swing tutorial on How to Use Tables for working examples. Start with working code. The tutorials also show you how to better structure your code by using SwingUtilities.invokeLater(...).

نصائح أخرى

Finally i found the Solution :

public void View()//method to store our view Scroll Pane
    {

        pnlView = new JPanel();
        pnlView.setSize(500,500);



        btnVBack = new JButton();
        btnVBack.setBounds(340,180,150,30);
        btnVBack.setText("BACK");
        btnVBack.addActionListener(this);
        pnlView.add(btnVBack);

        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBounds(0, 0, 330, 300);
        add(scrollPane);
        String columnNames[] = { "Column 1", "Column 2", "Column 3" };

        // Create some data
        String dataValues[][] =
        {
            { "12", "234", "67" },
            { "-123", "43", "853" },
            { "93", "89.2", "109" },
            { "279", "9033", "3092" }
        };
        table = new JTable(dataValues, columnNames);
        scrollPane.setViewportView(table);


        pnlView.setVisible(true);
        this.add(pnlView);



    }//view 
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top