• Post Reply Bookmark Topic Watch Topic
  • New Topic

Cardlayout not changing using netbeans and static and non static models  RSS feed

 
rodolfo tuble
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings!

I am developing a system on which I will be displaying multiple frames depending on the situation, as multiple jFrames is a bad practice I've opted to try Cardlayout and simply manipulate the jPanels to be presented inside a frame.
The problem now is that in my quest to learn cardlayout all of the examples include an event trigger while in my case the change of the jPanel should occur inside an if/else condition not because of an event triggered by a button.
Since my if/else code is inside a static model I've decided to call the cardlayout class outside using a new function, but unfortunately it is not working, then I tried to check if the model is really called and by replacing everything inside with a print, it worked meaning the model call is working, but the code is not "I'm referring to the card layout". So may I ask anyone here what seems to be wrong with my code?

The timer is there to make sure that my system runs indefinitely.

Thank you

Code:

 
Rob Camick
Ranch Hand
Posts: 2756
12
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Crossposted: http://stackoverflow.com/questions/33480897/jpanel-layout-does-not-change-using-cardlayout-and-some-static-variable-issue#comment54768686_33480897

You still haven't used any advice given from the above link.

You are still posting code that we can't execute because you use classes that are not part of the JDK. We have no idea what your data is like so we can't replicate your data.

You have been told you do NOT need to use a static method. It was suggested you create the Timer in the class or create a separate class and pass a reference to the class so you can access the GUI

You have been told NOT so use a TimerTask and instead use a Swing Timer. Looking at your code a second time a TimerTask is probably more appropriate (since it runs on a separate Thread) and it appears you are probably using blocking code so you don't want to block the EDT which will prevent the GUI from responding to events. In any case the above suggestion still applies, there is no need for the static method. Just start the TimerTask in the constructor of your class.

The problem now is that in my quest to learn cardlayout all of the examples include an event trigger while in my case the change of the jPanel should occur inside an if/else condition not because of an event triggered by a button.


And the TimerTask (or Timer) is an event trigger. It is an event that is schedule to occur at a certain time period. So the event code is the same as if the user clicked a button. This means that any code that updates the GUI needs to be added to the EDT by using SwingUtilities.invokeLater(...). Read the section from the Swing Tutorial on Concurrency. Maybe the SwingWorker is more appropriate for you since it will create the Thread for you and you can just "publish your results to the GUI as they become available.

You still haven't posted a proper SSCCE. You say your code needs to execute on an if/else condition. So a proper SSCCE just simulates the condition. So in your TimerTask you generate a random number. "IF" the number is odd you display one panel, "ELSE" you display another panel. Again, the concept you are trying to learn is how to swap a panel on a generated event. Simplify the problem and we can give you concrete solutions.



Don't create a new GUI. You need a reference to the existing GUI so you can just swap panels.

Maybe you shouldn't even be using a CardLayout, because you will lose the information every time you check for new input. Maybe you should use a JTabbedPane so you can add a new tab every time you get data from the CardRead. This way you will have history of all the transactions.
 
rodolfo tuble
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:Crossposted: http://stackoverflow.com/questions/33480897/jpanel-layout-does-not-change-using-cardlayout-and-some-static-variable-issue#comment54768686_33480897

You still haven't used any advice given from the above link.

You are still posting code that we can't execute because you use classes that are not part of the JDK. We have no idea what your data is like so we can't replicate your data.

You have been told you do NOT need to use a static method. It was suggested you create the Timer in the class or create a separate class and pass a reference to the class so you can access the GUI

You have been told NOT so use a TimerTask and instead use a Swing Timer. Looking at your code a second time a TimerTask is probably more appropriate (since it runs on a separate Thread) and it appears you are probably using blocking code so you don't want to block the EDT which will prevent the GUI from responding to events. In any case the above suggestion still applies, there is no need for the static method. Just start the TimerTask in the constructor of your class.

The problem now is that in my quest to learn cardlayout all of the examples include an event trigger while in my case the change of the jPanel should occur inside an if/else condition not because of an event triggered by a button.


And the TimerTask (or Timer) is an event trigger. It is an event that is schedule to occur at a certain time period. So the event code is the same as if the user clicked a button. This means that any code that updates the GUI needs to be added to the EDT by using SwingUtilities.invokeLater(...). Read the section from the Swing Tutorial on Concurrency. Maybe the SwingWorker is more appropriate for you since it will create the Thread for you and you can just "publish your results to the GUI as they become available.

You still haven't posted a proper SSCCE. You say your code needs to execute on an if/else condition. So a proper SSCCE just simulates the condition. So in your TimerTask you generate a random number. "IF" the number is odd you display one panel, "ELSE" you display another panel. Again, the concept you are trying to learn is how to swap a panel on a generated event. Simplify the problem and we can give you concrete solutions.


I'm that desperate as I am already cracking my head with this problem, I want to clarify that I have just started with java again and I still don't remember everything from before, so in short I forgot.

What do you mean by classes that are not part of the JDK?

Also is SSCCE to post a summary of your code? I did include a bunch regarding UI since I thought they could be causing some of my problems, regarding the trigger being an event thank you for that, I've always thought of event triggers to being only mouse trigger, button trigger, or something you press in the UI.

I also tried using this but it still didn't work, I also tried placing the cardlayout code directly inside public MainUI() and of course it worked, though I change panel1, panel2 to card2, card3 as the are
the name of the cards, I got this while reading the document on cardlayout, but in some tutorials they use the variable names instead of the card names:



Also for the condition this is it:




Regarding this part this was the only way I found to call my methods inside a static method so I just went along with it.


I'm planning on stopping the process if an input is found, though I am still not sure how to proceed with that as I am still trying to solve this part. a tabbedpane would be nice but I want the UI to be the same with an ATM.

also to be really honest I don't know how to
start the TimerTask in the constructor of your class.


I did try moving it a while ago and it only gave me more problems so I gave up on the idea and just went on and explore changing the cardlayout function and manipulating jLabels to see if it will work.

p.s I am new here and to reply to a post do I really need to use quote? as it would make my reply look longer that usual or am I allowed to delete them?

Thank you
 
Rob Camick
Ranch Hand
Posts: 2756
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by classes that are not part of the JDK?


Well, the card reader classes are not part of the JDK documentation. Yes, the code compiles, but how many people will have a card reader or know how to use the code you posted? When I run the code it just continually displays "no terminal".

Also is SSCCE to post a summary of your code?


Did you read the link? It needs to be able to compile and execute and demonstrate the "concept" of the problem. Its should not be your actual application. It should be a simplification of the problem to show that you actually understand your question. I already suggested one way you can simulate the problem, simply by generating random numbers so that you can handle the "if/else" logic to swap panels.

I am new here and to reply to a post do I really need to use quote?


I only quote the direct text I'm responding to.

Again, unless I see a proper SCCE (without the card terminal classes), I can't suggest a concrete solution. You just have to look at any of the example from the Swing tutorial I have suggested to see that they do not use a static method to use a Timer.
 
rodolfo tuble
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:
What do you mean by classes that are not part of the JDK?


Well, the card reader classes are not part of the JDK documentation. Yes, the code compiles, but how many people will have a card reader or know how to use the code you posted? When I run the code it just continually displays "no terminal".

Also is SSCCE to post a summary of your code?


Did you read the link? It needs to be able to compile and execute and demonstrate the "concept" of the problem. Its should not be your actual application. It should be a simplification of the problem to show that you actually understand your question. I already suggested one way you can simulate the problem, simply by generating random numbers so that you can handle the "if/else" logic to swap panels.

I am new here and to reply to a post do I really need to use quote?


I only quote the direct text I'm responding to.

Again, unless I see a proper SCCE (without the card terminal classes), I can't suggest a concrete solution. You just have to look at any of the example from the Swing tutorial I have suggested to see that they do not use a static method to use a Timer.


I'll just continue using full quote, ooh so that's what SCCE meant. Okay let me post it differently, also I was able to solve it.

I just transferred the timer from main to my MainUI class, the reason why it was at main was because I've always thought that main holds the stuff that needs to be executed or where you process data, until recently I discovered that it should only be used to display the GUI.

I wished someone pointed this out earlier so that I wouldn't have wasted this much time on it.

Though if you feel that it still needs improvement feel free to say so as I might have done something which is not considered a good practice.


 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rodolfo tuble wrote:. . .
I'm that desperate as I am already cracking my head with this problem, I want to clarify that I have just started with java again and I still don't remember everything from before, so in short I forgot.
In which case take it easy. You have asked for help and been given it.
What do you mean by classes that are not part of the JDK?
Classes not to be found in the standard Java® API documentation. Classes you have written yourself.
Also is SSCCE to post a summary of your code? . . .
No. Look it up here.
p.s I am new here and to reply to a post do I really need to use quote? as it would make my reply look longer that usual or am I allowed to delete them?

Thank you
I think you should only quote small parts of the previous post which you are answering, not the whole post. You can delete parts of your post before it is posted but not after it has a reply.

And welcome to the Ranch
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rodolfo tuble wrote:. . .
I'll just continue using full quote
Please don't.
, ooh so that's what SCCE meant. Okay let me post it differently, also I was able to solve it.
. . .
That does not look like an SSCCE to me.
 
rodolfo tuble
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:That does not look like an SSCCE to me.


Okay I won't quote everything,

I thought that SSCCE is your code that is simplified and can be compiled, so I left the UI part intact, well most of those are the code netbeans created for the UI.
 
Rob Camick
Ranch Hand
Posts: 2756
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just transferred the timer from main to my MainUI class, the reason why it was at main was because I've always thought that main holds the stuff that needs to be executed or where you process data, until recently I discovered that it should only be used to display the GUI.

I wished someone pointed this out earlier so that I wouldn't have wasted this much time on it.


I did suggest that several times both in this posting and your other posting. I suggest to get rid of the static code and just start the Timer in the constructor.

ooh so that's what SCCE meant. Okay let me post it differently, also I was able to solve it.


Yes, that is why you were given a link. The point of a SSCCE is to realize what is relevant to the stated problem (in this case swapping panels). By simplifying the problem you often solve the problem and if you don't you have code to post in the forum.

if you feel that it still needs improvement feel free to say so


Yes, most of the code is IDE generated which is why we don't like all the code. Again, you can't see the important code when it is hidden by the generated code. You could have manually create a frame with a panel in about 10 lines of code.

Also, the code still doesn't demonstrate your problem of swapping panels based on data. For example as I suggested your could generate a random number (to represent your card reader data) and then do processing based on the random value. Once you get the basic approach working your replace the random data with actual data and it should still work.
 
rodolfo tuble
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:I did suggest that several times both in this posting and your other posting. I suggest to get rid of the static code and just start the Timer in the constructor.


I'm sorry as I didn't understand what it meant by then, and I did not really knew you could do that, as I've also said I've always taught of the main to hold to processing. Which is big mistake in my part and I apologise for it, Also, what do you mean when you say constructor in Java?


Yes, most of the code is IDE generated which is why we don't like all the code. Again, you can't see the important code when it is hidden by the generated code. You could have manually create a frame with a panel in about 10 lines of code.


Does hardcoding the UI offers the same ease when using a GUI builder where you can specify the properties easily, It is true that a bunch of these are wasted code it's just that I'm used to using netbeans. I would try hardcoding the UI to give it a shot, Do you know of any easily understood tutorials that offer rich examples. It would be greatly appreciated. thank you
 
Rob Camick
Ranch Hand
Posts: 2756
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you know of any easily understood tutorials that offer rich examples.


I have referred you to the Swing tutorial several times in my answer. The reason refer to the tutorial rather than give specific code is to force you to read the tutorial and then hopefully look at the table of contents for all the other basics of Swing. You should keep the tutorial link handy.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!