0
JTable table = new JTable(data,columnNames);
JScrollPane pane = new JScrollPane(table);
this.add(pane);
this.add(table);

My data is showing but column name is not showing on top.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • How you implementing `TableModel` ? Please have a look at this [thread](http://stackoverflow.com/q/9647522/1057230). Hopefully that might be able to answer this post :-) – nIcE cOw Jan 11 '14 at 15:24

2 Answers2

3

A component can only have a single parent.

JScrollPane pane = new JScrollPane(table);
this.add(pane);
this.add(table);

First you add the table to the viewport of the scrollpane, which is good as this will cause the table header to automatically be displayed when the GUI is made visible.

But then you add the table directly to the frame, which is bad because it can no longer be displayed in the scrollpane.

Get rid of:

//this.add(table);

and then the scrollpane containing the table will be displayed properly on the frame.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • I commented that line,but its not showing header(column name) on top.but when i am printing in console it is showing both data and column name.. – user2592244 Jan 11 '14 at 17:12
  • 1
    Read the JTable API and follow the link to the Swing tutorial on `How to Use Tables` for a working example. If you still have problems then post a [SSCCE](http://sscce.org/) demonstrating the problem. I've given my best guess based on your 4 lines of code. – camickr Jan 11 '14 at 19:33
  • @camickr The SSCCE has been 'retired' in favor of the [MCVE](http://stackoverflow.com/help/mcve). The latter document is shorter, is hosted at Stack Exchange and was devised by consensus of the community. Though you might like to know.. – Andrew Thompson Jan 12 '14 at 02:48
  • 1
    @AndrewThompson, No problem. I liked that your version explicitly stated "copy, paste, compile and run" yet we still got code that we couldn't test. Crossing fingers that the new version helps with the quality/completeness of code examples. – camickr Jan 12 '14 at 03:36
  • *"..stated "copy, paste, compile and run""* That was succinct, unfortunately it excluded code for which the problem was actually a compiler error, also it did not apply to (for e.g.) HTML/JS problems *"..yet we still got code that we couldn't test."* Heck, I've *already* encountered problems for which the OP either did not visit the MCVE, or could not pay attention for the entire screen full (it almost fits on a single screen view without scrolling, on my current 1920x1080 monitor when FF is full screen height). We can lead a horse to water.. – Andrew Thompson Jan 12 '14 at 03:50
  • @user2592244 _I commented that line,but its not showing header_ then there is something wrong with the code you are **not** showing. Come on, you can't seriously expect us to read your editor through earth and/or water, can you :-) – kleopatra Jan 12 '14 at 09:07
0

Have a look at this example

import java.awt.Color;
import javax.swing.*;
public class table extends JFrame{

    public table() {
        setSize(600, 300);
        String[] columnNames = {"A", "B", "C"};
        Object[][] data = {
            {"Moni", "adsad", 2},
            {"Jhon", "ewrewr", 4},
            {"Max", "zxczxc", 6}
        };

        JTable table = new JTable(data, columnNames);
        JScrollPane tableSP = new JScrollPane(table);

        JPanel tablePanel = new JPanel();
        tablePanel.add(tableSP);
        tablePanel.setBackground(Color.red);

        add(tablePanel);
        setTitle("Marks");

        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                table ex = new table();
                ex.setVisible(true);
            }
        });
    }
}
ravibagul91
  • 20,072
  • 5
  • 36
  • 59
  • There is no reason at all for nesting panels... better not answer simple coding errors by over-complicated examples. As an aside: Please learn java naming conventions and stick to them. – kleopatra Jan 12 '14 at 09:03