Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Radio Buttons with JScroll Pane

 
An Sush
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am new to Swing and want to write a simple panel using swing. The requiremnets are as follows:
1) will have 3 radio buttons
2) just below that have a scroll panel and based on which radio button is selected, show different text in the scroll panel.
Kindly help me with the code or guide me to some tutorial
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can add an action listener to the JRadioButtons. On getting notified of the action event, you can figure out which was selected and display the text accordingly.

To display the text, you need to have a JTextComponent (something like JTextField or JTextArea) wrapped inside the JScrollPane. The scroll pane in itself is of no use for you to display text.

In a nutshell you need to:
1) Create JRadioButton instances
2) Add an action listener while creating them.
3) Create some JTextComponent instance
4) Create a container for these components(typically some panel)
5) Add all the child components to the panel using your favorite layout manager
6) Put the panel in a JFrame and display it.
7) In the action listener, figure out which button was clicked and display the text accordingly.

For a comprehensive Swing tutorial check out http://java.sun.com/docs/books/tutorial/uiswing/
 
An Sush
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh yes i created action item, radio buttons and jscroll panel also. but m unable to set the layout for them
at parent level i have a JPanel (BorderLayout):

and i created new radio buttons:

and then added them to the JPanel:

similarly i added all the radio buttons and the scroll panel also
but on running the prog, i see only one radio button
in short m unable to set the layout/coordinates for the elements.
kindly help me with it
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You really need to go through http://java.sun.com/docs/books/tutorial/uiswing/layout/visual.html before attempting anything further.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic