Search code examples
javaswinglayoutlayout-managernull-layout-manager

Element placement with null layout


I have a problem with null layout. Programm have a structure like this:

Window (JFrame)
    - Tabbed Pane (JTabbedPane)
      - .. Some tabs ..
      - Overridden JPanel wrapped in JScrollPane (class Table)

Also I have a overriden JLabel (class Kanban).

I try add in Table some instance of Kanban and have nothing. If I change layout of Table from null to BorderLayout(for example), element appears and works good.

Oracle documentation says about 3 steps: 1) set null layout, 2) call setBounds() on child elements and 3) call repaint() on element with null layout. It isn't work for me(very strage, really).

Table placement code (constructor of Window):

Table table = new Table();
JScrollPane panel = new JScrollPane(table);

tabbedPanel.addTab("New tab", panel);
tabbedPanel.setSelectedIndex(tabbedPanel.getTabCount() - 1);

table.setPreferredSize(new Dimension(600, 400));
table.setSize(600, 400);

Table constructor:

setDoubleBuffered(true);
setLayout(null);
setBounds(0,0,600,400);

Kanban kanban = new Kanban("Label text");
kanban.setBounds(10, 10, kanban.getWidth(), kanban.getHeight());

add(kanban);

What's wrong? Why elements don't draw in null layout?

--- Add I need a null layout because I need point location of labels.


Solution

  • kanban.getWidth(), kanban.getHeight() they are 0. But I agree with all the comments above. Don't use null layout. Define a panel with GridLayout and place all your labels there