JLabel not showing


Yacopo

I am trying to add line breaks in JLabel. '\n' doesn't work, I found that using HTML should be a solution, but whenever I try to do anything with HTML it causes the JFrame to disappear or not be visible anymore. I am using NetBeans 8.2 and Java JDK 1.8.0_251. Is there any solution? thanks

 WindowMain = new JFrame("Spasitel");
   WindowMain.setSize(960, 720);
   WindowMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   WindowMain.getContentPane().setBackground(Color.BLACK);
   //WindowMain.getContentPane().setFont(Courier New);
   WindowMain.setLayout(null);
   WindowMain.setResizable(false);
   WindowMain.setVisible(true);
   Toolkit toolkit = Toolkit.getDefaultToolkit();
   Dimension size = toolkit.getScreenSize();
   WindowMain.setLocation(size.width/2 - WindowMain.getWidth()/2, size.height/2 - WindowMain.getHeight()/2);

   JPanel TitleFrame = new JPanel();
   TitleFrame.setBounds(50,50, 860, 200);
   TitleFrame.setBackground(Color.WHITE);
   JLabel TitleText = new JLabel ("<html>Hello World!<br/>blahblahblah</html>", SwingConstants.CENTER);
   //TitleText.setFont("Courier New", Font.PLAIN, 18);
   TitleFrame.add(TitleText);
   TitleFrame.setForeground(Color.GREEN);
   TitleFrame.show();
   TitleFrame.setVisible(true);
   WindowMain.add(TitleFrame);

Use HTML

no HTML

Gilbert LeBlanc

I have created the following GUI.

JLabel example

I used JFrame, a JPaneland a JLabel. I call the method that SwingUtilities invokeLaterstarts the Swing GUI on the event dispatch thread ,

I used the Swing layout manager.

Here is the complete runnable code.

import java.awt.BorderLayout;
import java.awt.FlowLayout;

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

public class JLabelExample implements Runnable {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new JLabelExample());
    }

    @Override
    public void run() {
        JFrame frame = new JFrame("JLabel Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        frame.add(createMainPanel(), BorderLayout.CENTER);
        
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }
    
    private JPanel createMainPanel() {
        JPanel panel = new JPanel(new FlowLayout());
        panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        
        String s = "<html>This is a test of the emergency broadcast system.  ";
        s += "<br />This is only a test.";
        
        JLabel label = new JLabel(s);
        panel.add(label);
        
        return panel;
    }

}

Related


HTML in JLabel not showing

Matt: JLabel label = new JLabel("<html><body>Hello world</body></html>"); Nothing is displayed. If I get rid of the label, it shows plain text (as expected), so definitely the JLabel is added and displayed in the window. Same as: JEditorPane jep = new JEditor

Countdown timer, error showing seconds in JLabel

Connector: I'm having trouble displaying seconds to a label. Whenever I run the code, it should be "9(minutes):59(seconds)", but it is not going as planned, instead it only shows 9 minutes. Note: I'm just using a label to display the minute seconds timer. Here

Java Swing ImageIcon not showing in JLabel

gthanop: I found that the same error is reproduced every time I execute the following MRE : import java.awt.Component; import java.awt.Graphics; import java.awt.GraphicsConfiguration; import java.awt.GraphicsDevice; import java.awt.GraphicsEnvironment; import

Java Swing: JLabel text not showing on screen

sn24 I'm trying to get the name from the input Scannerand by displaying that name on the screen JLabel. However, it did not appear. The button I made appears but the label doesn't. Am I missing something? import javax.swing.*; import java.awt.*; import java.u

JLabel not showing in JFrame

because of Caracas I've searched stackoverflow for this issue, but I can't figure it out. So I have a simple image assigned to a JLabel and add that label to the JFrame, but it doesn't appear. Any help? PS: If I set the image to a different size than the scree

HTML in JLabel not showing

Matt: JLabel label = new JLabel("<html><body>Hello world</body></html>"); Nothing is displayed. If I get rid of the label, it shows plain text (as expected), so definitely the JLabel is added and displayed in the window. Same as: JEditorPane jep = new JEditor

Countdown timer, error showing seconds in JLabel

Connector: I'm having trouble displaying seconds to a label. Whenever I run the code, it should be "9(minutes):59(seconds)", but it is not going as planned, instead it only shows 9 minutes. Note: I'm just using a label to display the minute seconds timer. Here

Java Swing ImageIcon not showing in JLabel

gthanop: I found that the same error is reproduced every time I execute the following MRE : import java.awt.Component; import java.awt.Graphics; import java.awt.GraphicsConfiguration; import java.awt.GraphicsDevice; import java.awt.GraphicsEnvironment; import

Countdown timer, error showing seconds in JLabel

Connector: I'm having trouble displaying seconds to a label. Whenever I run the code, it should be "9(minutes):59(seconds)", but it is not going as planned, instead it only shows 9 minutes. Note: I'm just using a label to display the minute seconds timer. Here

Java Swing ImageIcon not showing in JLabel

gthanop: I found that the same error is reproduced every time I execute the following MRE : import java.awt.Component; import java.awt.Graphics; import java.awt.GraphicsConfiguration; import java.awt.GraphicsDevice; import java.awt.GraphicsEnvironment; import

Countdown timer, error showing seconds in JLabel

Connector: I'm having trouble displaying seconds to a label. Whenever I run the code, it should be "9(minutes):59(seconds)", but it is not going as planned, instead it only shows 9 minutes. Note: I'm just using a label to display the minute seconds timer. Here

Java Swing ImageIcon not showing in JLabel

gthanop: I found that the same error is reproduced every time I execute the following MRE : import java.awt.Component; import java.awt.Graphics; import java.awt.GraphicsConfiguration; import java.awt.GraphicsDevice; import java.awt.GraphicsEnvironment; import

Image not showing on JLabel

Vito I was commissioned to make a Java replica of Candy Crush Saga. Right now, I'm completely stuck on the GUI part. I've decided that each candy will be represented by a JLabel with a candy icon with a mouse listener to control its functionality. What happens

JLabel not showing in JFrame

because of Caracas I've searched stackoverflow for this issue, but I can't figure it out. So I have a simple image assigned to a JLabel and add that label to the JFrame, but it doesn't appear. Any help? PS: If I set the image to a different size than the scree

Image not showing on JLabel

Vito I was commissioned to make a Java replica of Candy Crush Saga. Right now, I'm completely stuck on the GUI part. I've decided that each candy will be represented by a JLabel with a candy icon with a mouse listener to control its functionality. What happens

JLabel not showing

Yacopo I am trying to add line breaks in JLabel. '\n' doesn't work, I found that using HTML should be a solution, but whenever I try to do anything with HTML it causes the JFrame to disappear or not be visible anymore. I am using NetBeans 8.2 and Java JDK 1.8.

JLabel array in Jpanel not showing

Stephen So I'm pulling my hair out over this and I can't seem to figure out why these JLabels aren't drawing to the window. I've been looking for another project I've done in the past that also used a JLabel in a JPanel and everything seemed to be correct. Any

ScrollPane not showing on JLabel

username When running the program, I can't see the scroll bar on the "tabs". What am I missing? import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Util1 { public static void main(String[] args) { new Util1(); }

JLabel not showing in JFrame

because of Caracas I've searched stackoverflow for this issue, but I can't figure it out. So I have a simple image assigned to a JLabel and add that label to the JFrame, but it doesn't appear. Any help? PS: If I set the image to a different size than the scree

Nothing is showing in my JLabel popup

Aaron Cloud I'm trying to create a simple script that allows images and text to pop up. The window is popping up, but it's blank with nothing on it. Does anyone know what I am doing wrong? import javax.swing.JFrame; import javax.swing.JLabel; import javax.swin

Java Swing ImageIcon not showing in JLabel

gthanop: I found that the same error is reproduced every time I execute the following MRE : import java.awt.Component; import java.awt.Graphics; import java.awt.GraphicsConfiguration; import java.awt.GraphicsDevice; import java.awt.GraphicsEnvironment; import

Java Swing ImageIcon not showing in JLabel

gthanop: I found that the same error is reproduced every time I execute the following MRE : import java.awt.Component; import java.awt.Graphics; import java.awt.GraphicsConfiguration; import java.awt.GraphicsDevice; import java.awt.GraphicsEnvironment; import

HTML in JLabel not showing

Matt: JLabel label = new JLabel("<html><body>Hello world</body></html>"); Nothing is displayed. If I get rid of the label, it shows plain text (as expected), so definitely the JLabel is added and displayed in the window. Same as: JEditorPane jep = new JEditor

Image not showing on JLabel

Vito I was commissioned to make a Java replica of Candy Crush Saga. Right now, I'm completely stuck on the GUI part. I've decided that each candy will be represented by a JLabel with a candy icon with a mouse listener to control its functionality. What happens

JLabel array in Jpanel not showing

Stephen So I'm pulling my hair out over this and I can't seem to figure out why these JLabels aren't drawing to the window. I've been looking for another project I've done in the past that also used a JLabel in a JPanel and everything seemed to be correct. Any

JLabel not showing in JFrame

because of Caracas I've searched stackoverflow for this issue, but I can't figure it out. So I have a simple image assigned to a JLabel and add that label to the JFrame, but it doesn't appear. Any help? PS: If I set the image to a different size than the scree

ScrollPane not showing on JLabel

username When running the program, I can't see the scroll bar on the "tabs". What am I missing? import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Util1 { public static void main(String[] args) { new Util1(); }

JLabel not showing

Yacopo I am trying to add line breaks in JLabel. '\n' doesn't work, I found that using HTML should be a solution, but whenever I try to do anything with HTML it causes the JFrame to disappear or no longer be visible. I am using NetBeans 8.2 and Java JDK 1.8.0_

JLabel not showing

Yacopo I am trying to add line breaks in JLabel. '\n' doesn't work, I found that using HTML should be a solution, but whenever I try to do anything with HTML it causes the JFrame to disappear or no longer be visible. I am using NetBeans 8.2 and Java JDK 1.8.0_