Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTabbedpane with JRadiobutton?

 
Tran Tuan Hung
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 733
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic