Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Swings newbee : Changing window components

 
shukla raghav
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can some one show me the code for a SIMPLE swing application window. were there are few components includign a button. Now on clicking the button i want to change the components on the window.I dont want old components i want new components.

Lets take an example

initially the window will have

Component - 1: a non editable JTextPane with a long string

Component - 2: a button that will convert entire string in JTextPane to uppercase and display

Component - 3: another button that will remove component -1 and Component-2 and show the length of the String as a label component (component) on the same window.

Its just like while installing an application we keep pressign "next" and we keep having different components on the window.Is it possible to have all the companents removed and just shows a message.

Please make it as simple and basic as possible without complications.I would be thankfull to all who help and contribute.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> Its just like while installing an application we keep pressign "next" and we keep having different components on the window.

your search keyword would be
CardLayout
 
shukla raghav
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand the use of cardlayout. But just being inquisitive is it possible to change the appearance of the same Frame by changing the components it has. I dont mean chaging the properties of compnents but changing the components as just described in the example i took.

I tried removing a component from JFrame using remove(Component c) method but the system hanged. i think there was a deadlock. So i just thought if its possible or not
 
shukla raghav
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I mean can i tell my actionPerformed() method to remove a few components from the JFrame like this.

 
Rob Camick
Ranch Hand
Posts: 2703
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I mean can i tell my actionPerformed() method to remove a few components from the JFrame like this.




No, because you didn't add the "jp" to the "jf". You added the "jp" to the "content", so you must remove it from the content.

In general when you add/remove individual components from a panel when the GUI is visible the code should be:



In order to invoke the revalidate() method you need to cast your "content" to a JPanel.

 
Rob Spoor
Sheriff
Posts: 20822
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JComponent is enough.
 
Can't .... do .... plaid .... So I did this tiny ad instead:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!