aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JTabbedpane with JRadiobutton? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTabbedpane with JRadiobutton?" Watch "JTabbedpane with JRadiobutton?" New topic
Author

JTabbedpane with JRadiobutton?

Tran Tuan Hung
Ranch Hand

Joined: Apr 08, 2007
Posts: 59
I have a JTabbedpane with 2 tabs (tab1, tab2).
I have 2 radiobutton (JRadioButton).
So for now, i want to when i click on radiobutton1 it will be show tab1
when i click on radiobutton2 it will be show tab2.
Here is my source code, please help me to slove it:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;

public class JTablePaneTest extends JFrame implements ActionListener{
private JTabbedPane pane;
private JRadioButton radioButton1 = new JRadioButton("Radiobutton1", true);
private JRadioButton radioButton2 = new JRadioButton("Radiobutton2", false);
JPanel radioPanel =null;

public JTablePaneTest() {
super("TEST");
this.setLayout(new BorderLayout());
this.setSize(new Dimension(300,300));
this.getContentPane().add(this.getAllRadioButton(), BorderLayout.SOUTH);
this.getContentPane().add(this.getPane(), BorderLayout.CENTER);
this.pack();
this.setVisible(true);
}
private JPanel getAllRadioButton(){
if(radioPanel==null){
radioPanel = new JPanel();
radioPanel.setLayout(new FlowLayout());
radioPanel.setBorder(BorderFactory.createEmptyBorder());
ButtonGroup bg = new ButtonGroup();
bg.add(radioButton1);
bg.add(radioButton2);
radioPanel.add(radioButton1);
radioPanel.add(radioButton2);
}
return radioPanel;
}
private JTabbedPane getPane(){
if(pane == null){
pane = new JTabbedPane();
pane.addTab("Tab1", null, panel1(), "Tab1");
pane.addTab("Tab2", null, panel2(), "Tab2");
}
return pane;
}
private JPanel panel1(){
JPanel panel1 = new JPanel();
panel1.setLayout(new GridBagLayout());
panel1.add(new JButton("TEST1"));
return panel1;
}
private JPanel panel2(){
JPanel panel2 = new JPanel();
panel2.setLayout(new GridBagLayout());
panel2.add(new JTextField(12));
return panel2;
}
public static void main(String[] args) {
new JTablePaneTest();
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == radioButton1){
//show tab1
}
if(e.getSource() == radioButton2){
//show tab2
}
}
}

Thanks you very much
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 685
You'll need to read the API doc to see which listener to use with the RadioButtons. Then add the listener to the button and write the code for the listener.
Then read the API doc for the JTabbedPane class API for which method to call in your listener to select the tab you want to show.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Originally posted by Norm Radder:
You'll need to read the API doc to see which listener to use with the RadioButtons. Then add the listener to the button and write the code for the listener.
Then read the API doc for the JTabbedPane class API for which method to call in your listener to select the tab you want to show.


This is the same answer as he received in the Sun Java forums. There's a conspiracy here! ;-)
Tran Tuan Hung
Ranch Hand

Joined: Apr 08, 2007
Posts: 59
of couse i ask this question to some forum to quickly slove my problem. In the sun forum, i get the same answer, my helper tell me that read API and give me the link to read it (. But my opinion is help me the way to slove it. because i already read it.
thanks for reply.
[ September 01, 2008: Message edited by: Tran Tuan Hung ]
Tran Tuan Hung
Ranch Hand

Joined: Apr 08, 2007
Posts: 59
Now i already slove my problem, i call the setSelectedIndex(i-1) of JTabbedPane when i click my button.
Thanks for your help.
[ September 01, 2008: Message edited by: Tran Tuan Hung ]
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Originally posted by Tran Tuan Hung:
[QB]of couse i ask this question to some forum to quickly slove my problem. In the sun forum, i get the same answer, my helper tell me that read API and give me the link to read it (.

I think that was a very good answer. How do you expect to progress in this language if you don't learn to read the API? It's the parable about giving a man a fish vs teaching him how to fish: one feeds him for a day, the other teaches him to feed himself forever.
But my opinion is help me the way to slove it. because i already read it.
then your question should have included specifics about what in the API you did or didn't understand.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

Originally posted by Tran Tuan Hung:
of couse i ask this question to some forum to quickly slove my problem.

That's fine, but then please BeForthrightWhenCrosspostingToOtherSites.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36486
    
  16
Originally posted by pete stein:
This is the same answer as he received in the Sun Java forums.


That is because it was the correct answer.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTabbedpane with JRadiobutton?
 
Similar Threads
missing components
TabbedPane
Problem with the JButton.
JRadioButtons w/a JButton
change text on jtextfield from one class after clicking submit from another class