1

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.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • What happens when you do this? Is the frame displayed at all? Any exception? – Thomas Jul 01 '16 at 16:24
  • @Thomas A blank jframe appear without the JTABLE –  Jul 01 '16 at 16:25
  • You might want to provide more information in that case, i.e. when do you add the table? What data/columns do you initialize it with? How's your frame configured? etc. – Thomas Jul 01 '16 at 16:28
  • 1) Add your JTable to a JScrollPane's viewport and add the scrollpane to the JFrame's contentPane, BorderLayout.CENTER. 2) If this doesn't work, then you must show more pertinent code, preferably a [mcve]. Please read the link. – Hovercraft Full Of Eels Jul 01 '16 at 16:29
  • Don't call `setLayout(...)`. The JFrame's contentPane already uses a BorderLayout. – Hovercraft Full Of Eels Jul 01 '16 at 16:32
  • @HovercraftFullOfEels even if a set a null frame Layout the error persist –  Jul 01 '16 at 16:40
  • @Cohen: using a null layout is the worst thing you can do to try to solve this. The best thing (again), is to post a complete compilable runnable **small** example program, a [mcve] that we can test ourselves. – Hovercraft Full Of Eels Jul 01 '16 at 16:45

3 Answers3

1

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();
    }
}
explv
  • 2,709
  • 10
  • 17
  • 1
    Don't forget to wrap [calls to Swing components on the EDT](http://stackoverflow.com/questions/15302085/why-to-use-swingutilities-invokelater-in-main-method) – copeg Jul 01 '16 at 17:05
0

Only thing you have to do to let visible your JTable, is delete the "setLayout(BorderLayout.CENTER)", cause that's making conflict with your code (it's overrading the BorderLayout previously create).

Volazh
  • 126
  • 2
  • 13
0

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);
    }
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197