Still slightly clueless about this GUI thing...(JProgressBar never draws on a JDialog)

  • Thread starter Inertia_sublimation
  • Start date
I

Inertia_sublimation

Hehe, yup, its me again, Im still having problems with my GUI, I hope you
guys dont mind my probobly insanely stupid questions. :blush:

Well, Ive followed all the advice in the thread I posted earlier ("GUI
Newb... Im not sure how to make this work. (Auto-scrollbars using a
JTextArea")

And rewrote the NetBeans auto-generated source by hand, it turned out quite
a bit more readable.

Here's the snippet I'm having trouble with:

private JDialog getWaitDialog()
{
/* Init local variables */
final JDialog waitDialog;
final JProgressBar waitBar;
final JLabel waitLabel;
final GridBagConstraints gridBagConstraints;

waitDialog = new JDialog();
waitBar = new JProgressBar();
waitLabel = new JLabel("Please wait...");
gridBagConstraints = new GridBagConstraints();

/* Set values on components: */
waitDialog.setTitle("Processing...");
waitDialog.setResizable(false);
waitDialog.setCursor(new
java.awt.Cursor(java.awt.Cursor.WAIT_CURSOR));
waitDialog.setLocationRelativeTo(null);

waitBar.setIndeterminate(true);

/* Add components to window */
waitDialog.getContentPane().add(waitLabel);
waitDialog.getContentPane().add(waitBar);

/*Add components to waitDialog */
waitDialog.getContentPane().setLayout(new java.awt.GridBagLayout());

/* Add the progressBar. */
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
waitDialog.getContentPane().add(waitBar, gridBagConstraints);

/* Add the label. */
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
waitDialog.getContentPane().add(waitLabel, gridBagConstraints);

waitDialog.pack();
return waitDialog;
}

/End snippet

You see, this method returns a JDialog containing a JLabel and JProgressBar.
It's supposed to be drawing the JProgressBar while a (possibly) extensive
process is running. After the process has completed its task, the program
setVisible(false) and discard()'s the JDialog, and opens a new window with
the result.

The problem is, is that when the method that calls getWaitDialog() runs, and
show()'s the dialog, all I get is a grey box, no label or progress bar,
until the task is completed (then it dissappears, like its supposed to).

Now... why does the dialog refuse to paint its components?

Here's the method that calls getWaitDialog():
public void mousePressed(MouseEvent evt)
{
/* Display the waitDialog */
final JDialog waitDialog = getWaitDialog();
waitDialog.show();

/* Run long process */
final JFrame resultWindow =
getResultWindow(parser.parseCode(codeTextArea.getText()));

/* Kill the waitDialog */
waitDialog.setVisible(false);
waitDialog.dispose();

/* Display process result */
resultWindow.show();
}

The JDialog *still* doesnt paint even if I have a Thread.sleep() in between
the waitDialog().show; and the resultWindow initilization.
 
I

Inertia_sublimation

I was browsing the comp.lang.java.help forum, and came across a post that
talked about thread safe Swing execution. I think that page had some useful
info. on how to accomplish my task, however I get compiler errors.
The page was:
http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html
Using the SwingWorker class, this is the new calling method (see the first
post):
itemCompile.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent evt)
{
final JDialog waitDialog = getWaitDialog();
waitDialog.show();

final JFrame resultWindow;
final SwingWorker worker = new SwingWorker()
{
String parsedText;

public Object construct()
{
parsedText =
parser.parseCode(codeTextArea.getText());
return parsedText;
}

//Runs on the event-dispatching thread.
public void finished()
{
resultWindow = getResultWindow(parsedText);
}
};
worker.start();


waitDialog.setVisible(false);
waitDialog.dispose();
resultWindow.show();
}
});

However, when I try to compile this, it complains:

CodeColorerGUI.java [199:1] cannot assign a value to final variable
resultWindow
resultWindow = getResultWindow(parsedText);
^
1 error
Errors compiling CodeColorerGUI.

When I un-finalize the resultWindow variable it still complains:
CodeColorerGUI.java [199:1] local variable resultWindow is accessed from
within inner class; needs to be declared final
resultWindow = getResultWindow(parsedText);
^
1 error
Errors compiling CodeColorerGUI.

I've never run into a situation where the compiler contradicts itself....
please help, thanks in advance!
 
A

ak

The problem is, is that when the method that calls getWaitDialog() runs, and
show()'s the dialog, all I get is a grey box, no label or progress bar,
until the task is completed (then it dissappears, like its supposed to).

Now... why does the dialog refuse to paint its components?

/* Run long process */
final JFrame resultWindow =
getResultWindow(parser.parseCode(codeTextArea.getText()));

long process should run in _another_thread_ (NOT in EDT)
 
A

ak

However, when I try to compile this, it complains:
CodeColorerGUI.java [199:1] cannot assign a value to final variable
resultWindow
resultWindow = getResultWindow(parsedText);
^
1 error
Errors compiling CodeColorerGUI.

When I un-finalize the resultWindow variable it still complains:
CodeColorerGUI.java [199:1] local variable resultWindow is accessed from
within inner class; needs to be declared final
resultWindow = getResultWindow(parsedText);
^
1 error
Errors compiling CodeColorerGUI.

I've never run into a situation where the compiler contradicts itself....

you shouldn't use local variables for this - use fields with lazy
initializing instead:

public class CodeColorerGUI {

JDialog waitDialog;
JProgressBar waitBar;
JLabel waitLabel;

private JDialog getWaitDialog() {
gridBagConstraints = new GridBagConstraints();

if(waitDialog == null) {
waitDialog = new JDialog();
waitBar = new JProgressBar();
waitLabel = new JLabel("Please wait...");
}

//.....
return waitDialog;
}


____________

http://reader.imagero.com the best java image reader.
 
A

Andrew Thompson

Inertia_sublimation said:
I was browsing the comp.lang.java.help forum, and came across a post that
talked about thread safe Swing execution. I think that page had some useful
info. on how to accomplish my task, however I get compiler errors. .....
When I un-finalize the resultWindow variable it still complains:
CodeColorerGUI.java [199:1] local...

Note the reference to local
... variable resultWindow is accessed from
within inner class; needs to be declared final
resultWindow = getResultWindow(parsedText);
^
1 error
Errors compiling CodeColorerGUI.

I've never run into a situation where the compiler contradicts itself....
please help, thanks in advance!

No, if you read between the lines, I think it is
suggesting that this variable must be both a
class variable (not local) and not final.

HTH
 
I

Inertia_sublimation

Thanks all! I was able to squish that little bug in my program. I ended up
using SwingWorker to process the parsing while the JDialog redrew itself.
And about the local variable in the compiler error....

ArrayList ruleOfThumbStorage = new ArrayList();
ruleOfThumbStorage.add( "Never code at 5 AM ;-)" );


Andrew Thompson said:
Inertia_sublimation said:
I was browsing the comp.lang.java.help forum, and came across a post that
talked about thread safe Swing execution. I think that page had some useful
info. on how to accomplish my task, however I get compiler errors. ....
When I un-finalize the resultWindow variable it still complains:
CodeColorerGUI.java [199:1] local...

Note the reference to local
... variable resultWindow is accessed from
within inner class; needs to be declared final
resultWindow = getResultWindow(parsedText);
^
1 error
Errors compiling CodeColorerGUI.

I've never run into a situation where the compiler contradicts itself....
please help, thanks in advance!

No, if you read between the lines, I think it is
suggesting that this variable must be both a
class variable (not local) and not final.

HTH

--
Andrew Thompson
* http://www.PhySci.org/ PhySci software suite
* http://www.1point1C.org/ 1.1C - Superluminal!
* http://www.AThompson.info/andrew/ personal site
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top