0

I am trying to make an application, which displays a JLabel and a button, which if clicked switches to another jPanel. For some reason my JLabel is not displaying at all in either case. I would appreciate an expert eye to look over my code and see what I am doing wrong. Thanks in advance.

HomeScreenUI(){
        //frame
        JFrame frame = new JFrame("Opisa");

        //panels, one before button click and one after
        JPanel panel = new JPanel();
        JPanel panelAfterButtonClick = new JPanel();
        panel.setLayout(null);
        panelAfterButtonClick.setLayout(null);
        
        //jlabel that isnt displaying + dimensions
        JLabel label = new JLabel("Opisa");
        Dimension size = label.getPreferredSize();
        label.setBounds(100, 100, size.width, size.height);
        label.setFont(new Font("Helvetica", Font.PLAIN, 70));
        
        //second jlabel that isn't displaying
        JLabel label2 = new JLabel("Opisa");
        Dimension size4 = label2.getPreferredSize();
        label2.setBounds(100, 100, size4.width, size4.height);
        label2.setFont(new Font("Helvetica", Font.PLAIN, 70));
        
        //adding the labels to the panels
        panel.add(label);
        panelAfterButtonClick.add(label2);
        
        //button that is displaying both before and after
        JButton button = new JButton("Click Me..");
        JButton buttonAfterClick = new JButton("Clicked Me..");
        //dimensions
        Dimension size2 = button.getPreferredSize();
        button.setBounds(100, 100, size2.width, size2.height);
        Dimension size3 = button.getPreferredSize();
        buttonAfterClick.setBounds(100, 100, size3.width, size3.height);
        //adding the buttons to the jpanel
        panel.add(button);
        panelAfterButtonClick.add(buttonAfterClick);
        
        //function that changes the panel after the button is clicked
        button.addActionListener(new ActionListener() {

              public void actionPerformed(ActionEvent event) {
                  frame.setContentPane(panelAfterButtonClick);
                  frame.invalidate();
                  frame.validate();
              }

            });
        //adding the panel to the frame and setting the size
        frame.add(panel);
        frame.setSize(1000,800);
        frame.setVisible(true);
    }
mohelt
  • 55
  • 2
  • 6
  • 2
    *"JPanel not displaying JLabel"* Uh-huh. My first guess(1) as to the reason would be.. `..setLayout(null);` 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). 1) Prove me wrong by reproducing the problem using layouts. – Andrew Thompson Mar 09 '21 at 18:02
  • 1
    *"switches to another jPanel"* Use a [`CardLayout`](http://download.oracle.com/javase/8/docs/api/java/awt/CardLayout.html) as shown in [this answer](http://stackoverflow.com/a/5786005/418556). – Andrew Thompson Mar 09 '21 at 18:03
  • 1
    Just to complement the comments above by @AndrewThompson here's an [example](https://stackoverflow.com/questions/42520492/jtable-not-showing-up-on-jframe-java/42521097#42521097) of what could go wrong if you insist on going the `null-layout` path. Misery and suffering is all you'll find at the end... – Frakcool Mar 09 '21 at 22:00

1 Answers1

0

Check the label bounds, more specifically its size... try setting some fixed values (great enough to show its content like setBounds(100, 100, 250, 80)).

The preferred size is being retrieved before changing the font size, so it is not big enough to show that big characters. Try changing the font first.

  • 3
    *"..and not recommended unless for very special cases."* I never said that. If the programmer cannot find a suitable layout and believes they have the expertise to determine the size and position of the children, they should encode that logic in a custom layout. So the special circumstance is .. ***never.*** – Andrew Thompson Mar 09 '21 at 18:18
  • *"but maybe the user is the one determining the position and size"* So code a `DragAndDrop` layout. It can be used to determined which of overlapping components gets drawn last / on top, or whether underlying components are truncated, squished or removed completely, or whether the dropped component positions and sizes are snapped to a grid... – Andrew Thompson Apr 06 '21 at 00:33