FAQ Database Discussion Community


JLabel is disappearing

java,swing,jbutton,jlabel
I have been trying to look for the answer everywhere and I couldn't find a right solution so I am hoping that you guys will be able to help me. I have created a custom JButton just by overriding paintComponent(Graphics g) using drawImage. And then I created JLabel,which I wanted...

How can I extend JComboBox to allow user to remove list items by clicking an x button, like an Internet browser tab?

java,swing,jbutton,jlabel,jcombobox
I am trying to create an object that resembles a JComboBox, but I want the user to be able to remove an item from the list. Imagine that each list element has to its right an x icon or button like what you find on a tab in your internet...

Text in JLabel doesn't get updated on pressing a JButton

java,swing,jlabel
I am working on a project but the program seems to have a bug that I can not find. Here is an MCVE which reproduces the problem: import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JButton; import java.awt.FlowLayout; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class SO{ JLabel label; JButton button; JPanel panel;...

JButton not shown on screen

java,swing,jframe,jbutton,jlabel
Im trying to make an application that will change the state of a traffic light in the click of a button. My code: Main import javax.swing.*; public class PP416 { public static void main(String[] args) { JFrame frame = new JFrame("Traffic light"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(new TrafficPanel()); frame.pack(); frame.setVisible(true); } } JPanel...

Changing a label during ActionEvent

java,jframe,label,jlabel
I am trying to enable/disable a label when a button is pressed and i want to do this during the event and not after it. As you can see below, i try to enable/disable the two labels: lblKeyboard and lblGamepad. They end up running after the "RemoteControl.run();" is executed but...

How to disable and re-enable label

java,swing,jlabel
In the below code, I have a label named card with a mouse click event. I only want the click event to implement once. Meaning it will implement the first time I click the label, but not the following times. How do I do this? I imagine I must disable...

How to move the text to the next line in the JLabel without HTML?

java,html,swing,jlabel
In the JLabel to move the text to the next line I use HTML: JLabel label = new JLabel("<html><b>Line<br/Next line</b></html>"); Is there a way to move the text to the next line without using HTML? Thanks!...

Array of JLabel ImageIcon

java,arrays,jlabel,imageicon
I am attempting to create a game of Sudoku. I am wanting to use the JSwing API. So, I am using an array of JLabels to display the grid. I have a picture drawn of a 3x3 grid, and I would like to display that in a 3x3 grid. My...

Is there a way to set a JLabel's PreferredSize to the size of its components?

java,swing,jlabel
Is there any way to get the (preferred I guess) size of the components of a JLabel so I can resize it to them? My JLabel gets a size of 0,0 unless I set its Preferred Size, but setting its size to an arbitrary pixel value seems like the wrong...

Java actionPerformed does not add the changed label until the GUI is resized

java,swing,label,actionlistener,jlabel
I am a high school student trying to learn java and any help or advice is appreciated. The code does not set the text after I press the button. The actionPerformed does not do what it is supposed to until I change the size of the GUI for some reason....

Image does not appear

java,swing,oop,jlabel
Im trying to use JLabel to insert a image into my GUI. However it does not appear Here is a partial of my code public FirstAid() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 700, 507); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(new BorderLayout(0, 0)); tabbedPane = new JTabbedPane(JTabbedPane.TOP); contentPane.add(tabbedPane,...

repaint method is doing nothing once called

java,swing,jlabel,java-2d
I am trying to understand how repaint and paintComponents work in Java Swing, and wondering why this program only display "hello" when it's executed. class MyLabel extends JLabel{ private static final long serialVersionUID = 1L; public MyLabel(){ System.out.println("hello"); repaint(); } @Override public void paintComponents(Graphics g) { super.paintComponents(g); System.out.println("painting"); } }...

align left and right two JLabels in a “North” BorderLayout

java,alignment,jlabel
I am using BorderLayout for my application. setLayout(new BorderLayout()); I need to align two JLabels in left and right in the "NORTH" of the JPanel. Here is my code: JPanel top = new JPanel(); top.add(topTxtLabel); top.add(logoutTxtLabel); add(BorderLayout.PAGE_START, top); So I need topTxtLabel in left and logoutTxtLabel in right. I tried...

How to prescribe the width and the height of a JLabel?

java,swing,menu,jlabel
I am making a game, and i'm in the menu, however, I have to put one button. And, to make this, I created a JLabel with one image, and I used the MouseListener, but I have to prescribe the proportion of this JLabel, how can I do this? Thanks for...

refresh JFrame Java without doing new JFrame

java,jframe,jlabel
I'm working on a game where you can upgrade your stats in a shop. When you buy something it should refresh the JLabel which shows your Coins Is there a way to do that without doing a new JFrame? public void actionPerformed(ActionEvent e) { user.setCoin(user.getCoin() - 5); user.setMaxJump(5); EpixController.getInstance().coinsUpdate(user, -5);...

Putting text from JTextField to JLabel

java,swing,jlabel,jtextfield
I 'm working on code that takes a string from a JTextField and puts it on a JLabel. But my problem is always the string shown is missing the last character. private void jTextField1KeyTyped(java.awt.event.KeyEvent evt) { String ch=jTextField1.getText(); jLabel1.setText(ch); } ...

Java GridBagLayout JPanels automatically resizing issue?

java,swing,jpanel,jlabel,gridbaglayout
I need a help with a resizing of Red coloured JPanel. Basically the problem is that I set the grid bag constraints for RED and BLUE JPanel same size and it works perfect, until i add the JLabel, then it just stupidly starts making the RED JPanel Larger size, but...

How to get text from JTextField to JLabel that is in another JFrame?

java,swing,oop,jlabel,jtextfield
I am making an application in Netbeans and I would like to get the text from the jTextField and set it to a jLabel that is in another jFrame (not the same!) I did this: jLabel.setText(jTextField1.getText()); But it doesn't work. And what event method should I use? actionPerformed?...

Removing an Image off a JFrame

java,swing,jframe,jlabel,imageicon
I have been trying to make this program display an image and then replace and then after a second it will remove the image and replace it with a different one after one second. Which image to display is controlled by a random int i. Everything works except the images...

HTML text in JLabel ignores alignment with text-align: center

java,html,swing,alignment,jlabel
I need to to have text formatted in HTML. The text was therefore enclosed in HTML tags with proper color and alignment applied. The color settings works, but not the alignment set by 'text-align: center;'. Looking to center align HTML text in the 'botlabel'. Cleaning and rebuilding the project did...

My JLabel and GUI code in a paintcomponent method do not draw at the same time

java,swing,jlabel,paintcomponent
When I try to draw a JLabel or my GUI, whatever I add to my JFrame last is drawn and the rest is just never drawn or painted over. I would appreciate if you could help me find a solution to draw the JLabel at a specific place along with...

How to extract associated icon from a File

java,image,file,extract,jlabel
I have an array of files - File[] files = new File[9] The files extensions are ".lnk" (Windows shortcut files) and I'm wondering how to extract the Image (The actual executable icon that you see in windows explorer...) from each file to set it to a JLabel ? Can anyone...

Java resize an image

java,swing,jlabel,imageicon
I would like to display an image in an area in my Jframe but the image takes up much space. I would like to take it fair precise dimensions. How can I do this in Java This is my simple code : I am open to any proposal if I...

How do I create a JLabel from variables? (ie both named and populated by variables)

java,variables,jlabel
Is there a way to create a JLabel from a variable? For a simple example, I'll use a survey creator, where the first user creates an array of questions for the next user to answer with 1-5 ratings. If I want the first user to be able to enter the...

How to print setText() on multiple lines of a JLabel?

java,swing,jlabel
When I print out the raw text from a website, it will only put one line of text in the JLabel, but in the console it will do multiple lines each on their own line. import java.awt.BorderLayout; import java.awt.EventQueue; import java.io.IOException; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import javax.swing.JLabel; import...

Changing label text using ActionListener

java,jbutton,actionlistener,jlabel,settext
I'm trying to make a program that allows me to change the text of a label when clicking different buttons, however regardless of what button I click the text of the label changes to the last line of actionPerformed. import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Gui extends JFrame{...

ScrollPane not showing on JLabel

java,swing,jscrollpane,jlabel
When I run this program, I don't see a scrollbar on the Label. 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(); } public Util1() { EventQueue.invokeLater(new Runnable() { public void run() { JFrame frame = new...

netbeans: Attach image in Jlabel using Jbutton

java,image,swing,netbeans,jlabel
How to create a personal detail which I need to attach to a personal image? When clicking the button, it can select the picture, but I still cannot figure it out how to set this coding, any one can help solve this? JFileChooser chooser=new JFileChooser(); chooser.showOpenDialog(null); File f=chooser.getSelectedFile(); personal_image. ...

How to Position a JLabel in the specified Layout?

java,swing,jbutton,jlabel,layout-manager
I am trying to position my Labels and Button on screen. I found an answer here to set label's Position using: label3.setHorizontalAlignment(SwingConstants.CENTER); label3.setVerticalAlignment(SwingConstants.CENTER); but there has been no effect in my code. Also, on changing my layout from Flow Layout to Border Layout, only one Jbutton [CLEAR] appears on the...

Don't read '<' with XML-file in JLabel

java,html,xml,swing,jlabel
I have a XML-file that contains the symbol '&lt (<)': <?xml version="1.0" encoding="UTF-8" standalone="no"?> <parent> <child>&lt;100</child> </parent> I read this value using the method: public String getValue(String parentTag, int parentIndex) { Node root = m_doc.getElementsByTagName(parentTag).item(parentIndex); return root.getTextContent(); } And displays it in a JLabel with HTML: JLabel label = new...

Can you make an editable label?

java,swing,jlabel
Quick question, can you make a textfield, that looks like a JLabel, but that can still be edited? You could still change its size and its font, but the background + the border should be gone. As of now, I'm trying to do this with Swing. What you would still...

How to change multiple jLabel's text Dynamically in a single for loop?

java,swing,jlabel
Is it possible ? I have 5 Labels on jFrame as jLabel1, jLabel2, jLabel3, jLabel4 and jLabel5. I want to set text as null other than a single one which is input given by user. For experiment, I am using 5 labels but there are more than 150 labels in...

Update a Label with a Swing Timer

java,swing,timer,jlabel
I'm having some trouble with this piece of code. I'm starting a timer with a random number, and I want to update a JLabel with the countdown, every second. But I haven't figured out how to do so, since the only listener the timer triggers is at the end of...

Illegal Number Format Exception in Java

jlabel,numberformatexception
I have this code, which pulls in a double value from an external method call, converts it into a String and then displays it in a JLabel. My problem is that sometimes the value might display: 1.95000000000000001, and I want to reduce that to 1.95. When I run the application,...

Changing a JLabel while GUI is running

java,user-interface,jframe,jlabel
The code as I have it now does work in changing the JLabel, but the issue is that after the first change, it only adds onto the label, rather than changing it like I intended. The code is as follows. public static void changeJLabel(JPanel panelName, JLabel JLabel, String newText) {...

How can I update the text of a JButton when I click on it?

java,swing,refresh,jbutton,jlabel
I assume there is a better way than removing every element from the JPanel and remaking them, because that's the workaround I had but now obviously when the button is clicked it's told to remove itself so the action never completes. Here's my awful code for adding the button action...

Setting text in JLabel in a loop

java,swing,user-interface,jlabel
start = System.currentTimeMillis(); FileReader filereader1 = new FileReader("virus_database/point.dat"); JTextField jtextfield1 = new JTextField(); jtextfield1.read(filereader1, null); point = Integer.parseInt(jtextfield1.getText()); filereader1.close(); scanFileName.setText(""); scanFileName.setText("Please wait. Loading Database..."); for(int i = 0; i < point && cmd != 1; i++) { FileReader filereader4 = new FileReader((new StringBuilder()).append("virus_database/malware").append(i).append(".res").toString()); JTextField...

image doesnt shows on JLabel when click on next button, using java Swing

java,swing,jlabel,imageicon
I am trying to make small program that shows pic on JLabel when user click on Next Button.The Problem is when i click on next button it shows nothing.but if i resize frame it shows all pics from directory. instead of one picture at a time. Please excuse my English....

JLabel absorbs background from my mouse pointer coordinates JAVA

java,swing,animation,jlabel,transparent
The code is above. This is an class with score etc. on the left on the window. Each time i move an cursor it shows different "background"under timer i don't know why. This Timer Label is in JPanel no the left which is in some part transparent .How to get...

Changing a JLabel's position

java,swing,jlabel,null-layout-manager
After a lot of research I only could get that board image with using label. Now I cannot change it's position. I tried a lot of functions. What is the exact function do I need public class Board extends JFrame { private ImageIcon image; private JLabel label; public Board() {...

Swing - revalidate of UI not happening [duplicate]

java,swing,jlabel,repaint,event-dispatch-thread
This question already has an answer here: Update JLabel repeatedly with results of long running task 4 answers I have a JButton and a JLabel. The code is written so that when the user clicks on the button, the action performed changes the text of the JLabel and starts...

Make JLabel background transparent in a frame with background [closed]

java,swing,graphics,transparency,jlabel
I have a JLabel on my JFrame and a JFrame with a picture background. The problem is although my JLabel is opaque but it still has a grey background which is annoying. Here is my code: import java.awt.Graphics; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Image; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPasswordField;...

JLabel not showing in JFrame

java,swing,jframe,jlabel
I have searched this problem in stackoverflow,however, i couldn't resolve my problem. So I have a simple image that I assign to a JLabel and add that label in to JFrame but it doesn't appear. Any help? PS: If the image that I set is not same with the screen...

How to display an image on another image?

java,jframe,jpanel,jlabel,game-engine
I try to create a little game in java but I'm in trouble. When I draw a map, I'm not able to display the characters without overwrite the titlesets of the squares. My goal is to be able to display many pictures on the same square (like the titleset of...

How do i add a mouseListener to a anonymous JLabel object?

java,swing,awt,jlabel,mouselistener
Currently I'm using a for-loop to populate the JPanel with numbers from 1-31 So basically, What I want to do is if let say i click on number 1, it will do show System.out.println(1); Here's the code: public class MonthPanel extends JPanel implements MouseListener { public MonthPanel() { setLayout(new GridLayout(6,7));...

How to make a class extending a jLabel so that it appears as a JLabel in my main class?

java,class,jlabel,extends
So, essentially I'm making memory. I have a tile class, which at this point will only have a number, and a boolean (to show if it is flipped or not). I made duplicates of all the numbers as Tile objects and randomized them, and in turn added them to my...

JLabel - Cutting Off The Top of Image

java,swing,resize,icons,jlabel
in my new Java project I have a JFrame in which there is a JLabel set to North with BorderLayout, and below it is an image. The image fits fine on the JFrame, but the JLabel cuts off the top of it. How can I resize this JLabel? I tried...

VERY basic Slot Machine (checking to see if three labels are equal) Java

java,if-statement,radio-button,jlabel
Here is a run down of the program: Three JLables are made. Three buttons are made corresponding to the labels. When a button is pressed it calls a random generator that picks a number between 0-9. Depending on what button is pressed the corresponding JLabel is changed to the number...

How to I actively set JLabels from an ArrayList to the JPanel?

java,arraylist,jlabel
I am trying to make a Frogger game. This is a simple JPanel with JLabels on it. public class PlayField extends JPanel implements KeyListener{ ArrayList<JLabel> rows = new ArrayList<JLabel>(); Random g = new Random(); public PlayField(int length){ //length is the number of rows generateField(length); for(JLabel label : rows){ add(label); //here...

How to refresh a JFrame?

java,jframe,jlabel
So in my program I set in the beginning a JLabel to it so now an image is on it. Now I need to remove that JLabel and put a new one instead of it. So I did: frame.remove(test_loadingJL); frame.add(test_lockScreenJL); But when I look, it is still the first image...

Java project working on Eclipse but not working when exported to runnable jar file

java,eclipse,jar,jlabel
i am having a issue with exporting my java file to a runnable jar file. In the eclipse it works fine, but when i exported it, it doesn't work. I already tried the (java -jar MyJar.jar) to get the log but it says "Unnable to access the jar file" I...

Swing Chat HTML formatted messages

java,swing,user-interface,jscrollpane,jlabel
I'm trying to make a chat client to swing. And I need that message in the history of correspondence were formatted in HTML. The problem I tried to solve with JTextPane, as it supports HTML formatting. When I did, just text display, in principle everything was normal. But when I...

Splitting an Image Object into a 2D Array in Java

java,image,jlabel,multidimensional-array,bufferedimage
The aim of this little project is to break down an image (in this case a flag) into pieces like a jigsaw and store each piece in part of a 2D array. I then want to be able to view each piece individually so that I know it has worked....

unable to create new JLabel with html in method

java,swing,jlabel
I am trying to make a fix-width label in Java, which I find a solution here. But I fail whenever I put any inside a label -- while the label is create inside a method. my code is here : public class testingGui JFrame myframe = new JFrame("Some title here");...

Add new label and textfield on button click Java

java,swing,jbutton,actionlistener,jlabel
I have this code with three buttons. I want to add a label and a textfield after a buttonclick. So for the 'button' I want to add the 'label' and 'textfield. For 'button2' I want to add 'label2' and 'textfield2'. And so on. I already have this code, but it...