Re: Layout Doesn't Work

Discussion in 'Java' started by clusardi2k, Aug 8, 2012.

  1. clusardi2k

    clusardi2k Guest

    To: clusardi2k
    From:

    On Wednesday, August 8, 2012 12:24:39 PM UTC-4, (unknown) wrote:
    > 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.LEADIN
    G)
    .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.LEADIN
    G)
    .addGroup(layout.createSequentialGroup()
    .addGap(270, 270, 270)
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Al
    ignment.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
    }

    --- 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
     
    clusardi2k, Aug 8, 2012
    #1
    1. Advertising

  2. clusardi2k

    markspace Guest

    To: clusardi2k
    From: markspace <-@.>

    On 8/8/2012 10:22 AM, wrote:

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



    You can't block the EDT like this. The loops stop the EDT from displaying any
    updates to the GUI. Use a SwingWorker to execute tasks off the EDT.

    <http://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html>

    Reading that entire trail (Concurrency in Swing) will be a benefit to you.

    --- 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, Aug 8, 2012
    #2
    1. Advertising

  3. clusardi2k

    Jeff Higgins Guest

    To: clusardi2k
    From: Jeff Higgins <>

    On 08/08/2012 01:22 PM, wrote:
    > On Wednesday, August 8, 2012 12:24:39 PM UTC-4, (unknown) wrote:
    >> I'll report back in a few minutes!

    >
    > It gave me confusing results


    Are you a troll?
    If so, nice!
    If not, yep.

    --- 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
     
    Jeff Higgins, Aug 8, 2012
    #3
  4. clusardi2k

    clusardi2k Guest

    To: clusardi2k
    From:

    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,

    On Wednesday, August 8, 2012 1:22:05 PM UTC-4, (unknown) wrote:
    //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.LEADIN
    G)
    .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.LEADIN
    G)
    .addGroup(layout.createSequentialGroup()
    .addGap(270, 270, 270)
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Al
    ignment.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
    }

    --- 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
     
    clusardi2k, Aug 8, 2012
    #4
  5. clusardi2k

    markspace Guest

    To: clusardi2k
    From: markspace <-@.>

    On 8/8/2012 11:15 AM, wrote:
    > 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?

    --- 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, Aug 9, 2012
    #5
  6. clusardi2k

    Jeff Higgins Guest

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

    --- 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
     
    Jeff Higgins, Aug 10, 2012
    #6
  7. clusardi2k

    Jeff Higgins Guest

    To: Jeff Higgins
    From: Jeff Higgins <>

    On 08/10/2012 10:09 AM, Jeff Higgins wrote:

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

    + timer.setRepeats(false);

    --- 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
     
    Jeff Higgins, Aug 10, 2012
    #7
  8. clusardi2k

    Jeff Higgins Guest

    To: Jeff Higgins
    From: Jeff Higgins <>

    > pack();
    > label.setPreferredSize(label.getPreferredSize());
    > label.setVisible(false);

    - setVisible(true);

    --- 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
     
    Jeff Higgins, Aug 10, 2012
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rick Spiewak
    Replies:
    3
    Views:
    3,214
    Rick Spiewak
    Aug 26, 2003
  2. RobertH
    Replies:
    1
    Views:
    758
    Steve C. Orr [MVP, MCSD]
    Nov 4, 2003
  3. NWx
    Replies:
    4
    Views:
    2,999
    Kevin Spencer
    Feb 19, 2004
  4. Replies:
    1
    Views:
    640
    John Timney \(MVP\)
    Jun 19, 2006
  5. clusardi2k

    Re: Layout Doesn't Work

    clusardi2k, Aug 9, 2012, in forum: Java
    Replies:
    4
    Views:
    249
    markspace
    Aug 10, 2012
Loading...

Share This Page