jLabel setVisible(true) Doesn't Work

C

clusardi2k

From: (e-mail address removed)

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,

--- BBBS/Li6 v4.10 Dada-1
* Origin: Prism bbs (1:261/38)
--- Synchronet 3.16a-Win32 NewsLink 1.98
Time Warp of the Future BBS - telnet://time.synchro.net:24
 
M

markspace

To: clusardi2k
From: 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>

--- BBBS/Li6 v4.10 Dada-1
* Origin: Prism bbs (1:261/38)
--- Synchronet 3.16a-Win32 NewsLink 1.98
Time Warp of the Future BBS - telnet://time.synchro.net:24
 
C

clusardi2k

To: markspace
From: (e-mail address removed)

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,

--- BBBS/Li6 v4.10 Dada-1
* Origin: Prism bbs (1:261/38)
--- Synchronet 3.16a-Win32 NewsLink 1.98
Time Warp of the Future BBS - telnet://time.synchro.net:24
 
J

Jeff Higgins

To: clusardi2k
From: Jeff Higgins <[email protected]>

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();
}
});
}
}

--- BBBS/Li6 v4.10 Dada-1
* Origin: Prism bbs (1:261/38)
--- Synchronet 3.16a-Win32 NewsLink 1.98
Time Warp of the Future BBS - telnet://time.synchro.net:24
 
J

John B. Matthews

To: clusardi2k
From: "John B. Matthews" <[email protected]>

(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>

--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>

--- BBBS/Li6 v4.10 Dada-1
* Origin: Prism bbs (1:261/38)
--- Synchronet 3.16a-Win32 NewsLink 1.98
Time Warp of the Future BBS - telnet://time.synchro.net:24
 
J

Jeff Higgins

To: Jeff Higgins
From: Jeff Higgins <[email protected]>

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();
}
});
}
}

--- BBBS/Li6 v4.10 Dada-1
* Origin: Prism bbs (1:261/38)
--- Synchronet 3.16a-Win32 NewsLink 1.98
Time Warp of the Future BBS - telnet://time.synchro.net:24
 
C

clusardi2k

To: Jeff Higgins
From: (e-mail address removed)

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();
}
});
}
}

--- BBBS/Li6 v4.10 Dada-1
* Origin: Prism bbs (1:261/38)
--- Synchronet 3.16a-Win32 NewsLink 1.98
Time Warp of the Future BBS - telnet://time.synchro.net:24
 
M

markspace

To: clusardi2k
From: 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>

--- BBBS/Li6 v4.10 Dada-1
* Origin: Prism bbs (1:261/38)
--- Synchronet 3.16a-Win32 NewsLink 1.98
Time Warp of the Future BBS - telnet://time.synchro.net:24
 
J

Jeff Higgins

To: clusardi2k
From: Jeff Higgins <[email protected]>

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.

--- BBBS/Li6 v4.10 Dada-1
* Origin: Prism bbs (1:261/38)
--- Synchronet 3.16a-Win32 NewsLink 1.98
Time Warp of the Future BBS - telnet://time.synchro.net:24
 
C

clusardi2k

To: markspace
From: (e-mail address removed)

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;
}

--- BBBS/Li6 v4.10 Dada-1
* Origin: Prism bbs (1:261/38)
--- Synchronet 3.16a-Win32 NewsLink 1.98
Time Warp of the Future BBS - telnet://time.synchro.net:24
 
J

Jeff Higgins

To: Jeff Higgins
From: Jeff Higgins <[email protected]>

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.

--- BBBS/Li6 v4.10 Dada-1
* Origin: Prism bbs (1:261/38)
--- Synchronet 3.16a-Win32 NewsLink 1.98
Time Warp of the Future BBS - telnet://time.synchro.net:24
 
R

Roedy Green

To: clusardi2k
From: Roedy Green <[email protected]>

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

The background colour setBackground() is ignored unless you do a setOpaque(
true ).

See http://mindprod.com/jgloss/jlabel.html for sample code.
--
Roedy Green Canadian Mind Products http://mindprod.com A new scientific truth
does not triumph by convincing its opponents and making them see the light,
but rather because its opponents eventually die, and a new generation grows up
that is familiar with it.
~ Max Planck 1858-04-23 1947-10-04

--- BBBS/Li6 v4.10 Dada-1
* Origin: Prism bbs (1:261/38)
--- Synchronet 3.16a-Win32 NewsLink 1.98
Time Warp of the Future BBS - telnet://time.synchro.net:24
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top