0

In my

public UI(){

I have JTextField,JButtons and labels
I have also setLayout(null);

}

Now I'm Trying to create a JTable

JTable table = new JTable(data, headers);

But it wont display until I take out setLayout

For all the buttons,text boxes and labels I have setBounds();

How can I display the JTable while the setLayout is Null?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Shiv Chand
  • 43
  • 1
  • 6

1 Answers1

4

Don't use null Layout, instead use LayoutManagers. Java created for you amazing layouts, you just have to use it, and let the layout do your job instead of hard working of calculating the position and size.

That's my answer, but if you insist of using it, just call setBounds(x,y,width,height) method for the JTable. But again, don't use absolute positioning(null Layout).

//....
JScrollPane scrol = new JScrollPane(table);
scrol.setBounds(table.getBounds());
//....
Caffe Latte
  • 1,693
  • 1
  • 14
  • 32
  • @mKorbel: Yes, you're right, the header won't show up unill adding it to `JScrollPane` and set the bounds of scrollPane too. – Caffe Latte Mar 25 '14 at 08:11
  • I Tried doing this but still displayed nothing `table.setBounds(10,10,100,100);` – Shiv Chand Mar 25 '14 at 08:20
  • 2
    @Shiv Chand and before anything here to solve the conflict in your posts here `1. How can I display the JTable while the setLayout is Null?` v.s. `2. con.add(new JScrollPane(table), BorderLayout.SOUTH);`, because only one of ways can to shows your JTable, notice JTable can't be empty for `BorderLayout.SOUTH`, otherwise there is created rectangle of 10 x 10 pixels – mKorbel Mar 25 '14 at 08:26
  • @ShivChand: listen to mLorbel's advice, and I edited the post. – Caffe Latte Mar 25 '14 at 08:35
  • @Caffè not, please to amend code posted here with setPreferredScrollableViewportSize – mKorbel Mar 25 '14 at 08:53