-1

Why does my Inner JPanel not display my JLabel text?

I have an outer JPanel, and an inner JPanel to display some text with the right dimensions. However I don't understand why it won't show?

                JPanel jp = new JPanel();
                jp.setBackground(Color.decode("#ffffff"));
                jp.setBounds(0, 35, 400, 315);

                JPanel mostInner = new JPanel();
                mostInner.setForeground(Color.black);
                mostInner.setBounds(207, 5, 190, 240);
                jp.add(mostInner);

                JLabel jltxt = new JLabel();
                jltxt.setText("Test");

                mostInner.add(jltxt);
mKorbel
  • 109,525
  • 20
  • 134
  • 319
FareJump
  • 113
  • 2
  • 10
  • have you set visibility to true.. – Iamat8 Dec 27 '15 at 16:15
  • 2
    1. For best help, create and post your minimal runnable example program or [mcve] (please check the link). 2. You appear to be using setBounds and perhaps a null layout, both of which are generally bad ideas. If you instead post an image of what you're trying to create (or a link to such image) with your [mcve], we can better help you. – Hovercraft Full Of Eels Dec 27 '15 at 16:18

2 Answers2

2

Again, much better to avoid using null layouts and setBounds(...). While null layouts and setBounds() might seem to Swing newbies like the easiest and best way to create complex GUI's, the more Swing GUI'S you create the more serious difficulties you will run into when using them. They won't re-size your components when the GUI resizes, they are a royal witch to enhance or maintain, they fail completely when placed in scrollpanes, they look gawd-awful when viewed on all platforms or screen resolutions that are different from the original one.

For example, the following code creates this GUI:

Simple GUI

This uses a GridLayout to place a JPanel in the right hand side of another JPanel. If I wanted to add more components in different relative locations, it would be easy to do by simply nesting JPanels, each using its own layout.

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.*;

public class WorkWithLayouts extends JPanel {
    private static final long serialVersionUID = 1L;
    private static final int PREF_W = 400;
    private static final int PREF_H = 315;
    private static final String BG = "#ffffff";

    public WorkWithLayouts() {
        JPanel mostInner = new JPanel();
        mostInner.setForeground(Color.black);
        mostInner.setOpaque(false); // if you want the backing jpanel's background to show through

        // add title temporarily just to show where mostInner panel is
        mostInner.setBorder(BorderFactory.createTitledBorder("most inner")); // TODO: delete this

        JLabel jltxt = new JLabel();
        jltxt.setText("Test");
        mostInner.add(jltxt);

        setBackground(Color.decode(BG));
        setLayout(new GridLayout(1, 2));
        add(new JLabel()); // empty label
        add(mostInner);
    }

    @Override
    public Dimension getPreferredSize() {
        if (isPreferredSizeSet()) {
            return super.getPreferredSize();
        } else {
            return new Dimension(PREF_W, PREF_H);
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            createAndShowGui();
        });
    }

    private static void createAndShowGui() {
        WorkWithLayouts mainPanel = new WorkWithLayouts();
        JFrame frame = new JFrame("Work With Layouts");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.add(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
0

As already mentioned by Hovercraft Full Of Eels, you can help us to help you by adding a small program to your question that shows your problem (see https://stackoverflow.com/help/mcve for more information). Then people can try to reproduce and solve the issue.

When I created a small program myself, it seemed to work fine:

Screenshot of example without issues

import java.awt.*;
import javax.swing.*;

public class LabelNotVisible {
    public static void main(String[] arguments) {
        SwingUtilities.invokeLater(() -> new LabelNotVisible().createAndShowGui());
    }

    private void createAndShowGui() {
        JFrame frame = new JFrame("Stack Overflow");
        frame.setBounds(100, 100, 800, 200);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        JPanel jp = new JPanel();
        jp.setBackground(Color.decode("#ffffff"));
        //jp.setBounds(0, 35, 400, 315);

        JPanel mostInner = new JPanel();
        mostInner.setForeground(Color.black);
        //mostInner.setBounds(207, 5, 190, 240);
        jp.add(mostInner);

        JLabel jltxt = new JLabel();
        jltxt.setText("Test");

        mostInner.add(jltxt);

        frame.getContentPane().add(jp);
        frame.setVisible(true);
    }
}
Community
  • 1
  • 1
Freek de Bruijn
  • 3,552
  • 2
  • 22
  • 28
  • Thanks for your reply. I have to add bounds for my jltxt to show inside my JPane. I'll update my post with a better overview. – FareJump Dec 27 '15 at 17:08
  • @FareJump: no you don't have to add bounds. If you use layout managers in an intelligent way, the components will set their own proper bounds. And Freek -- please use answers to help the OP, not lead astray, including avoiding using setBounds and such, when there is no need. – Hovercraft Full Of Eels Dec 27 '15 at 17:11
  • @HovercraftFullOfEels - I agree with you that layout managers are the way to go. The calls to the `setBounds` method are only there because I copied the code from the OP to create a runnable example, in an - unsuccessful - attempt to reproduce the problem. Of course I'm trying to help the OP! – Freek de Bruijn Dec 27 '15 at 17:18
  • I use a layered layout for my Frame, but prefere to set my components with setbounds. – FareJump Dec 27 '15 at 17:30
  • 1
    Please expand the code in your question to a runnable example that shows the issue with the label. – Freek de Bruijn Dec 27 '15 at 17:32
  • 1
    @farejump Regarding `"... but prefere to set my components with setbounds."` Please understand that by doing this you're setting yourself up for a maintanence nightmare. Please check out these links to see why: [1](http://stackoverflow.com/q/9443990), [2](http://stackoverflow.com/q/33249871), [3](http://stackoverflow.com/q/13633503), [4](http://stackoverflow.com/q/17481559). Again, if you show an image of what you're trying to achieve, if you can show us small runnable code, we can likely show you a better way. – Hovercraft Full Of Eels Dec 27 '15 at 18:48
  • The `setBounds` calls will do nothing and are just adding noise to the example – MadProgrammer Dec 27 '15 at 21:03
  • @FareJump *"since it just worked"* - isn't always a solution – MadProgrammer Dec 27 '15 at 21:03
  • @FareJump - In this case it's probably better to delete your question, since it is unclear what the problem was, which makes it very unlikely that someone else can benefit from your question. – Freek de Bruijn Dec 27 '15 at 21:15