Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Flow Logic in Swing  RSS feed

 
Al Wells
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This may be a beginner's question but it was suggested to me that I post here anyway. I am trying to get certain things to display on screen when true and other things to display when not true. Can you tell me why the if statement is not working and what I can do to fix it?

I do know that there are likely other problems with this code. If there is any feedback that you can give me about other problems that you see, I would appreciate that as well.

Thanks so much,
Al



[ May 22, 2005: Message edited by: Al Wells ]
[ May 23, 2005: Message edited by: Gregg Bolinger ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, you can't compare objects to 0 in Java. The value for "no object" is 'null', and boolean values are 'true' and 'false'. None of these are equivalent to ints the way they would be in C/C++. So the "logButton == 0" expressions are illegal, and I can't be sure what you mean.

Now. The way things work is that when the button is pressed, the actionPerformed() method of any ActionListeners that have been attached to a button are called. The action listener doesn't have to check any sort of button state or anything: if the listener is invoked, the button has been pressed.

Does this help?

There are some other issues. One is that the main class extends JFrame, and in fact, you create an instance of that JFrame subclass, but then you don't use it; you go and create yet another plain old JFrame and use that, instead. Roughly, the fix for this is to do away with the variable theScreen altogether! Just delete the line where you declare it, and every where you use it, just delete the word and the dot that follows it; the methods are then implicitly called on the "current instance" -- the object the methods belong to, which in this case is an instance of MainScreen.

Make sense?
 
Al Wells
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ernest.

That does make sense. What I am trying to do is to get the state of the screen to change. When the button is not pressed, I want it to be visible. When it is pressed, I want it to go away and to create a new panel object which will then be displayed to allow the user to log in (two text fields and a button). After log in, another object is created (in yet another class) that will check the log in success). That was the reason for the if and while statements.

The reason that I thought the button returned a boolean value of 1 or 0 was due to an example that I was looking at. In looking at it again, the if statement in that example was an ActionEvent getter (e.getSource in the example). It is all suddenly so clear... I'll fix that and then try again.

All the best,
Al
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!