jLabel setVisible(true) Doesn't Work

C

clusardi2k

I apologize for the simple question, but how do the label setVisible properly.

(1) I dragged a jLabel to my form. I then set it like so:

my_jLabel.setVisible (false);

Later in the project, I set its visibility to true:

my_jLabel.setVisible (true);

But, the label is no where to be found. That's my problem.

(2)FYI: If I do the following after setting it to true, I do see the label:

JOptionPane.showMessageDialog(null,"Is the jLable visible");

(3) Instead of using the show method, doing the following after setting the label visibility to true did not help.

my_jLabel.repaint();
my_jLabel.validate();

(4) Replacing the showMessageDialog with a sleep did not help.

(5) Replacing the above setVisible in (1) with the following code did not help:

SwingUtilities.invokeLater(new Runnable()
{ //The EDT (Event Dispatch Thread)
public void run()
{
JLabel myLabel = new JLabel("Old Text");
my_jLabel.setVisible (true);
}
});

(6) Using google to search for an answer isn't helping.

Thanks,
 
M

markspace

SwingUtilities.invokeLater(new Runnable()
{ //The EDT (Event Dispatch Thread)
public void run()
{
JLabel myLabel = new JLabel("Old Text");
my_jLabel.setVisible (true);


The second to the last line is the problem. Your label has to be inside
another component (a container) to be visible. Changing a local
variable will never work. Even changing an instance field won't work
unless you've specially defined your own component somehow.

Most Swing components are also containers. However normally you use
JFrame and JPanel as your containers. Call the add method, or use the
GUI layout tool to just drag and drop components onto one.

This might help:

<http://docs.oracle.com/javase/tutorial/uiswing/components/panel.html>
 
C

clusardi2k

On Tuesday, August 7, 2012 3:33:29 PM UTC-4, markspace wrote:
Your label has to be inside another component (a container) to be visible.Changing a local variable will never work. Even changing an instance fieldwon't work unless you've specially defined your own component somehow. Most Swing components are also containers. However normally you use JFrame andJPanel as your containers. Call the add method, or use the GUI layout toolto just drag and drop components onto one.

Does anyone have a simple working project of this:

(1) It has a form with a JPanel dragged from the swing control palette,
(2) In the JPanel a Jlabel is dragged from the swing control palette.
(3) The jlabel is set to invisible at the start of the project.
(4) The project becomes visible when a button is pressed.
(5) The project becomes invisible when a button is pressed.

Thanks,
 
J

Jeff Higgins

On Tuesday, August 7, 2012 3:33:29 PM UTC-4, markspace wrote:
Your label has to be inside another component (a container) to be visible. Changing a local variable will never work. Even changing an instance field won't work unless you've specially defined your own component somehow. Most Swing components are also containers. However normally you use JFrame and JPanel as your containers. Call the add method, or use the GUI layout tool to just drag and drop components onto one.

Does anyone have a simple working project of this:

(1) It has a form with a JPanel dragged from the swing control palette,
(2) In the JPanel a Jlabel is dragged from the swing control palette.
(3) The jlabel is set to invisible at the start of the project.
(4) The project becomes visible when a button is pressed.
(5) The project becomes invisible when a button is pressed.

Thanks,

No. But here's a start. You'll need add the
appropriate controls and handlers and wrap it in a project.

import javax.swing.JFrame;
import javax.swing.JLabel;

public class Scratch {

private static void createAndShowGUI() {
JFrame frame = new JFrame("Scratch");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JLabel label = new JLabel("Scratch");
frame.getContentPane().add(label);
// label.setVisible(false);
frame.pack();
frame.setVisible(true);
}

public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
 
J

John B. Matthews

(3) Instead of using the show method, doing the following after
setting the label visibility to true did not help.

my_jLabel.repaint();
my_jLabel.validate();

Using validate() is appropriate if you add or remove components or
change the Container's layout, as shown here [1]. When required,
repaint() should be invoked _after_ validate(). CardLayout, shown
here [2], is often a better alternative.

[1] <http://stackoverflow.com/a/5751044/230513>
[2] <http://stackoverflow.com/a/5655843/230513>
 
J

Jeff Higgins

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

@SuppressWarnings("serial")
public class Scratch extends JPanel implements ActionListener {

private JButton vButton, iButton;
private JLabel label;

public Scratch() {

vButton = new JButton("Visible");
vButton.setMnemonic(KeyEvent.VK_D);
vButton.setToolTipText("Sets Label visible (true)");
vButton.setActionCommand("visible");
vButton.addActionListener(this);
vButton.setEnabled(false);

iButton = new JButton("Invisible");
iButton.setMnemonic(KeyEvent.VK_E);
iButton.setToolTipText("Sets Label visible (false)");
iButton.setActionCommand("invisible");
iButton.addActionListener(this);

label = new JLabel("Scratch");

add(vButton);
add(label);
add(iButton);
}

public void actionPerformed(ActionEvent e) {
if ("invisible".equals(e.getActionCommand())) {
label.setVisible(false);
vButton.setEnabled(true);
iButton.setEnabled(false);
} else {
label.setVisible(true);
vButton.setEnabled(false);
iButton.setEnabled(true);
}
}

private static void createAndShowGUI() {

JFrame frame = new JFrame("Scratch");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Scratch scratch = new Scratch();
frame.setContentPane(scratch);
frame.pack();
frame.setVisible(true);
}

public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
 
C

clusardi2k

Nice project thanks, but I have two questions:

(Q1) How can you either modify this code or create a different project to use controls that were dragged to the JFrame from the swing Palette. The code is not to create the buttons, JFrame, JPanel, or JLabel.

I.E.:In Design View suppose you have a JFrame, jPanel1, jButton1, jButton2,and jLabel1 already on the Frame. They were dragged to the form. Your current project did not create them. The buttons and label are in the jPanel. How would you make jLabel1 become invisible and invisible using two buttons.

(Q2) I noticed that int the below project the buttons move when one of the buttons is pressed. How can you stop that from happening.

On Tuesday, August 7, 2012 10:27:46 PM UTC-4, Jeff Higgins wrote:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

@SuppressWarnings("serial")
public class Scratch extends JPanel implements ActionListener {

private JButton vButton, iButton;
private JLabel label;

public Scratch() {

vButton = new JButton("Visible");
vButton.setMnemonic(KeyEvent.VK_D);
vButton.setToolTipText("Sets Label visible (true)");
vButton.setActionCommand("visible");
vButton.addActionListener(this);
vButton.setEnabled(false);

iButton = new JButton("Invisible");
iButton.setMnemonic(KeyEvent.VK_E);
iButton.setToolTipText("Sets Label visible (false)");
iButton.setActionCommand("invisible");
iButton.addActionListener(this);


label = new JLabel("Scratch");


add(vButton);
add(label);
add(iButton);
}

public void actionPerformed(ActionEvent e) {
if ("invisible".equals(e.getActionCommand())) {
label.setVisible(false);
vButton.setEnabled(true);
iButton.setEnabled(false);
} else {
label.setVisible(true);
vButton.setEnabled(false);
iButton.setEnabled(true);

}
}

private static void createAndShowGUI() {

JFrame frame = new JFrame("Scratch");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Scratch scratch = new Scratch();
frame.setContentPane(scratch);

frame.pack();
frame.setVisible(true);
}

public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
 
M

markspace

Nice project thanks, but I have two questions:

(Q1) How can you either modify this code or create a different
project to use controls that were dragged to the JFrame from the
swing Palette. The code is not to create the buttons, JFrame, JPanel,
or JLabel.

I.E.:In Design View suppose you have a JFrame, jPanel1, jButton1,
jButton2, and jLabel1 already on the Frame. They were dragged to the
form. Your current project did not create them. The buttons and label
are in the jPanel. How would you make jLabel1 become invisible and
invisible using two buttons.


What have you actually tried? Where's your code?

What in this tutorial doesn't work for you?

<http://netbeans.org/kb/docs/java/gui-functionality.html#Exercise_3>
 
J

Jeff Higgins

Nice project thanks, but I have two questions:

(Q1) How can you either modify this code or create a different project to use controls that were dragged to the JFrame from the swing Palette. The code is not to create the buttons, JFrame, JPanel, or JLabel.
I'm not certain what a swing Palette is. Probably a graphical GUI
builder of some sort. Two that I am aware of are associated with the
Netbeans and Eclipse IDEs. Questions regarding these tools are probably
better asked in their respective forums.

For Netbeans somewhere near here:
<http://forums.netbeans.org/>
For Eclipse near here:
I.E.:In Design View suppose you have a JFrame, jPanel1, jButton1, jButton2, and jLabel1 already on the Frame. They were dragged to the form. Your current project did not create them. The buttons and label are in the jPanel. How would you make jLabel1 become invisible and invisible using two buttons.

(Q2) I noticed that int the below project the buttons move when one of the buttons is pressed. How can you stop that from happening.
You will need to let your LayoutManager know your intentions. The
best I can offer is a pointer to the Swing tutorial on LayoutManagers.
<>
How your builder practices layout is better learned from
it's documentation and body of practitioners.

Since my last post I have come up with a possible use for
a disappearing label.
In the TextComponentDemo, located here:
<http://docs.oracle.com/javase/tutorial/uiswing/components/generaltext.html>

you might use the technique to enable/disable the status label
component. I'm not sure it's a great idea but, what the heck. I just
about have the code stripped down to demonstrate the idea and will post
back here when I am able. Unless of course someone in the meantime
writes in telling me that that is a real crappy idea, and comes up with
something better.
 
C

clusardi2k

What have you actually tried? Where's your code?
What in this tutorial doesn't work for you?

Thanks, it appears small projects work well! So, I have to investigate why setVisible is not working for me.

Basically, I'm going to move the below code down through my project to see where things stop working. I'll report back in a few minutes!

if ( jLabel1.isVisible() )
{
jLabel1.setVisible(false);
return;
}
else if ( 1 == 1 )
{
jLabel1.setVisible(true);
return;
}
 
C

clusardi2k

I'll report back in a few minutes!

It gave me confusing results. It was like the project ran too fast to ever display the label. I thought this was nonsense, so I created the below project.

The big question now becomes how do I (using the below project) get jLabel1 to be visible while the project is executing in the for loops?

Information about 3 of my button clicks:

Start run jLabel1 is visible :)

(1st button press)
jLabel1 disappears at end of method and never comes back up!

(2nd button press)
jLabel1 never is seen

(3rd button press)
jLabel1 never is seen

//Code:

package hide_show_label_with_icon;

import java.util.logging.Level;
import java.util.logging.Logger;

public class Hide_Show_Label_with_Icon extends javax.swing.JFrame {

public Hide_Show_Label_with_Icon() {
initComponents();
}

@SuppressWarnings("unchecked")
private void initComponents() {

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

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jButton1.setText("jButton1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

jLabel1.setText("jLabel1");

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(46, 46, 46)
.addComponent(jButton1)
.addGap(60, 60, 60)
.addComponent(jLabel1)
.addContainerGap(233, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(270, 270, 270)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jLabel1))
.addContainerGap(36, Short.MAX_VALUE))
);

pack();
}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
System.out.println("jLabel1.isVisible is " + jLabel1.isVisible());

jLabel1.setVisible(true);

for (int i = 0;i < 100000; i++)
for (int i2 = 0;i2 < 100000; i2++);
for (int i3 = 0;i3 < 100000; i3++);

System.out.println("jLabel1.isVisible is " + jLabel1.isVisible());
jLabel1.setVisible(false);
System.out.println("Done");
System.out.println();

}


public static void main(String args[]) {

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {
new Hide_Show_Label_with_Icon().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
// End of variables declaration
}
 
J

Jeff Higgins

Since my last post I have come up with a possible use for
a disappearing label.
In the TextComponentDemo, located here:
<http://docs.oracle.com/javase/tutorial/uiswing/components/generaltext.html>

you might use the technique to enable/disable the status label
component. I'm not sure it's a great idea but, what the heck. I just
about have the code stripped down to demonstrate the idea and will post
back here when I am able. Unless of course someone in the meantime
writes in telling me that that is a real crappy idea, and comes up with
something better.
Nope, turns out Container's add and remove methods work better here.
 
C

clusardi2k

So, how do you convert the below project to use SwingWorker?

Also, in general, how do you stop buttons and things from moving around when things go invisible and vice versa.

Thanks,

package hide_show_label_with_icon;

import java.util.logging.Level;
import java.util.logging.Logger;

public class Hide_Show_Label_with_Icon extends javax.swing.JFrame {

public Hide_Show_Label_with_Icon() {
initComponents();
}

@SuppressWarnings("unchecked")
private void initComponents() {

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

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jButton1.setText("jButton1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

jLabel1.setText("jLabel1");

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(46, 46, 46)
.addComponent(jButton1)
.addGap(60, 60, 60)
.addComponent(jLabel1)
.addContainerGap(233, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(270, 270, 270)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jLabel1))
.addContainerGap(36, Short.MAX_VALUE))
);

pack();
}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
System.out.println("jLabel1.isVisible is " + jLabel1.isVisible());

jLabel1.setVisible(true);

for (int i = 0;i < 100000; i++)
for (int i2 = 0;i2 < 100000; i2++);
for (int i3 = 0;i3 < 100000; i3++);

System.out.println("jLabel1.isVisible is " + jLabel1.isVisible());
jLabel1.setVisible(false);
System.out.println("Done");
System.out.println();

}


public static void main(String args[]) {

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {
new Hide_Show_Label_with_Icon().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
// End of variables declaration
}
 
M

markspace

So, how do you convert the below project to use SwingWorker?


Did you read the tutorial at the link I send you? Where's the code you
wrote after reading that?
 
J

Jeff Higgins

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;
import javax.swing.WindowConstants;

@SuppressWarnings("serial")
public final class Hide_Show_Label_with_Delay
extends JFrame {

private final JButton button;
private final JLabel label;
private final Timer timer;

private Hide_Show_Label_with_Delay() {

button = new JButton("Show");
label = new JLabel("Label");
timer = new Timer(3000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
button.setEnabled(true); }});

setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setResizable(false);

button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (label.isVisible()) {
label.setVisible(false);
button.setText("Show");
button.setEnabled(false);
timer.start();
} else {
label.setVisible(true);
button.setText("Hide");
button.setEnabled(false);
timer.start();
} }});

GroupLayout layout
= new GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
layout.setHonorsVisibility(label, Boolean.FALSE);

layout.setHorizontalGroup(
layout.createSequentialGroup()
.addComponent(button)
.addComponent(label));
layout.setVerticalGroup(
layout.createParallelGroup(
GroupLayout.Alignment.BASELINE)
.addComponent(button)
.addComponent(label));

pack();
label.setPreferredSize(label.getPreferredSize());
label.setVisible(false);
setVisible(true);
}

public static void main(String args[]) {
final Hide_Show_Label_with_Delay gui
= new Hide_Show_Label_with_Delay();
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
gui.setVisible(true); }});
}
}
 
J

Jeff Higgins

timer = new Timer(3000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
button.setEnabled(true); }});
+ timer.setRepeats(false);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,733
Latest member
LonaMonzon

Latest Threads

Top