Results 1 to 12 of 12
Thread: [SOLVED] update while running
- 03-21-2009, 02:04 AM #1
Member
- Join Date
- Feb 2009
- Posts
- 12
- Rep Power
- 0
[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
-
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.
- 03-21-2009, 03:01 AM #3
Senior Member
- Join Date
- Dec 2008
- Posts
- 526
- Rep Power
- 0
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 )
- 03-21-2009, 03:31 AM #4
Member
- Join Date
- Feb 2009
- Posts
- 12
- Rep Power
- 0
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.
- 03-21-2009, 03:35 AM #5
Member
- Join Date
- Feb 2009
- Posts
- 12
- Rep Power
- 0
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.
-
Without seeing your code in context, it's quite impossible to tell you what is wrong much less how to fix it.swing.component.setText("whatever"); does not work to update while running.
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)It only updates the GUI after all code are completed witch leaves my users without feedback.
Nah, NetBeans is fine. Chances are your code is broken somewhere. Trust me; they're a whole lot smarter then you or me.Might be that NetBeans is broken
- 03-21-2009, 04:37 AM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
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.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.
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.
- 03-22-2009, 02:23 AM #8
Member
- Join Date
- Feb 2009
- Posts
- 12
- Rep Power
- 0
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
my JFrame class. It has some extra junk with it from netbeans thou.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);
}
}
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
}
-
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(); } }); } }
- 03-24-2009, 01:02 AM #10
Member
- Join Date
- Feb 2009
- Posts
- 12
- Rep Power
- 0
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! =)
-
Thanks for the feed-back. I'm glad that you've got things moving forward. Best of luck.
- 04-27-2013, 05:05 AM #12
Member
- Join Date
- Apr 2013
- Posts
- 1
- Rep Power
- 0
Similar Threads
-
phonebook update
By nanna in forum New To JavaReplies: 5Last Post: 03-09-2009, 10:13 PM -
JPanel won't update
By ibanez270dx in forum New To JavaReplies: 3Last Post: 01-06-2009, 08:59 PM -
How to update my jdk???
By low224 in forum New To JavaReplies: 4Last Post: 01-04-2009, 04:51 PM -
SQL update problem
By skiing in forum New To JavaReplies: 9Last Post: 11-21-2008, 06:54 PM -
Using sql:update tag
By Java Tip in forum Java TipReplies: 0Last Post: 01-13-2008, 11:49 PM


LinkBack URL
About LinkBacks

Bookmarks