2

I've created an app but I have a problem with my JLabels not showing up properly. The app currently looks like this:

app screenshot

These are 2 JPanels inside a JFrame created with the following code:

public JFrame window = new JFrame();
public JPanel top = new JPanel();
public static JPanel main = new JPanel();
public JPanel login = new JPanel();

// ...

Inside the main class:

window.setSize(1000, 700);
login.setSize(250, 200);
//   main.setSize(500,500);
main.setLocation(500,100);

window.add(login);
window.add(main);

login.add(new view.LoginPanel());

main.setLayout(new BorderLayout());
main.add(new view.CategoryList(), BorderLayout.CENTER);
main.validate();
main.repaint();

window.validate();
window.setVisible(true);

That will show up the frames as I have them now. But above the black line there should be a title that would be created by this:

public class CategoryList extends JPanel implements MouseListener {
    public CategoryList() {
        super();
        setLayout(null);
        initComponents();
        revalidate();
        repaint();
        addTitle();
    }
}

The title is created like this:

private void addTitle() {
    JLabel lblTitle = new JLabel();
    lblTitle.setText("Winkelapplicatie");
    lblTitle.setBounds(20, 20, 150, 20);
    lblTitle.setFont(WinkelApplication.FONT_16_BOLD);
    this.add(lblTitle);
    System.out.println("addTitle");
}

But it does not show up. I know there are a lot of methods not shown in this code, but I have included what I think is all the necessary code.

I hope someone can help me, thanks in advance!

Edit:

I've stripped my code so it can be online: main package: WinkelApplication.java:

package main;


import java.awt.BorderLayout;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;



public final class WinkelApplication {

public static final String NAME = "hi";
public static final String CURRENCY = "\u20AC";
public JFrame window = new JFrame();
public JPanel top = new JPanel();
public static JPanel main = new JPanel();
public JPanel login = new JPanel();

public static final Font FONT_10_PLAIN = new Font("Verdana", Font.PLAIN, 10);
public static final Font FONT_10_BOLD = new Font("Verdana", Font.BOLD, 10);
public static final Font FONT_12_BOLD = new Font("Verdana", Font.BOLD, 12);
public static final Font FONT_16_BOLD = new Font("Verdana", Font.BOLD, 16);

private static WinkelApplication instance = new WinkelApplication();

private WinkelApplication() {
}

public void initialize() {
    try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception e) {
        System.err.println("Error setting LookAndFeelClassName: " + e);
    }

}

public void startup() {
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setSize(1000, 700);
    login.setSize(250, 200);
     main.setLocation(500,100);
    window.add(login);
    window.add(main);
    login.add(new main.LoginPanel());  
     main.setLayout(new BorderLayout());
    main.add(new main.CategoryList(), BorderLayout.CENTER);
    main.validate();
    main.repaint();
    window.validate();
    window.setVisible(true);


}

public static WinkelApplication getInstance() {
    return instance;
}

public static void main(String args[]) {
    final WinkelApplication applicatie = WinkelApplication.getInstance();
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            try {
                applicatie.initialize();
                applicatie.startup();
            } catch (Exception e) {
                System.out.println("Application" + applicatie.getClass().getName() + "failed to launch");
            }
        }
    });
}
}

LoginPanel class: (Just a textfield created with the GUI Designer in Netbeans)

  /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package main;

import main.WinkelApplication;

public class LoginPanel extends javax.swing.JPanel {

/**
 * Creates new form LoginPanel
 */
public LoginPanel() {
    initComponents();
}

private Boolean loggedIn = null;

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    javax.swing.JLabel jLabel1 = new javax.swing.JLabel();

    jLabel1.setText("TextField in JPanel 1");

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(114, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jLabel1)
            .addContainerGap(98, Short.MAX_VALUE))
    );
}// </editor-fold>                        

public Boolean isLoggedIn(){
    return loggedIn;
}
// Variables declaration - do not modify                     
// End of variables declaration                   
}

CategoryList class (Here the text should be above the line, but that does not show up)

package main;

import main.WinkelApplication;
import java.awt.Graphics;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class CategoryList extends JPanel {

public CategoryList() {
    super();
    setLayout(null);
    initComponents();
}

/** create the gui for this page */
private void initComponents() {
    addTitle();
}

/** add the page's title */
private void addTitle() {
    JLabel lblTitle = new JLabel();
    lblTitle.setText("Winkelapplicatie");
    lblTitle.setBounds(20, 20, 150, 20);
    lblTitle.setFont(WinkelApplication.FONT_16_BOLD);
    this.add(lblTitle);
    System.out.println("addTitle");
}

@Override
public void paint(Graphics graphics) {
    super.paint(graphics);
    graphics.drawLine(20, 45, 540, 45);
}
}

Hope somebody can help me now. Thanks in advance again.

12
  • 2
    Your code is kind of fragmented and incomplete making it hard for us to fully understand. We don't want to see all of your code, just that which shows your problem, is complete enough to compile and run, and has no code unrelated to the problem. Best to create and post an sscce that we can run and which shows your problem. Also, get out of the habit of using null layout. You almost never want to use that. Dec 8, 2012 at 13:51
  • @Hovercraft Full Of Eels Okay, thanks for answering that. I got the code and I have to make it better and bigger, I uploaded the source (just the parts anybody would need) thats here: link. So you can try it and see the code, to paste it here that would be a lot of classess to do and to read... That's why I uploaded it. I hope anybody can help me.
    – Marc
    Dec 8, 2012 at 14:07
  • 1
    Marc: no one wants to upload the source. Please re-read my post and check the link in my comment above. It will tell you exactly what we need. Understand that this will require effort on your part. So if you don't get a decent answer soon, consider putting in this effort as it can be well worth it. Luck. Dec 8, 2012 at 14:13
  • 3
    Actually it is painted but seems to be hidden by the login panel. Remove window.add(login); to verify. You really should consider to not use null layout - as your issue shows it is more complicated to handle than any reasonable layout manager.
    – Howard
    Dec 8, 2012 at 15:23
  • 1
    I also looked up what setLayout(null) does, that is that I don't have to set every location of labels and so on, but that's already done by somebody else on which code I have to continue, so I have to use it.
    – Marc
    Dec 8, 2012 at 20:08

2 Answers 2

2

A possible simple solution:

Your main application JPanel should use a BorderLayout. Place the title JLabel in the BorderLayout.PAGE_STATE (also known as BorderLayout.NORTH), and place the rest of your GUI in the BorderLayout.CENTER position.

3
  • That's just the problem, I got that code already and I have to use it as a JPanel, I have to show it in a JPanel and show multiple JPanels on the screen... I hope there is a solution...
    – Marc
    Dec 8, 2012 at 20:02
  • @Marc: Then place the JPanel using layouts. JLabels, JPanels, etc all descend from JComponent and can be placed into any container almost interchangeably. There is a solution and really it is you studying the layout manager tutorial and using the information it contains. Start here: Laying out components in containers Dec 8, 2012 at 22:24
  • I got it, I submitted it here as a answer but I can only set it as good after 24 hours... Thanks anyway! :)
    – Marc
    Dec 9, 2012 at 14:59
1

I fixed this by setting the main layout also to setLayout(null), I just have to use this for this project...

Next time when I have to write it from scratch I won't use it! Anyway, thanks for helping, I would have never found the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.