Results 1 to 12 of 12
  1. #1
    Pierced1 is offline Member
    Join Date
    Feb 2009
    Posts
    12
    Rep Power
    0

    Question [SOLVED] update while running

    I'm at a loss here:confused:. Have tried multiple google search with no luck in several days now.

    All i want to know is how do i update the text of lets say an JLabel or an TextField while a program is running?

    As it is now it will only update the component after all code has executed. And if i minimize the program while running and pulling it back upp it only shows a black window until program has executed all code.

    (i'm using NetBeans 6.5) The components are in a JPanel that's in a JFrame.

    Could someone give an exempel in let's say a while loop?:o

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    to update the text of a JLabel or JTextField, you simply call setText(...), that's it. If it doesn't work, then you've got something else that's wrong. Post your code. Also, I strongly suggest that until you fully understand Swing, you not use Netbeans to generate your code. You'll be far better off going through the Sun Swing tutorials and learn to code it yourself. if you do this, you'll thank me, trust me.

  3. #3
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    What effect exactly you want to achive?
    If you just want to change text in Swing components but here I can't get in what way... By some timer or what? So we need more information )

  4. #4
    Pierced1 is offline Member
    Join Date
    Feb 2009
    Posts
    12
    Rep Power
    0

    Default

    I just want to update the GUI while the program is running (executing code).
    I want to update at several points in the program, one is in a while loop, one is at the beginning of an method.

    It only updates the GUI after all code are completed witch leaves my users without feedback.

    swing.component.setText("whatever"); does not work to update while running.

  5. #5
    Pierced1 is offline Member
    Join Date
    Feb 2009
    Posts
    12
    Rep Power
    0

    Default

    btw the code is a few thousand lines long... i don't think posting them is necessary since i have same problem if i try to update an label or textfield in a jframe with only a button while in a while-loop for example.

    Might be that NetBeans is broken, i have noticed it doesn't always run the latest saved compilation.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    swing.component.setText("whatever"); does not work to update while running.
    Without seeing your code in context, it's quite impossible to tell you what is wrong much less how to fix it.

    It only updates the GUI after all code are completed witch leaves my users without feedback.
    But I have a sneaking suspicion that you are somehow running a long process on the EDT (the Event Dispatch Thread -- the main thread that runs Swing graphics and user interactions) and that this is somehow putting your app to sleep. Again, given the information presented, it's impossible to tell. You would do well though to read this article from the Sun tutorials on concurrency and Swing: Lesson: Concurrency in Swing (The Java™ Tutorials > Creating a GUI with JFC/Swing)
    Might be that NetBeans is broken
    Nah, NetBeans is fine. Chances are your code is broken somewhere. Trust me; they're a whole lot smarter then you or me.

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    btw the code is a few thousand lines long... i don't think posting them is necessary since i have same problem if i try to update an label or textfield in a jframe with only a button while in a while-loop for example.
    Doubtless your code is wonderful, but when people say "post code" they don't mean post thousands of lines of stuff that has nothing to do with the actual problem.

    A simple label in a frame that doesn't update during the execution of the while loop is fine. The important thing is that it should compile, run and illustrate the problem. It's a SSCCE.

    Your phrase "while in a while loop" suggests the EDT problem that Fubarable mentioned. If the concurrency link doesn't help, post a SSCCE.

  8. #8
    Pierced1 is offline Member
    Join Date
    Feb 2009
    Posts
    12
    Rep Power
    0

    Default

    So now i have made an SSCCE for you to look at. I have duplicated the problem and tried some things from Sun Java that you mentioned but i don't understand their example in.

    Simple Background Tasks (The Java™ Tutorials > Creating a GUI with JFC/Swing > Concurrency in Swing)

    my test code that replicates my original code is here:

    start class
    import javax.swing.SwingUtilities;

    public class Start {

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

    private static void createAndShowGUI() {
    System.out.println("Created GUI on EDT? "+SwingUtilities.isEventDispatchThread()); //true
    TestClass tc = new TestClass();
    tc.pack();
    tc.setVisible(true);
    }
    }
    my JFrame class. It has some extra junk with it from netbeans thou.
    public class TestClass extends javax.swing.JFrame {

    boolean running=true;
    /** Creates new form TestClass */
    public TestClass() {
    initComponents();
    }

    private void doThis() {
    for (int i = 0; i < 35; i++) {
    if(i==5)
    testLbl.setText("something");
    if(i==10)
    testLbl.setText("anything");
    if(i==15)
    testLbl.setText("something");
    if(i==20)
    testLbl.setText("anything");
    if(i==25)
    testLbl.setText("something");
    if(i==30)
    testLbl.setText("anything");
    }
    }

    /** This method is called from within the constructor to
    * initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is
    * always regenerated by the Form Editor.
    */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

    testPanel = new javax.swing.JPanel();
    testLbl = new javax.swing.JLabel();
    startBtn = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);

    testLbl.setText("nothing");

    startBtn.setText("start");
    startBtn.addMouseListener(new java.awt.event.MouseAdapter() {
    public void mouseClicked(java.awt.event.MouseEvent evt) {
    startBtnMouseClicked(evt);
    }
    });

    javax.swing.GroupLayout testPanelLayout = new javax.swing.GroupLayout(testPanel);
    testPanel.setLayout(testPanelLayout);
    testPanelLayout.setHorizontalGroup(
    testPanelLayout.createParallelGroup(javax.swing.Gr oupLayout.Alignment.LEADING)
    .addGroup(testPanelLayout.createSequentialGroup()
    .addGroup(testPanelLayout.createParallelGroup(java x.swing.GroupLayout.Alignment.LEADING)
    .addGroup(testPanelLayout.createSequentialGroup()
    .addGap(19, 19, 19)
    .addComponent(startBtn))
    .addGroup(testPanelLayout.createSequentialGroup()
    .addGap(29, 29, 29)
    .addComponent(testLbl)))
    .addContainerGap(29, Short.MAX_VALUE))
    );
    testPanelLayout.setVerticalGroup(
    testPanelLayout.createParallelGroup(javax.swing.Gr oupLayout.Alignment.LEADING)
    .addGroup(testPanelLayout.createSequentialGroup()
    .addGap(18, 18, 18)
    .addComponent(testLbl)
    .addGap(18, 18, 18)
    .addComponent(startBtn)
    .addContainerGap(32, Short.MAX_VALUE))
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addComponent(testPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    );
    layout.setVerticalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addComponent(testPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );

    pack();
    }// </editor-fold>

    private void startBtnMouseClicked(java.awt.event.MouseEvent evt) {
    doThis();
    }

    // Variables declaration - do not modify
    private javax.swing.JButton startBtn;
    private javax.swing.JLabel testLbl;
    private javax.swing.JPanel testPanel;
    // End of variables declaration

    }

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Yes, you're expecting to see the changes in the JLabel when they are made almost instantaneously. To see changes here you should use a Swing Timer. Here's an SSCCE that shows an example of this:

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JComponent;
    import javax.swing.SwingConstants;
    import javax.swing.Timer;
    
    public class DemoTimer
    {
      private static final String[] LABEL_STRINGS =
      {
        "Sunday", "Monday", "Tuesday", "Wednesday", 
        "Thursday", "Friday", "Saturday"
      };
      private static final Dimension APP_SIZE = new Dimension(300, 100);
      protected static final int TIMER_DELAY = 600;
      private JPanel mainPanel = new JPanel();
      private JLabel label = new JLabel("Watch this space", SwingConstants.CENTER);
    
      public DemoTimer()
      {
        final JButton startButton = new JButton("Start");
        JPanel btnPanel = new JPanel();
        btnPanel.add(startButton);
        startButton.addActionListener(new ActionListener()
        {
          public void actionPerformed(ActionEvent e)
          {
            startButton.setEnabled(false);
            new Timer(TIMER_DELAY, new ActionListener()
            {
              int count = 0;
              public void actionPerformed(ActionEvent e)
              {
                if (count < LABEL_STRINGS.length)
                {
                  label.setText(LABEL_STRINGS[count]);
                  count++;
                }
                else
                {
                  startButton.setEnabled(true);
                  ((Timer)e.getSource()).stop();
                }
              }
            }).start();
          }
        });
        
        
        mainPanel.setPreferredSize(APP_SIZE);
        mainPanel.setLayout(new BorderLayout());
        mainPanel.add(label, BorderLayout.NORTH);
        mainPanel.add(btnPanel, BorderLayout.SOUTH);
      }
    
      public JComponent getComponent()
      {
        return mainPanel;
      }
    
      private static void createAndShowUI()
      {
        JFrame frame = new JFrame("DemoTimer");
        frame.getContentPane().add(new DemoTimer().getComponent());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    
      public static void main(String[] args)
      {
        java.awt.EventQueue.invokeLater(new Runnable()
        {
          public void run()
          {
            createAndShowUI();
          }
        });
      }
    }

  10. #10
    Pierced1 is offline Member
    Join Date
    Feb 2009
    Posts
    12
    Rep Power
    0

    Default

    Yes!!!
    Finally i got it to work, you set me on the right track and I'm very thankful for that.

    It was a thread issue but not really that the GUI ran on the wrong thread(but i learned it should run on the EDT-thread from now on) but that my big,slow and long method was on the wrong thread. (but i know it ain't easy without the actual code so =)

    When i did a simple new thread for the method with a run method in it the system updated and did not freeze anymore. I found the answer on a related thread at the bottom of my thread =).

    So thanks again Fubarable and pbrockway2 for the help, i learned a bunch of new stuff! =)

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Thanks for the feed-back. I'm glad that you've got things moving forward. Best of luck.

  12. #12
    jGuru is offline Member
    Join Date
    Apr 2013
    Posts
    1
    Rep Power
    0

Similar Threads

  1. phonebook update
    By nanna in forum New To Java
    Replies: 5
    Last Post: 03-09-2009, 10:13 PM
  2. JPanel won't update
    By ibanez270dx in forum New To Java
    Replies: 3
    Last Post: 01-06-2009, 08:59 PM
  3. How to update my jdk???
    By low224 in forum New To Java
    Replies: 4
    Last Post: 01-04-2009, 04:51 PM
  4. SQL update problem
    By skiing in forum New To Java
    Replies: 9
    Last Post: 11-21-2008, 06:54 PM
  5. Using sql:update tag
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-13-2008, 11:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •