简体   繁体   中英

Adding Jtable into jframe

I'm trying to add a JTable into the frame, but no works, i had tried:

public Jtablee()
    {
        setSize(400,400);

        String[] columnsNames = {"firstName", "LastName", "sport", "# ofYear", "vegetarian"};

        Object[][] data = {{"Katty", "Smith", "SnowBoard", new Integer(5), new Boolean(false)}, {"Jhon", "Doe", "Rowing", new Integer(3), new Boolean(true)},
                {"Sue", "Black", "Knitting", new Integer(2), new Boolean(false)},{"Jane", "White", "Speed ride", new Integer(20), new Boolean(true)}};

        JTable t = new JTable(data, columnsNames);
        add(t.getTableHeader(), BorderLayout.PAGE_START);
        add(t, BorderLayout.CENTER);

        setLayout(new BorderLayout());
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

Just the blank frame appear, without the JTable

Note: I don't want to add it into a scrollPane, I wanna add it directly.

You do not need to call setLayout(new BorderLayout()) as the content pane Container already has that layout. Calling it after adding your components will make them vanish.

You also do not need to call setSize() on the JFrame . Instead you should call pack() which will automatically resize it to fit its contents (the table)

Finally you should not be be using new Integer() and new Boolean() here as it is unnecessary boxing, instead use the primitives.

So your code should look like this:

import javax.swing.*;
import java.awt.*;

public class Test extends JFrame {

    public Test() {

        String[] columnsNames = {"firstName", "LastName", "sport", "# ofYear", "vegetarian"};

        Object[][] data = {
                {"Katty", "Smith", "SnowBoard", 5, false},
                {"Jhon", "Doe", "Rowing", 3, true},
                {"Sue", "Black", "Knitting", 2, false},
                {"Jane", "White", "Speed ride", 20, true}
        };

        JTable table = new JTable(data, columnsNames);
        add(table.getTableHeader(), BorderLayout.PAGE_START);
        add(table, BorderLayout.CENTER);

        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }

    public static void main(String[] args) {
        new Test();
    }
}

要让 JTable 可见,您唯一要做的就是删除"setLayout(BorderLayout.CENTER)",因为这与您的代码发生冲突(它覆盖了之前创建的 BorderLayout)。

I want here to present my example to add a simple table into JFrame using JScrollPane and without possibility of reordering the table header by users.

import java.awt.*;
import javax.swing.*;

public class addTable extends JFrame{

    JTable table;
    public addTable(){
        setLayout(new FlowLayout());
        String [] columnNames = {
            "First name", "Last name", "Header"};
        Object [][] data = {{ "Bill", "George", "Male"}, 
            {"Marry", "Anna", "Female"}, {"Rick",  
            "Bernard", "Male"}};
        table = new JTable(data, columnNames);
        table.setPreferredScrollableViewportSize(new Dimension(650, 75) );
        table.setFillsViewportHeight(true);
        JScrollPane scrollPane = new JScrollPane(table);
        add(scrollPane);
    }
    public static void main(String args[]){
        addTable t = new addTable();
        t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        t.setSize(800, 400);
        t.setTitle("My Table");
        t.getTableHeader().setReorderingAllowed(false);
        t.setLocationRelativeTo(null);
        t.setVisible(true);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM