Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Need a little bit of help stacking buttons  RSS feed

 
David Crane
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well I ended up sick today so I missed a class lesson so I do not know how to stack buttons so that once you click one it disappears and another one appears.

I'll be very grateful for help doing this.
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like you are talking about the CardLayout class. You can read the How to Use CardLayout lesson from the Java Tutorial to learn about it. And you can read more about it in the JavaDoc for the CardLayout.
 
David Crane
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
um not to be rude but that went over my head with a few feet to spare, is there a simpler way to do it?
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David,
Are you talking about swing and JButton? If yes the links by Mark should point you in the right direction.
Did you go through them?
 
David Crane
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
to be honest they're a bit too complicated for me, I've only been programming for about two weeks, and that just goes beyond what I've learned so far
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You never answered Maneesh's question.

David Crane wrote:to be honest they're a bit too complicated for me, I've only been programming for about two weeks, and that just goes beyond what I've learned so far

Then you may wish to not implement this feature and try for something a bit simpler.
 
David Crane
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes and I did in my second post ask if there was a simpler way to do it
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes and I did in my second post ask if there was a simpler way to do it


And I really don't think that there is. The simplest way to swap content in a Swing application is to use a CardLayout. The tutorial for this is quite good, and if you want to give it a go, then by all means try. If your attempt fails, then post it here with your question. If you feel that you even this attempt is too complex, then what I meant by my post above is that perhaps you will need to change what you want your program to do, to behave in a simpler fashion, not to code this swapping in a simpler way. Best of luck
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a classmate that attend the class you missed that might be able to provide you with notes? Or at least can give you a better general idea of what your teacher discussed?

Can't you contact the teacher? Most teacher's today do provide their e-mail addresses. (Or is this not a college class?)

If we at least had a good starting point to know what concepts or topics where discussed, we might be able to help better. If you have only been programming for two weeks, then I would agree that the CardLayout is a bit advanced for where you are likely at in your studies. At the same time, I cannot see a teacher discussing the topic that it sounds like you are describing in the second or third week of a course. And the topic/example you mentioned in your original post sounds like a classic example for discussing the CardLayout class. So we all may misunderstanding the problem/topic you are asking for guidance on.

What have you learned so far? That might also help us determine a starting point to help you.
 
David Crane
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is a high school class, and yea I'll go try to contact a class mate, thanks for the help
 
Rob Spoor
Sheriff
Posts: 20904
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With JButton there is one other way. Instead of swapping buttons, have one JButton and swap the Actions for it using setAction. The button will reflect the settings of the Action such as the name, icon, mnemonic, accelerator etc.
 
David Crane
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well I got some help from a classmate I used an absolute layout on the holder they were in and had it set so when I click one it disappears and the next one appears
 
Rob Spoor
Sheriff
Posts: 20904
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No offense, but a) using absolute layouts is horrible, and b) that hiding and showing is just what CardLayout does for you.

In short, you create one JPanel with a CardLayout. You add all the buttons to that JPanel. You then use the CardLayout's next method to hide the current button and show the next.
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We usually discuss such things on a different forum. Moving.
 
David Crane
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob I really don't care, I just started programming and it's what was taught in the day of class I missed
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> ..so be quiet please

that will no doubt impress all those trying to help you.

what response do you expect you'll get for your next problem?

*plonk*
 
Rob Spoor
Sheriff
Posts: 20904
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed. http://faq.javaranch.com/java/BeNice
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!