0

I have here the code when a button is clicked. It is supposed to add a JTable in the JPanel regularPanel. The problem is that it doesn't show in the panel.

I added the panel in the frame via clicking the panel component.

    String[] columnNames = {"First Name",
                            "Last Name",
                            "Sport",
                            "# of Years",
                            "Vegetarian"};

    Object[][] data = {
        {"Kathy", "Smith",
         "Snowboarding", new Integer(5), new Boolean(false)},
        {"John", "Doe",
         "Rowing", new Integer(3), new Boolean(true)},
        {"Sue", "Black",
         "Knitting", new Integer(2), new Boolean(false)},
        {"Jane", "White",
         "Speed reading", new Integer(20), new Boolean(true)},
        {"Joe", "Brown",
         "Pool", new Integer(10), new Boolean(false)}
    };
    JTable table = new JTable(data,columnNames);
    table.setBounds(100, 100, 100, 80);
    JScrollPane tableContainer = new JScrollPane(table);
    regularPanel.add(tableContainer);

What should I do to view that table in the panel?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
CrazyGirl
  • 35
  • 7
  • 1
    1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). 2) I would approach this by adding the `JTable` from the start, but *populating the table model* on button click. 3) `table.setBounds(100, 100, 100, 80);` Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. .. – Andrew Thompson May 19 '15 at 11:11
  • 1
    .. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson May 19 '15 at 11:11
  • what problem are you facing exactly ? is there any error message ? – Alok Mishra May 19 '15 at 11:13
  • The problem is that I cant see the table in the panel after clicking the button – CrazyGirl May 19 '15 at 11:15
  • Did you try `revalidate()` and `repaint()`? – Blip May 19 '15 at 11:18
  • did you see it after resizing the Frame ? @CrazyGirl – Alok Mishra May 19 '15 at 11:30
  • revalidate and repaint didnt do it. @Blip – CrazyGirl May 19 '15 at 11:37
  • nope. didnt see it. @AlokMishra – CrazyGirl May 19 '15 at 11:37
  • is your `regularPanel` an instance of `JPanel`? – Blip May 19 '15 at 11:39
  • Yes, it is a jpanel. @Blip – CrazyGirl May 19 '15 at 11:46
  • could you post the code related to the `regularPanel`? – Blip May 19 '15 at 11:49
  • Please show little more code , i am still not able to get what problem you are facing . i have run your code in my eclipse and it ran successfully and the table is shown . i need to to know how you are handling the button click event . – Alok Mishra May 19 '15 at 11:56

1 Answers1

0

Add your table to the JScrollPane viewport.

Try the following:

TableModel tableDataModel = new AbstractTableModel() {
    public int getColumnCount() { return 2; }
    public int getRowCount() { return 5; }
    public Object getValueAt(int row, int col) { return new Integer(row*col); }
JTable table = new JTable(tableDataModel);
JScrollPane scrollPane = new JScrollPane();
scrollPane.getViewport().add(table);

In your button event handler, you should also be sure to call revalidate() and potentially repaint() once the table container is added to your panel.

bblincoe
  • 2,393
  • 2
  • 20
  • 34