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]
# 1

Add p.revalidate() after the if-then-else clause in your ActionListener.

mbmerrilla at 2007-7-29 12:05:31 > top of Java-index,Desktop,Core GUI APIs...
# 2

Thanks for the help.It did work,but only once .I mean,when i click on show work radiobutton ,it properly added the button b1,and when i clicked show play rediobutton,it added b2 button,butthen again if i click on show work radiobutton,there is no change,but if i resize the window the change occurs,please help

thanks and regards

javapria at 2007-7-29 12:05:31 > top of Java-index,Desktop,Core GUI APIs...
# 3

1) Don't forget to use the "Code Formatting Tags",

see http://forum.java.sun.com/help.jspa?sec=formatting,

so the posted code retains its original formatting.

2) Don't forget to reply to all your postings when you get help:

http://forum.java.sun.com/thread.jspa?threadID=5190850

http://forum.java.sun.com/thread.jspa?threadID=5193820

3a) Use a CardLayout:

http://java.sun.com/docs/books/tutorial/uiswing/layout/card.html

3b) or you need to remove the original panel from the content pane before adding the new panel

camickra at 2007-7-29 12:05:31 > top of Java-index,Desktop,Core GUI APIs...
# 4

Thanks camickr and mbmerrill

Card layout is actually working fine.I have another problem now.I have created two JTree's(Custom and default).they are two different classes extending JPanel.In my first view i want default Jtree to be viewed when i click the radiobutton,like wise for the custom Jtree.Now the third view wants both default as well as Custom Jtree's.When I add these these panels ,the size of the Frame is becoming too big and its not according to the requirement.When I add single Jtree's ,the size is just fine.How do I restrict the size.I am Quite new to Swings ,so please do help

thanks

javapria at 2007-7-29 12:05:31 > top of Java-index,Desktop,Core GUI APIs...
# 5

Add the tree to a JScrollPane and add the scrollpane to the panel.

camickra at 2007-7-29 12:05:31 > top of Java-index,Desktop,Core GUI APIs...
# 6

Both the trees are added to JScrollPane,it's still not working

javapria at 2007-7-29 12:05:31 > top of Java-index,Desktop,Core GUI APIs...
# 7

If you need further help then you need to create a "Short, Self Contained, Compilable and Executable, Example Program (SSCCE)",

see http://homepage1.nifty.com/algafield/sscce.html,

that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

camickra at 2007-7-29 12:05:31 > top of Java-index,Desktop,Core GUI APIs...
# 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.

javapria at 2007-7-29 12:05:31 > top of Java-index,Desktop,Core GUI APIs...
# 9

1) Read reply 3. I asked for formatted code.

Its not!

2) Read reply 7. I asked for a SSCCE.

Its not!

camickra at 2007-7-29 12:05:31 > top of Java-index,Desktop,Core GUI APIs...
# 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

javapria at 2007-7-29 12:05:31 > top of Java-index,Desktop,Core GUI APIs...