participate


Swing [Archive] - Unresponsive status bar
<<   Back to Forum  |   Give us Feedback
This topic has 7 replies on 1 page.
robmrozek
Posts:33
Registered: 3/19/02
Unresponsive status bar   
Mar 22, 2002 5:53 AM

 
I have been pulling my hair out over a JTextField I am using as a status bar which just doesn't seem to respond when I set the text. Here is the method the constructor calls to make the status bar:

private void makeStatusBar() {
statusField = new JTextField();
statusField.setEditable(false);
statusField.setForeground(Color.black);
statusField.setFont(new Font("TimesRoman", Font.PLAIN, 12));
}

And here is part of my actionPerformed method which makes use of the status bar:

statusField.setText(" Connecting with database ...");
Data[] temp = database.search(queryFieldText);
statusField.setText("");

The database.search method is an invocation of a remote method, and while testing I have deliberately put a two second delay in the search() method to simulate a network delay. But my status panel remains stubbornly blank.

Does a separate thread look after the text setting and is it possible that before it gets a look in the next line is being executed?

I've checked out this forum and have seen advice on SwingUtilities.invokeAndWait(), but this seems very complicated and so far my experiments have ended in failure!

Any ideas?
Thanks, Rob
 
camickr
Posts:32,726
Registered: 2/27/98
Re: Unresponsive status bar   
Mar 22, 2002 10:11 AM (reply 1 of 7)  (In reply to original post )

 
The problem, as I understand how swing works, is that repainting of components is only done when the event thread is free. Therefore, Swing doesn't get a chance to paint the status bar before the text is reset to "". Here is a solution that uses the paintImmediately() method.

import java.awt.;
import java.awt.event.
;
import javax.swing.;
import javax.swing.border.
;
import javax.swing.text.*;

public class StatusBar extends JFrame
{
JTextField statusBar;

public StatusBar()
{
JPanel panel = new JPanel();
panel.setLayout( new BorderLayout() );
setContentPane( panel );

JButton button = new JButton( "Connect..." );
panel.add( button, BorderLayout.NORTH );

button.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
statusBar.setText( "Connecting to database..." );
Dimension dim = statusBar.getSize();
statusBar.paintImmediately(0, 0, dim.width, dim.height);

try
{
Thread.sleep( 2000 );
}
catch (Exception exc) {}

statusBar.setText( "" );
}
});

statusBar = new JTextField();
panel.add( statusBar, BorderLayout.SOUTH );
}

public static void main(String[] args)
{
StatusBar frame = new StatusBar();
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
frame.pack();
frame.setVisible(true);
}
}
 
robmrozek
Posts:33
Registered: 3/19/02
Re: Unresponsive status bar   
Mar 25, 2002 4:45 AM (reply 2 of 7)  (In reply to #1 )

 
Thanks for bringing to my attention the JComponent paintImmediately method - it worked!
 
simonfox1
Posts:1
Registered: 2/25/00
Re: Unresponsive status bar   
Nov 9, 2002 1:24 PM (reply 3 of 7)  (In reply to #2 )

 
Thanks very much for this tip as I was having similar problems with a recursive routine that needed to show an update with each iteration.

A great help.
 
svenmeier
Posts:315
Registered: 8/13/98
Re: Unresponsive status bar   
Nov 11, 2002 1:13 AM (reply 4 of 7)  (In reply to #3 )

 
paintImmediately() is just a dirty hack, e.g. it won't help you with layout-updates or repaints when your component was obscured.

If you want a real solution, you have to get used to SwingWorker. Or take a look at http://spin.sourceforge.net .

Sven
 
benoitx
Posts:3
Registered: 4/2/98
Re: Unresponsive status bar   
Nov 12, 2002 10:10 AM (reply 5 of 7)  (In reply to #4 )

 
Sven,

Could you provide a simple example based around SwingWorker for the problem mentioned? Spin looks good but a bit overwhelming at the moment.

Many thanks
Benoit
 
svenmeier
Posts:315
Registered: 8/13/98
Re: Unresponsive status bar   
Nov 15, 2002 2:59 AM (reply 6 of 7)  (In reply to #5 )

 
public void actionPerformed(ActionEvent ev) {
 
  statusField.setText(" Connecting with database ...");
 
  final queryFieldText = queryField.getText();
 
  new SwingWorker() {
    public Object construct() {
      return database.search(queryFieldText);
    }
 
    public void finished() {
      Data[] temp = (Data[])getValue();
 
      // show data on the GUI
 
      statusField.setText("");
    }
  }).start();
}
 
acethrowbot
Posts:53
Registered: 3/7/03
Re: Unresponsive status bar   
Apr 22, 2003 12:29 PM (reply 7 of 7)  (In reply to #6 )

 
just a thought. i think that you should try using a JLabel, instead of an uneditable JTextField. you might find that it looks better. it's probably better form anyway.
 
This topic has 7 replies on 1 page.
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics
    Users Online : 27
  • Guests : 138

About Sun forums
  • Sun Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Sun Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums