Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Swing / AWT / SWT and the fly likes JPanel question ... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JPanel question ..." Watch "JPanel question ..." New topic
Author

JPanel question ...

tadi raja
Ranch Hand

Joined: Jul 07, 2008
Posts: 109
can i replace existing panel with new panel in JFrame ??
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10167
    
    8

Originally posted by tadi raja:
can i replace existing panel with new panel in JFrame ??


"Tadi Raja"
I could answer this with a simple yes, but wont even bother, because you will then ask how.

Please refer to my answer to your very first post

Like I have mentioned, you need to show some efforts. It is OK to make mistakes and end up baffled. That is how you learn. Each and every member of this forum has been through that phase before and believe me this approach is the best.

So why dont you try out something on your own and post the code here if you run into some problem?
Believe me the ranchers will be more than happy to help you fix it. You might just pick up some pointers too...and all this for free! Amazing isnt it?

Waiting for your code snippets. (Dont forget to use the CODE tags!)


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
tadi raja
Ranch Hand

Joined: Jul 07, 2008
Posts: 109
Here is the code ...

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class PanelDemo1 extends JFrame implements ActionListener
{
JTabbedPane tab;
CardLayout card;
JTextField tf1,tf2;
JLabel lb1,lb2;
JCheckBox c1,c2;
JPanel mainPanel;
JButton next,prev;
public PanelDemo1()
{
super("panel demo");

tab = new JTabbedPane();
tab.add("First Tab",firstPanel());
tab.add("Second Tab",secondPanel());

card = new CardLayout();

mainPanel = new JPanel();
mainPanel.setLayout(card);

getContentPane().add(tab);
// getContentPane().add(mainPanel);
}

public JPanel firstPanel()
{
JPanel pn1 = new JPanel();

tf1 = new JTextField(12);
tf2 = new JTextField(12);
pn1.add(tf1);
pn1.add(tf2);

next = new JButton();
next.setText("next");
next.addActionListener(this);
pn1.add(next);

return pn1;
}

public JPanel hidingPanel()
{
JPanel pn3 = new JPanel();

JButton btn = new JButton();

btn.setText("back");
btn.addActionListener(this);
pn3.add(btn);

return pn3;
}

public JPanel secondPanel()
{
JPanel pn2 = new JPanel();

lb1 = new JLabel("label1");
lb2 = new JLabel("label2");

prev = new JButton();

prev.setText("prev");
prev.addActionListener(this);
pn2.add(prev);

pn2.add(lb1);
pn2.add(lb2);
return pn2;
}

public void actionPerformed(ActionEvent ae)
{
if(ae.getActionCommand() == "next")
{
mainPanel.add(hidingPanel());
card.next(mainPanel);
}

}

public static void main(String[] args)
{
PanelDemo1 pd = new PanelDemo1();
pd.setSize(500,400);
pd.setVisible(true);
pd.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

if we click on next button i got this exception ...

java.lang.IllegalArgumentException: cannot add to layout: constraint must be a string

at java.awt.CardLayout.addLayoutComponent(CardLayout.java:190)

at java.awt.Container.addImpl(Container.java:664)

at java.awt.Container.add(Container.java:307)

at PanelDemo1.actionPerformed(PanelDemo1.java:84)

at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1786)

at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.java:1839)

at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)

at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)

at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:245)

at java.awt.Component.processMouseEvent(Component.java:5100)

at java.awt.Component.processEvent(Component.java:4897)

at java.awt.Container.processEvent(Container.java:1569)

at java.awt.Component.dispatchEventImpl(Component.java:3615)

at java.awt.Container.dispatchEventImpl(Container.java:1627)

at java.awt.Component.dispatchEvent(Component.java:3477)

at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:3483)

at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3198)

at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3128)

at java.awt.Container.dispatchEventImpl(Container.java:1613)

at java.awt.Window.dispatchEventImpl(Window.java:1606)

at java.awt.Component.dispatchEvent(Component.java:3477)

at java.awt.EventQueue.dispatchEvent(EventQueue.java:456)

at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10167
    
    8

To start with, please use code tags. You click the CODE button at the bottom and place your code inside the tags. This makes it much easier for others to read.

Now to your actual code.
What exactly are you trying to achieve here?

Do you want to display a new tab to the tabbed pane? If yes, you need to use JTabbedPane#addTab in your actionPerformed.

Do you want to replace the tabbed pane with the hidingPanel? If yes, you need to do something like this:


Notice the code formatting with the code tags


There are some other problems with your code too like naming conventions (e.g. hidingPanel should be called getHidingPanel and access modifiers (all your instance variables are default) and other things such as

You should be using

Let us do one thing at a time. So what exactly are you trying to do with your code?
tadi raja
Ranch Hand

Joined: Jul 07, 2008
Posts: 109
Thank you for your earlier response . now i got the solution
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10167
    
    8

Anytime!
tadi raja
Ranch Hand

Joined: Jul 07, 2008
Posts: 109
I got the exact solution but i want TabbedPane as it is ..ie TabbedPane is not removed when an event fires ....
tadi raja
Ranch Hand

Joined: Jul 07, 2008
Posts: 109
In the same programmme can i don't hide the TabbedPane even if i clicked on Button . ie TabbedPane not hiding .... can you please give me solution for this .
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JPanel question ...
 
Similar Threads
WA #1.....word association
changing sizes in GridLayout
Where is the Plugin Console?
Over writting JA\Panel content
how to add background image to applet