-1

I have problem with displaying components on my JFrame. I'm closing my current window and opening new one and want to display jLabel on it but nothing is happening. Code is below :

               Frame[] nF = DBChooser.getFrames(); 

                nF[0].setVisible(false);
                JFrame windoow = new JFrame("Processing");                       
                JPanel pan = new JPanel(); 
                windoow.setPreferredSize(new Dimension(400, 150));  
                pan.setPreferredSize(new Dimension(400, 150));                  

                JLabel textLabel = new JLabel ("Processing...");   
                textLabel.setLayout(null);
                pan.setLayout(null);
                windoow.setLayout(null);                   
                pan.add(textLabel);
                pan.revalidate();
                pan.repaint();                 
                windoow.getContentPane().add(pan); 
                windoow.setLocationRelativeTo(null);                    
                windoow.pack();                  
                windoow.setVisible(true); 

I appreciate any help

johansonik
  • 35
  • 1
  • 7
  • 1) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 3) Provide ASCII art or a simple drawing of the *intended* layout of the GUI at minimum size, and .. – Andrew Thompson Aug 22 '17 at 11:39
  • .. if resizable, with more width and height. – Andrew Thompson Aug 22 '17 at 11:39

2 Answers2

1

It is because you set a null layout to window and panel without specifying any width, lenght or position, either use some LayoutManager or set these properties (eg. bounds). A null LayoutManager means that you need to set everything yourself, because there is nothing (no LayoutManager) that would place your elements automatically. This example uses a BorderLayout, which creates a nice effect:

enter image description here

the code:

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Test {
    public static void main(String[] args) {
        JFrame windoow = new JFrame("Processing");
        JPanel pan = new JPanel();
        windoow.setPreferredSize(new Dimension(400, 150));
        pan.setPreferredSize(new Dimension(400, 150));

        JLabel textLabel = new JLabel("Processing...");
        textLabel.setLayout(null);
        pan.setLayout(new BorderLayout());
        windoow.setLayout(new BorderLayout());
        pan.add(textLabel);
        pan.revalidate();
        pan.repaint();
        windoow.getContentPane().add(pan);
        windoow.setLocationRelativeTo(null);
        windoow.pack();
        windoow.setVisible(true);
    }
}
Krzysztof Cichocki
  • 6,294
  • 1
  • 16
  • 32
1

Why you need so many setLayout(null); ? I remove them and it worked

public class DBChooser extends Frame {

    public static void main(String args[]) {
        Frame[] nF = DBChooser.getFrames();
//      nF[0].setVisible(false);
        JFrame windoow = new JFrame("Processing");
        JPanel pan = new JPanel();
        windoow.setPreferredSize(new Dimension(400, 150));
        pan.setPreferredSize(new Dimension(400, 150));
        JLabel textLabel = new JLabel("Processing...");
//      textLabel.setLayout(null);
//      pan.setLayout(null);
//      windoow.setLayout(null);                   
        pan.add(textLabel);
        pan.revalidate();
        pan.repaint();
        windoow.getContentPane().add(pan);
        windoow.setLocationRelativeTo(null);
        windoow.pack();
        windoow.setVisible(true);
    }
}
John Joe
  • 12,412
  • 16
  • 70
  • 135