GUI question

J

jason

hello,
i have question regarding adding an additional GUI to an existing GUI.

i have the following placed inside of the action listener of a
button:

i am trying to implement the following but all i am getting is a
second blank popup window (no progress bar).

this UI is popped up after i click a "go!" button. the
meat of the analysis code executes.

once this starts i would like to watch my progress bar do what it is
made to do. you know. progress!
Code:
public void actionPerformed(ActionEvent arg0) {

JFrame1.setSize(270, 250);
JFrame1.setLocation(200, 200);
JFrame1.setResizable(false);
Insets insets = JFrame1.getInsets();
        JFrame1.setLayout(null);
            JProgressBar JProgressBar1=new JProgressBar(0,100);
            JProgressBar1.setValue(50);
            JProgressBar1.setStringPainted(true);
            Dimension size=JProgressBar1.getPreferredSize();

JProgressBar1.setBounds(insets.left,insets.top,size.width,size.height);
            JFrame1.add(JProgressBar1);
                JFrame1.setVisible(true); //i can see this. and all
the applied choices are true. i cannot resize and it appears where it
should at the correct size. but lo and behold, not progressbar.

         for (int i=0;i<=Out.size()-1;i++){

                JProgressBar1.setValue(i);
//other operations..
           }
System.exit(0);
 
J

jason

I didn't read carefully, but almost certainly this is being done on the
EDT.  Which means the EDT can't run to update the progress bar display.
  Therefore, you don't see any updates.

Try looking at one of the examples on this page:

<http://java.sun.com/docs/books/tutorial/uiswing/components/progress.html>

Yes, I seem to be having an issue of associating the two GUI's.

IE: code will look from 1-100 a multiple of times (usually 3-4).
GUI-1: takes input values which cause variations in the complexity of
calculations and the "invoke" or "start" command.
GUI-2: this GUI i want to show the progress of my code underlying GUI
1.

I have looked more closely at the page you referenced and my main
question is the following:
how would i go about creating a GUI that accepts input values? i
envision this second GUI as some sort of function operating in the
following:
Code:
//coming from GUI-1 class:

public void actionPerformed(ActionEvent arg0) {

for (int i=0;i<=someMaxSize;i++){

    MyGUI2class.main(i);

}//close for

}//close action performed

//in my GUI2 class
public static void main(int arg0){


if (!MyGUI2==null){ //i am coding this in this window, please take
this as pseudo and not executable.
//create GUI up here etc.
   MyGUI2.setVisible(true);
}else{
   progressBar.setValue(arg0);
}//close if

}//close main

this demonstrates the basic idea of what i am trying to accomplish. i
am sure it is nothing new to a lot of you.

any assistance in overcoming this obstacle would be appreciated!
 
L

Lew

jason said:
I have looked more closely at the page you referenced and my main
question is the following:
how would i [sic] go about creating a GUI that accepts input values? i [sic]
envision this second GUI as some sort of function operating in the
following:
Code:
//coming from GUI-1 class:

public void actionPerformed(ActionEvent arg0) {

for (int i=0;i<=someMaxSize;i++){

MyGUI2class.main(i);[/QUOTE]

You definitely will not want to start a new instance of your second "GUI" 
every time you update a progress bar!

You aren't talking about two GUIs.  GUI means "graphical user interface".  You 
have one of those.

The 'actionPerformed()' method should simply update the value used by the 
progress bar.  This will take negligible time and thus not tie up the EDT.
 
J

jason

jason said:
I have looked more closely at the page you referenced and my main
question is the following:
how would i [sic] go about creating a GUI that accepts input values? i [sic]
envision this second GUI as some sort of function operating in the
following:
Code:
//coming from GUI-1 class:[/QUOTE]
[QUOTE]
public void actionPerformed(ActionEvent arg0) {[/QUOTE]
[QUOTE]
for (int i=0;i<=someMaxSize;i++){[/QUOTE]
[QUOTE]
     MyGUI2class.main(i);[/QUOTE]

You definitely will not want to start a new instance of your second "GUI"
every time you update a progress bar!

You aren't talking about two GUIs.  GUI means "graphical user interface".  You
have one of those.

The 'actionPerformed()' method should simply update the value used by the
progress bar.  This will take negligible time and thus not tie up the EDT.
[/QUOTE]

i know what a gui is, i have programmed many in different languages
but am having a hard time updating this progressbar.

currently i have a progressbar in my main gui set to 50 (just so that
i can see it is registering, which it is). once i click my action
button the gui animation ceases and the gui becomes a static image of
itself (progress bar looks like an image of itself as of when i
clicked the action button). also, the value on the progressbar seems
unable to be changed.
 
J

jason

jason said:
I have looked more closely at the page you referenced and my main
question is the following:
how would i [sic] go about creating a GUI that accepts input values? i [sic]
envision this second GUI as some sort of function operating in the
following:
Code:
//coming from GUI-1 class: 
public void actionPerformed(ActionEvent arg0) { 
for (int i=0;i<=someMaxSize;i++){ 
     MyGUI2class.main(i);[/QUOTE][/QUOTE]
[QUOTE]
You definitely will not want to start a new instance of your second "GUI"
every time you update a progress bar![/QUOTE]
[QUOTE]
You aren't talking about two GUIs.  GUI means "graphical user interface".  You
have one of those.[/QUOTE]
[QUOTE]
The 'actionPerformed()' method should simply update the value used by the
progress bar.  This will take negligible time and thus not tie up the EDT.[/QUOTE]

i know what a gui is, i have programmed many in different languages
but am having a hard time updating this progressbar.

currently i have a progressbar in my main gui set to 50 (just so that
i can see it is registering, which it is). once i click my action
button the gui animation ceases and the gui becomes a static image of
itself (progress bar looks like an image of itself as of when i
clicked the action button). also, the value on the progressbar seems
unable to be changed.[/QUOTE]

let me be more specific.

GUI1 - i have approximately 10 components. all working as they should.

when i use my actionlistener on my button, i expect my progressbar in
GUI1 to update accordingly as updated by a component update call (ie:
component(11).setValue(i)). unforunately it is not doing so.

with this i thought: maybe if i make a secondary popup window and
allow this second window to house my progressbar and update
accordingly i may have better luck. this is currently where i find
myself. i am not assuming this is the way to go about doing so, but in
my experiments with my first gui i am trying to see what works and
therefore i am not assuming i have the best solution, but am just
trying to reach a solution in general.

any pointers as to how to go about solving this would be appreciated
as my insight into GUI manipulation in java is minimal.

yet, thanks to the help i have received on here so far i have been
able to enlarge my understanding quickly.
 
L

Lew

jason said:
i [sic] know what a gui is, i [sic] have programmed many in different languages
but am having a hard time updating this progressbar.

My point wasn't to explain what a GUI is but that you only have one of them,
not two as you've been saying.

If you were such an expert you'd know that already.
currently i [sic] have a progressbar in my main gui set to 50 (just so that
i [sic] can see it is registering, which it is). once i [sic] click my action
button the gui animation ceases and the gui becomes a static image of
itself (progress bar looks like an image of itself as of when i [sic]
clicked the action button). also, the value on the progressbar seems
unable to be changed.

No doubt because you're invoking a new 'main()' each time rather than updating
the one progress bar, as I mentioned in my previous post.

Why don't you provide an SSCCE? This discussion is unlikely to progress far
without it at this point.

<http://sscce.org/>
 
J

jason

jason said:
i [sic] know what a gui is, i [sic] have programmed many in different languages
but am having a hard time updating this progressbar.

My point wasn't to explain what a GUI is but that you only have one of them,
not two as you've been saying.

If you were such an expert you'd know that already.
currently i [sic] have a progressbar in my main gui set to 50 (just so that
i [sic] can see it is registering, which it is). once i [sic] click my action
button the gui animation ceases and the gui becomes a static image of
itself (progress bar looks like an image of itself as of when i [sic]
clicked the action button). also, the value on the progressbar seems
unable to be changed.

No doubt because you're invoking a new 'main()' each time rather than updating
the one progress bar, as I mentioned in my previous post.

Why don't you provide an SSCCE?  This discussion is unlikely to progress far
without it at this point.

<http://sscce.org/>

Lew,
thanks for your input. no need to poke fun at my lack of expertise
with java, i come from a numerical computation background and
therefore the user interface aspect was never really an issue and
those languages which facilitate computation make it easier to
initialize and implement progress bars than more application oriented
languages.

i will provide an example when i am at the machine the code is on.
java is a language i have decided to learn on my own, so any help i
receive is a great help.

i will update this post later today with a ssce.
thanks
 
L

Lew

jason wrote:
Lew,
thanks for your input. no need to poke fun at my lack of expertise
with java [sic], i [sic] come from a numerical computation background and

You misconstrue. I totally am not making fun of your lack of expertise in
Java. My comment about having only one GUI is not Java-specific.

What I was making fun of is your ego reaction to the advice. By taking
offense at some imagined insult not present in the messages, you missed the
points entirely.
 
E

Eric Sosman

[...]
when i use my actionlistener on my button, i expect my progressbar in
GUI1 to update accordingly as updated by a component update call (ie:
component(11).setValue(i)). unforunately it is not doing so.

The "classical" reason for this behavior is that you are
running the progress-making action on the Event Dispatch Thread,
preventing it from responding to changes. Here's the scenario:

1) user does mouse stuff
2) Swing deciphers mouse stuff
2) Swing decides your button has been clicked
3) Swing calls your ActionListener's actionPerformed()
4) your actionPerformed() runs and returns
5) Swing resumes monitoring events

(I'm using "Swing" as shorthand for "Swing, AWT, assorted core
classes, native code, O/S drivers, and miscellaneous whatnot.")
The point is that during (4) above, "Swing" is *not* monitoring
the mouse or the keyboard or anything else: *your* code runs and
monopolizes the EDT until it returns. So if your code tries to
do things to the visible state of a component, nothing (much) will
show up on the screen until *after* your code finishes. Dollars
to doughnuts this is the cause of your difficulty.

The cure is to avoid doing long-winded things on the EDT.
If your ActionListener (or whatever) can do its job quickly and
let any visual changes happen later, fine: Just go ahead and do
it right there in actionPerformed() or in things it calls. But
if the job will take significant time, or if you need the GUI to
remain responsive while the job makes progress, perform the work
on a different thread. Have your ActionListener start a new thread
or send a "work order" to an existing thread, so actionPerformed()
can return immediately while the work progresses in parallel.

All this is described in the on-line tutorial; begin with
<http://java.sun.com/docs/books/tutorial/uiswing/concurrency/index.html>
 
J

jason

[...]
when i use my actionlistener on my button, i expect my progressbar in
GUI1 to update accordingly as updated by a component update call (ie:
component(11).setValue(i)). unforunately it is not doing so.

     The "classical" reason for this behavior is that you are
running the progress-making action on the Event Dispatch Thread,
preventing it from responding to changes.  Here's the scenario:

        1) user does mouse stuff
        2) Swing deciphers mouse stuff
        2) Swing decides your button has been clicked
        3) Swing calls your ActionListener's actionPerformed()
        4) your actionPerformed() runs and returns
        5) Swing resumes monitoring events

(I'm using "Swing" as shorthand for "Swing, AWT, assorted core
classes, native code, O/S drivers, and miscellaneous whatnot.")
The point is that during (4) above, "Swing" is *not* monitoring
the mouse or the keyboard or anything else: *your* code runs and
monopolizes the EDT until it returns.  So if your code tries to
do things to the visible state of a component, nothing (much) will
show up on the screen until *after* your code finishes.  Dollars
to doughnuts this is the cause of your difficulty.

     The cure is to avoid doing long-winded things on the EDT.
If your ActionListener (or whatever) can do its job quickly and
let any visual changes happen later, fine: Just go ahead and do
it right there in actionPerformed() or in things it calls.  But
if the job will take significant time, or if you need the GUI to
remain responsive while the job makes progress, perform the work
on a different thread.  Have your ActionListener start a new thread
or send a "work order" to an existing thread, so actionPerformed()
can return immediately while the work progresses in parallel.

     All this is described in the on-line tutorial; begin with
<http://java.sun.com/docs/books/tutorial/uiswing/concurrency/index.html>

eric,
perfect. that is EXACTLY my issue!
thank you very much!
 
M

markspace

jason said:
GUI1 - i have approximately 10 components. all working as they should.


I have to echo Lew's advice here. You have one GUI. You have many GUI
components. Some of those components are Components, JComponents,
Windows, etc. But still only one GUI.

Off the top of my head, it sounds like you are trying to add a second
Window to the existing user interface. But I'm not really sure, because
you explain it oddly.

Your terminology makes it difficult to understand you. You say you've
only used GUI components from a client perspective, which I can believe.
Please try to learn a little from this post and use a more standard
(and precise) terminology. You sound like you are not a native English
speaker; is that true?

when i use my actionlistener on my button, i expect my progressbar in
GUI1 to update accordingly as updated by a component update call (ie:
component(11).setValue(i)). unforunately it is not doing so.


As Eric just said, and we all said previously, that is because you have
locked up the EDT (the only means by which the GUI can update) and
prevent it from running.

Please refer to this page again.

<http://java.sun.com/docs/books/tutorial/uiswing/components/progress.html>

Especially this part:

<http://java.sun.com/docs/books/tutorial/uiswing/components/progress.html#bars>

and the source code for that example. It does this:

<http://java.sun.com/docs/books/tuto...moProject/src/components/ProgressBarDemo.java>

public void actionPerformed(ActionEvent evt) {
startButton.setEnabled(false);
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
//Instances of javax.swing.SwingWorker are not reusuable, so
//we create new instances as needed.
task = new Task();
task.addPropertyChangeListener(this);
task.execute();
}

where Task is a SwingWorker, which is just the easiest way of executing
code on a thread other than the EDT. Please review that code for how to
write a SwingWorker (the Task class declaration is right there with the
code).
 
J

jason

I have to echo Lew's advice here.  You have one GUI.  You have many GUI
components.  Some of those components are Components, JComponents,
Windows, etc.  But still only one GUI.

Off the top of my head, it sounds like you are trying to add a second
Window to the existing user interface.  But I'm not really sure, because
you explain it oddly.

Your terminology makes it difficult to understand you.  You say you've
only used GUI components from a client perspective, which I can believe.
  Please try to learn a little from this post and use a more standard
(and precise) terminology.  You sound like you are not a native English
speaker;  is that true?


As Eric just said, and we all said previously, that is because you have
locked up the EDT (the only means by which the GUI can update) and
prevent it from running.

Please refer to this page again.

<http://java.sun.com/docs/books/tutorial/uiswing/components/progress.html>

Especially this part:

<http://java.sun.com/docs/books/tutorial/uiswing/components/progress.h...>

and the source code for that example.  It does this:

<http://java.sun.com/docs/books/tutorial/uiswing/examples/components/P...>

     public void actionPerformed(ActionEvent evt) {
         startButton.setEnabled(false);
         setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
         //Instances of javax.swing.SwingWorker are not reusuable, so
         //we create new instances as needed.
         task = new Task();
         task.addPropertyChangeListener(this);
         task.execute();
     }

where Task is a SwingWorker, which is just the easiest way of executing
code on a thread other than the EDT.  Please review that code for how to
write a SwingWorker (the Task class declaration is right there with the
code).
You sound like you are not a native English speaker; is that true? ....

But I'm not really sure, because
you explain it oddly.

Your terminology makes it difficult to understand you.
it is difficult to explain something one is new to, and something that
one is expanding exposure to. it is easy to express a finite lexicon
of proper terms once one's exposure is complete. in the process of
exposure it is a bit difficult. ie: it is very easy to get the correct
search results when the proper terms are used.
Off the top of my head, it sounds like you are trying to add a second
Window to the existing user interface. But I'm not really sure, because
you explain it oddly.
yes. this is what i am trying to do. a second window with a
JProgressBar.
as stated, i am not stuck on this solution, but it is just an
experiment i was trying which was unsuccessful.
 
N

Nigel Wade

it is difficult to explain something one is new to, and something that
one is expanding exposure to. it is easy to express a finite lexicon of
proper terms once one's exposure is complete. in the process of exposure
it is a bit difficult. ie: it is very easy to get the correct search
results when the proper terms are used.

I think you would benefit greatly from reading the fine Java Tutorial
series at
http://java.sun.com/docs/books/tutorial/index.html.

I think it's a great place for any self-taught learner to begin. It will
start you off in the right direction with the correct terminology and
methodology of Java.

With regard to your specific problem, you are (in common with most
beginners to the subject) having a problem getting to grips with the the
threads used within Java, and in particular threads used by Swing. This
is hardly surprising, threads are not a simple concept to understand, and
the event driven thread used by Swing doesn't make life simple. The above
mentioned tutorial should help you in this regard, in particular the
Swing tutorial:
http://java.sun.com/docs/books/tutorial/uiswing/TOC.html
which has a whole section on concurrency (threads). There is also a sub-
section about progress bars which should address your specific need, but
a basic understanding of concurrency in Swing will make reading that
section easier.
 
L

Lew

Nigel said:
I think you would benefit greatly from reading the fine Java Tutorial

Nice citation! ("Read the said:
series at <http://java.sun.com/docs/books/tutorial/index.html>.

I think it's a great place for any self-taught learner to begin. It will
start you off in the right direction with the correct terminology and
methodology of Java.

With regard to your specific problem, you are (in common with most
beginners to the subject) having a problem getting to grips with the the
threads used within Java, and in particular threads used by Swing. This
is hardly surprising, threads are not a simple concept to understand, and
the event driven thread used by Swing doesn't make life simple. The above
mentioned tutorial should help you in this regard, in particular the
Swing tutorial:
 <http://java.sun.com/docs/books/tutorial/uiswing/TOC.html>
which has a whole section on concurrency (threads). There is also a sub-
section about progress bars which should address your specific need, but
a basic understanding of concurrency in Swing will make reading that
section easier.

On threading issues in general, and Swing threading in particular, the
book /Java Concurrency in Practice/ by Brian Goetz, et al., is superb.

It's one of two books on Java on which one should spend their hard-
earned ducats, the other being /Effective Java/, 2nd ed., by Joshua
Bloch (who's also a co-author of JCIP).

GUIs in general, not just Swing, have to handle their actions in a
single thread to be safe and effective. See
<http://weblogs.java.net/blog/kgh/archive/2004/10/
multithreaded_t.html>
which is referenced from the tutorial to which Nigel linked.

RTFM (or as Nigel said, RTFJT).
 

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,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top