Note that there are some explanatory texts on larger screens.

plurals
  1. POJLabels (And other things) wont show up inside a JPanel in a JFrame (multiple JFrames)
    text
    copied!<p>I've created an app but I have a problem with my JLabels not showing up properly. The app currently looks like this:</p> <p><img src="https://i.stack.imgur.com/HFnuC.png" alt="app screenshot"></p> <p>These are 2 JPanels inside a JFrame created with the following code:</p> <pre><code>public JFrame window = new JFrame(); public JPanel top = new JPanel(); public static JPanel main = new JPanel(); public JPanel login = new JPanel(); // ... </code></pre> <p>Inside the main class:</p> <pre><code>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); </code></pre> <p>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:</p> <pre><code>public class CategoryList extends JPanel implements MouseListener { public CategoryList() { super(); setLayout(null); initComponents(); revalidate(); repaint(); addTitle(); } } </code></pre> <p>The title is created like this:</p> <pre><code>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"); } </code></pre> <p>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.</p> <p>I hope someone can help me, thanks in advance!</p> <p>Edit:</p> <p>I've stripped my code so it can be online: main package: WinkelApplication.java:</p> <pre><code>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"); } } }); } } </code></pre> <p>LoginPanel class: (Just a textfield created with the GUI Designer in Netbeans)</p> <pre><code> /* * 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") // &lt;editor-fold defaultstate="collapsed" desc="Generated Code"&gt; 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)) ); }// &lt;/editor-fold&gt; public Boolean isLoggedIn(){ return loggedIn; } // Variables declaration - do not modify // End of variables declaration } </code></pre> <p>CategoryList class (Here the text should be above the line, but that does not show up)</p> <pre><code>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); } } </code></pre> <p>Hope somebody can help me now. Thanks in advance again.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload