Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

jLabel setVisible(true) Doesn't Work

539 views
Skip to first unread message

clusardi2k

unread,
Aug 10, 2012, 2:38:52 PM8/10/12
to
From: "clusardi2k" <clusardi2k@1:261/38.remove-k2r-this>

From: "clusardi2k" <clusardi2k@1:261/38.remove-qhs-this>

From: clusa...@aol.com

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

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

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

markspace

unread,
Aug 10, 2012, 2:38:52 PM8/10/12
to
To: clusardi2k
From: "markspace" <markspace@1:261/38.remove-k2r-this>

To: clusardi2k
From: "markspace" <markspace@1:261/38.remove-qhs-this>

To: clusardi2k
From: markspace <-@.>

On 8/7/2012 12:15 PM, clusa...@aol.com wrote:

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

clusardi2k

unread,
Aug 10, 2012, 2:38:54 PM8/10/12
to
To: markspace
From: "clusardi2k" <clusardi2k@1:261/38.remove-k2r-this>

To: markspace
From: "clusardi2k" <clusardi2k@1:261/38.remove-qhs-this>

To: markspace
From: clusa...@aol.com

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,

Jeff Higgins

unread,
Aug 10, 2012, 2:38:54 PM8/10/12
to
To: clusardi2k
From: "Jeff Higgins" <jeff.higgins@1:261/38.remove-k2r-this>

To: clusardi2k
From: "Jeff Higgins" <jeff.higgins@1:261/38.remove-qhs-this>

To: clusardi2k
From: Jeff Higgins <je...@invalid.invalid>

On 08/07/2012 06:22 PM, clusa...@aol.com wrote:
> 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();
}
});

John B. Matthews

unread,
Aug 10, 2012, 2:38:55 PM8/10/12
to
To: clusardi2k
From: "John B. Matthews" <john.b..matthews@1:261/38.remove-k2r-this>

To: clusardi2k
From: "John B. Matthews" <john.b..matthews@1:261/38.remove-qhs-this>

To: clusardi2k
From: "John B. Matthews" <nos...@nospam.invalid>

In article <ddf6ecbb-1c20-4163...@googlegroups.com>,
clusa...@aol.com wrote:

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

Jeff Higgins

unread,
Aug 10, 2012, 2:38:55 PM8/10/12
to
To: Jeff Higgins
From: "Jeff Higgins" <jeff.higgins@1:261/38.remove-k2r-this>

To: Jeff Higgins
From: "Jeff Higgins" <jeff.higgins@1:261/38.remove-qhs-this>

To: Jeff Higgins
From: Jeff Higgins <je...@invalid.invalid>

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);
0 new messages