JFrame issue
Hi,
I am creating a JFrame and adding two panels using box layout.One panel consists of radioibuttons .When I click on one radio button,the action to be performed is to add a button object to the other panel.when I tried to do this,it worked with out any errors,but the problem is that i can see the button only if I minimize and maximize it back.How do i solve this,gere is my code,
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class TestFrame extends JFrame
{
JPanel p=new JPanel();
JButton b1=new JButton("please work");
JButton b2=new JButton("plaese play");
TestFrame()
{
// p=new JPanel();
getContentPane().setLayout(new BoxLayout(getContentPane(),BoxLayout.Y_AXIS));
JPanel radioPanel=new JPanel();
//p.add(new JButton("please work"));
// b1=new JButton("please work");
// b2=new JButton("plaese play");
JRadioButton r1=new JRadioButton("show work");
r1.setActionCommand("show work");
JRadioButton r2=new JRadioButton("show play");
r2.setActionCommand("show play");
ButtonGroup group=new ButtonGroup();
group.add(r1);
group.add(r2);
radioPanel.add(r1);
radioPanel.add(r2);
r1.addActionListener(new MyRadioListener());
r2.addActionListener(new MyRadioListener());
getContentPane().add(p);
//p.add(b1);
getContentPane().add(radioPanel);
pack();
//setVisible(true);
//p.add(b1);
}
class MyRadioListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String command=e.getActionCommand();
if(command.equals("show work"))
{
System.out.println("fhssjkhfks");
p.add(b1);
//getContentPane().add(p);
}
else if(command.equals("show play"))
{
p.remove(b1);
p.add(b2);
}
}
}
[1941 byte] By [
javapria] at [2007-11-27 10:56:52]

# 8
OK,let me send the code ,
[code]
//construtor initialization
JPanel labelPanelDef=new JPanel();
labelPanelDef.setLayout(new BoxLayout(labelPanelDef,BoxLayout.Y_AXIS));
l1=new JLabel("Chat Rooms");
labelPanelDef.add(l1);
tree=new MyTreeDefault();
labelPanelDef.add(tree);
System.out.println(labelPanelDef.getSize());
JPanel labelPanelCus=new JPanel();
labelPanelCus.setLayout(new BoxLayout(labelPanelCus,BoxLayout.Y_AXIS));
JLabel l=new JLabel("Chat Rooms");
labelPanelCus.add(l);
tree1=new MyTreeDefault1();
labelPanelCus.add(tree1);
JPanel labelPanelDefCus=new JPanel();
labelPanelDefCus.setLayout(new BoxLayout(labelPanelDefCus,BoxLayout.Y_AXIS));
JLabel ld=new JLabel("Chat Rooms");
labelPanelDefCus.add(ld);
//tree1=new MyTreeDefault1();
labelPanelDefCus.add(tree);
labelPanelDefCus.add(tree1);
cards=new JPanel(new CardLayout());
cards.add(labelPanelDef,"Show Default chat rooms only");
cards.add(labelPanelCus,"show Custom chat rooms only");
cards.add(labelPanelDefCus,"show default and custom chat rooms");
JPanel labelPanel2=new JPanel();
l2=new JLabel("Room Description");
labelPanel2.add(l2);
desc=new JTextArea(10,20);
desc.setLineWrap(true);
desc.setWrapStyleWord(true);
desc.setEditable(false);
JScrollPane scroll=new JScrollPane(desc);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
labelPanel2.add(scroll);
labelPanel2.setLayout(new BoxLayout(labelPanel2,BoxLayout.Y_AXIS));
join=new JButton("join chat rooms");
labelPanel2.add(join);
JPanel customPanel=new JPanel();
l3=new JLabel("Create Custom Chat Room");
customPanel.add(l3);
customPanel.setLayout(new BoxLayout(customPanel,BoxLayout.Y_AXIS));
JPanel textPanel=new JPanel();
textPanel.setLayout(new BoxLayout(textPanel,BoxLayout.X_AXIS));
l4=new JLabel("chat Room Name:");
textPanel.add(l4);
customName=new JTextField(20);
textPanel.add(customName);
customPanel.add(textPanel);
l5=new JLabel("Chat Room Description");
customPanel.add(l5);
custom=new JTextArea(10,20);
JScrollPane scroller=new JScrollPane(custom);
scroller.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scroller.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
customPanel.add(scroller);
joinCustom=new JButton("Create Custom Chat Room");
customPanel.add(joinCustom);
JPanel radioPanel=new JPanel();
radioPanel.setLayout(new BoxLayout(radioPanel,BoxLayout.X_AXIS));
JRadioButton defaultchat=new JRadioButton("Show Default chat rooms only",true);
defaultchat.setActionCommand("Show Default chat rooms only");
JRadioButton customchat=new JRadioButton("show Custom chat rooms only");
customchat.setActionCommand("show Custom chat rooms only");
JRadioButton defcuschat=new JRadioButton("show default and custom chat rooms");
defcuschat.setActionCommand("show default and custom chat rooms");
ButtonGroup group=new ButtonGroup();
group.add(defaultchat);
group.add(customchat);
group.add(defcuschat);
radioPanel.add(defaultchat);
radioPanel.add(customchat);
radioPanel.add(defcuschat);
defaultchat.addActionListener(new MyRadioListener());
customchat.addActionListener(new MyRadioListener());
defcuschat.addActionListener(new MyRadioListener());
labelPanel2.add(customPanel);
JPanel view=new JPanel();
view.setLayout(new BoxLayout(view,BoxLayout.X_AXIS));
//view.add(labelPanel1);
view.add(cards);
view.add(labelPanel2);
getContentPane().setLayout(new BoxLayout(getContentPane(),BoxLayout.Y_AXIS));
getContentPane().add(view);
getContentPane().add(radioPanel);
pack();
setVisible(true);
}
class MyRadioListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
CardLayout c=(CardLayout)(cards.getLayout());
String command=e.getActionCommand();
c.show(cards,command);
}
}
[/code
here tree and tree1 are default and custom trees and they aer in JScrollPanes.
# 10
let me redo it properly this time,
JPanel labelPanelDef=new JPanel();
labelPanelDef.setLayout(new BoxLayout(labelPanelDef,BoxLayout.Y_AXIS));
l1=new JLabel("Chat Rooms");
labelPanelDef.add(l1);
tree=new MyTreeDefault();
labelPanelDef.add(tree);
JPanel labelPanelCus=new JPanel();
labelPanelCus.setLayout(new BoxLayout(labelPanelCus,BoxLayout.Y_AXIS));
JLabel l=new JLabel("Chat Rooms");
labelPanelCus.add(l);
tree1=new MyTreeDefault1();
labelPanelCus.add(tree1);
JPanel labelPanelDefCus=new JPanel();
labelPanelDefCus.setLayout(new BoxLayout(labelPanelDefCus,BoxLayout.Y_AXIS));
JLabel ld=new JLabel("Chat Rooms");
labelPanelDefCus.add(ld);
[b]labelPanelDefCus.add(tree);
labelPanelDefCus.add(tree1); cards=new JPanel(new CardLayout());
cards.add(labelPanelDef,"Show Default chat rooms only");
cards.add(labelPanelCus,"show Custom chat rooms only");
cards.add(labelPanelDefCus,"show default and custom chat rooms");
//other components created