1

I am using a JFrame & JPanel to create a frame.JFrame contains comboboxes,CheckBoxes,JLabels etc.Layout of JPanel is set to "null",because i am using setBounds(int,int,int,int) to set the position of components on JFrame.

There is also a JButton(named as "Submit").On submit button i have added actionListener,so that if i click on this button,A table will generate on frame(at runtime).

JTable is successfully displaying on JFrame but Table columns name are not displaying along with JFrame(Only table data is displaying when submit button is pressed).

I am also using setBounds with JTable.What should i have to do in this case ?

submitButton.addActionListener(new ActionListener()
{
    public void actionPerformed(ActionEvent e)
    {
       Object ColumnName[] = {"Document Name", "Modified Date", "Size(in MB)"};
       Object data[][] = { {"test.txt", "24-Oct-2011", "540"}};
       table = new JTable(data,ColumnName);
       table.setBounds(20,230,330,100);
       panel.add(table);
       panel.repaint();
       panel.revalidate();
     }
});

Give me your all valuable suggestions soon. Thank you.

2
  • Give me your all valuable suggestions soon. Thank you. == then follows suggestion by @Amn +1, there are all valuable stuff
    – mKorbel
    Oct 31, 2011 at 10:47
  • @mKorbel Sir i followed the same way which was suggested by Amn.But this is not working,all components are not add to the frame after using this way.
    – SilentBomb
    Oct 31, 2011 at 11:02

3 Answers 3

2

may it works revalidate(); method of panel . call after setting table on panel.
but JScrollPane is best way to do this.

1
  • I have used repaint() & revalidate() methods after adding table to panel.All RowData(Data) is displaying but without column Name.
    – SilentBomb
    Oct 31, 2011 at 9:29
2

I think that you couldn't:

  1. Use setBounds(int, int, int, int), because this is job for LayoutManager
  2. Recreate JTable at run time. Just implement DefalutTableModel, there you can add/remove TableRows, reset TableModel - all changes are immediately displayed in JTable.

And you could:

  1. (As mentioned both posters) put JTable into a JScrollPane.
  2. Go through examples from the table tutorial and tons of Swing related examples here.
3
  • sir i am not using any LayoutManager(set to null).So thats why i am using setBounds() method.
    – SilentBomb
    Oct 31, 2011 at 11:43
  • My Headername problem is solved. I am using two JPanels panel1 & panel2. panel1 contains all the components(including Submit Button) and panel2 contains the jtable(when submit button is clicked).But now the jtable is displaying on whole frame(including panel1).Can you suggest me solution to fix the both panels positions.
    – SilentBomb
    Oct 31, 2011 at 12:31
  • depends what JComponents are pleaced into 1st. JPanel, if is there only services for JTable, then follows tutorial for BorderLayout and place this JPanel to the NORTH or SOUTH area and JTable (inside JScrollPanel)to the CENTER area, then you GUI is pretty resiziable, better would be start new thread about layout JCompoentns in the GUI, only a few JComponents, there are going about idea how to do it, not how to debug your your project :-) idea is described sscce.org, then a chance of getting correct answer is higher
    – mKorbel
    Oct 31, 2011 at 14:03
1

You would need to add JTable into a JScrollPane first and then add that JScrollPane into your main panel.

JScrollPane jsp = new JScrollPane(jtable);

Have a look at this: JTable

EDIT: this thread discusses the same issue: Jtable Columns

7
  • I am using only a JPanel,according to you i have added JScrollPane into my main Panel,but its not working.Edit : That thread was relates with database.
    – SilentBomb
    Oct 31, 2011 at 9:24
  • I would assume that you would have to add the JPanel into your JFrame. SO the way its linked is: scrollpane.add(table) --> panel.add(scrollpane) --> frame.add(panel)
    – Aman
    Oct 31, 2011 at 9:30
  • table = new JTable(data,ColumnName); table.setBounds(20,230,330,100); panel.add(table); panel.repaint(); panel.revalidate(); I am adding Jtable to Jframe by this way.Thia code is written at actionListener(at submit button)
    – SilentBomb
    Oct 31, 2011 at 9:39
  • instead of adding the table into your panel, add it to a JScrollPane as I showed in my original reply and then add this scroll pane to your JPanel and finally add the JPanel to JFrame. Everything else would be same. JScrollPane creates a viewport for your table with column headers and scroll bars.
    – Aman
    Oct 31, 2011 at 9:45
  • I did according to your suggestion,but its not working.Now even data is also not displaying.I want to ask that whether i have to declare JScrollPane scrollpane=new JScrollPane(); inside actionListener or outside.
    – SilentBomb
    Oct 31, 2011 at 9:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.