-1

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.

Ivan
  • 490
  • 1
  • 7
  • 23

2 Answers2

2

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

StanislavL
  • 56,971
  • 9
  • 68
  • 98
0

NUllLayout is the most effficient layout manager becaouse of the freedom the comes with it, you can explicitly specify where your elements will be placed. But to realy work efficiently with it, you need to picture positions on your Frame in terms of pixels. Apply some Geometry in you coding consider the following code.

package com.samuTech.DialogBoxes;

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.JLabel;

public class NullLayout2 extends JFrame {

private static JLabel nameLabel;
private static JLabel passwordLabel;
private static JTextField userName;
private static JPasswordField pass;
private static JButton ok;

public NullLayout2(){

    super("Null Layout");

    setSize(getMaximumSize().width,getMaximumSize().height);
    setLocation(getLocation().x,getLocation().y);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
    getContentPane().setBackground(Color.BLUE);
    setLayout(null);

    nameLabel = new JLabel("User Name");
    passwordLabel = new JLabel("Password");
    userName =  new JTextField(20);
    pass = new JPasswordField(20);
    ok = new JButton("Ok");

    userName.setBounds(200,100,200,30);
    pass.setBounds(200,150,200,30);
    ok.setBounds(300,200,100,30);
    nameLabel.setBounds(5,100,150,30);
    passwordLabel.setBounds(5,150,150,30);

    add(userName);
    add(passwordLabel);
    add(nameLabel);
    add(pass);
    add(ok);

}

public void pain(Graphics g){

    g.drawString("Graphitii trials ", 55, 400);
}
public static void main(String[]args){
    javax.swing.SwingUtilities.invokeLater(

            new Runnable(){

                @Override
                public void run(){
                    new NullLayout2();
                }
            }

    );
}

}

Instead of giving up a tool you will obviously need in your software development life especial GUI design please consult in the official documentation for Null Layout here

Samuel Owino
  • 747
  • 1
  • 12
  • 24