Categories
java jtable layout layout-manager null-layout-manager

How to add JTable in JPanel with null layout?

243

I want to add JTable into JPanel whose layout is null. JPanel contains other components. I have to add JTable at proper position.

1

  • 19

    just to emphasize jzd’s answer: forget about null layout. That’s definitely un-swing’ish – either learn using LayoutManagers or use a different language.

    – kleopatra

    Apr 11, 2011 at 13:05

49

Don’t use a null layout. Learn to use LayoutManagers:

http://download.oracle.com/javase/tutorial/uiswing/layout/using.html

LayoutManagers allow you to properly handle things window resizing or dynamic component counts. They might seem intimidating at first, but they are worth the effort to learn.

    20

    As I can remember, the null layout means an absolute position so it will be pretty hard you to count the X point for your JTable left upper corner location. But if you just want to have all panel components one by one you can use FlowLayout() manager as

    JPanel panel=new JPanel(new FlowLayout());
    panel.add(new aComponent());
    panel.add(new bComponent());
    panel.add(new JTable());
    

    or if you need to fill the panel you should use GridLayout() as…

    int x=2,y=2;
    JPanel panel=new JPanel(new GridLayout(y,x));
    panel.add(new aComponent());
    panel.add(new bComponent());
    panel.add(new JTable());
    

    Good luck