hi guys,
I have a program using a JTabbedPane where I 3 classes, 2 are JPanels and one is a JFrame which contains a JTabbedPane. I am trying to swap one JPanel when a button is clicked. The first JPanel is a login and when user is verified I have removed it and I am trying to replace it with another JPanel and at the same time trying to enable the tabs as they are disabled when the program starts. I have tried everything but I am having difficulties. Any suggests would be much appreciated. I just need pointing in the right direction.

Can we see what you've tried?

Otherwise a simple remove and add on the JTabbedPane (its remove and add as pertaining to it's tabs, or place another panel on the tab in question and place your login panel in that panel and remove and add from that panel) and don't forget to call validate and/or repaint on the JTabbedPane (or the component that contains that JTabbedPane).

sounds like you want to use a card layout? (its like a tabbed pane but without tabs, ie no default user interface) so when the user clicks on "login" you can programatically switch to the card showing the JPanel/whatever else you want.

Or you could allways just remove(mypanel) then add(myotherpanel), methods along those lines exist

hey guys.
Thanks for the response. I dont want to use a cardlayout because I am removing it from the JTabbedPane and adding the JPanel unto a JPanel thats to the side of the JTabbedPane. The remove is working I did try adding it but nothing happened. Here is my code. The DDLoginInterface id where I remove the JPanel and add the other one by calling the method getJPanel from the DDMainMenu class and I also try to enable the tabs which doesnt happen. I did post the other class cause its just a Panel with components on it. Thanks again

package DDFinal.Interface;

import DDFinal.method.*;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DDLoginInterface.java
 *
 * Created on 04-Nov-2010, 15:21:57
 */
/**
 *
 * @author msamuelm
 */
public class DDLoginInterface extends javax.swing.JPanel {

    private Staff staff = new Staff();
    private DDLogin ddl = new DDLogin();
    private DDMainMenu mainMenu;//= new DDMainMenu();
    private boolean enable = false;

    /** Creates new form DDLoginInterface */
    public DDLoginInterface() {
        initComponents();
    }

    public JPanel getPanel() {
        JPanel pane = new DDLoginInterface();
        return pane;
    }

    
    /** This method is called from within the constructor to
     * initialisAe the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        username = new javax.swing.JLabel();
        password = new javax.swing.JLabel();
        usernameF = new javax.swing.JTextField();
        login = new javax.swing.JButton();
        clear = new javax.swing.JButton();
        passwordF = new javax.swing.JPasswordField();

        setBackground(new java.awt.Color(204, 204, 204));
        setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED, new java.awt.Color(153, 153, 153), new java.awt.Color(0, 153, 255), new java.awt.Color(204, 204, 204), new java.awt.Color(204, 204, 204)));
        setLayout(null);

        jLabel1.setText("Please type your username and password to login");
        add(jLabel1);
        jLabel1.setBounds(60, 70, 312, 15);

        username.setText("Username");
        add(username);
        username.setBounds(61, 132, 62, 15);

        password.setText("Password");
        add(password);
        password.setBounds(61, 190, 60, 15);
        add(usernameF);
        usernameF.setBounds(154, 127, 88, 19);

        login.setText("Login");
        login.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loginActionPerformed(evt);
            }
        });
        add(login);
        login.setBounds(60, 240, 70, 25);

        clear.setText("Clear");
        clear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clearActionPerformed(evt);
            }
        });
        add(clear);
        clear.setBounds(150, 240, 70, 25);
        add(passwordF);
        passwordF.setBounds(150, 190, 109, 19);
    }// </editor-fold>                        

    private void loginActionPerformed(java.awt.event.ActionEvent evt) {                                      
        if (evt.getSource() == login) {
            if (usernameF.getText().length() == 0
                    && passwordF.getText().length() == 0) {
                JOptionPane.showMessageDialog(null, "Please make sure, that a "
                        + "username and password have been entered ");

            } else if (ddl.loggin(usernameF.getText(), passwordF.getText())) {
                 JOptionPane.showMessageDialog(null, "You have logged in sucessfully");
                //staff.searchStaff(usernameF.getText());
                getPanel().removeAll();
                  this.setVisible(false);
                mainMenu = new DDMainMenu();
                DDSearchInterface ddSearch= new DDSearchInterface();
                mainMenu.getPanel.add(ddSearch);
                mainMenu.getPane().setEnabled(true);
                System.out.println("hello actionPerformed 4");
               
            } else {
                JOptionPane.showMessageDialog(null, "Incorrect username or password. Please check your username and password or speak to your"
                        + "systems administrator ");
            }

        }
    }                                     

    private void clearActionPerformed(java.awt.event.ActionEvent evt) {                                      
        passwordF.setText(null);
        usernameF.setText(null);
    }                                     
    // Variables declaration - do not modify                     
    private javax.swing.JButton clear;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JButton login;
    private javax.swing.JLabel password;
    private javax.swing.JPasswordField passwordF;
    private javax.swing.JLabel username;
    private javax.swing.JTextField usernameF;
    // End of variables declaration                   
}
package DDFinal.Interface;


import DDFinal.Interface.DDLoginInterface;
import javax.swing.*;


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DDMainMenu.java
 *
 * Created on 04-Nov-2010, 13:21:08
 */



/**
 *
 * @author msamuelm
 */
public class DDMainMenu extends javax.swing.JFrame {
    private DDLoginInterface ddl= new DDLoginInterface();
    private DDSearchInterface ddSearch= new DDSearchInterface();

    /** Creates new form DDMainMenu */
    public DDMainMenu() {
        initComponents();
        ddPane.setEnabled(false);
        searchPane.setViewportView(ddl);
       // searchPane.setViewportView(ddSearch);

        
    }

    public JScrollPane getScrollPane (){
        return searchPane;
    }
    public JTabbedPane getPane(){
        return ddPane;
    }
    

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        search = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        ddPane = new javax.swing.JTabbedPane();
        searchPane = new javax.swing.JScrollPane();
        searchTab = new javax.swing.JTextPane();
        salesPane = new javax.swing.JScrollPane();
        salesTab = new javax.swing.JTextPane();
        refundPane = new javax.swing.JScrollPane();
        refundTab = new javax.swing.JTextPane();
        adminPane = new javax.swing.JScrollPane();
        adminTab = new javax.swing.JTextPane();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Delux Decor Sales System");
        setBackground(java.awt.Color.lightGray);

        search.setBackground(new java.awt.Color(204, 204, 204));
        search.setBorder(new javax.swing.border.MatteBorder(null));
        search.setLayout(null);

        jLabel2.setIcon(new javax.swing.ImageIcon("/home/msamuelm/Desktop/house.jpeg")); // NOI18N
        jLabel2.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED, new java.awt.Color(204, 0, 0), new java.awt.Color(204, 0, 51), new java.awt.Color(153, 153, 153), new java.awt.Color(153, 153, 153)));
        search.add(jLabel2);
        jLabel2.setBounds(10, 20, 103, 67);

        jLabel1.setFont(new java.awt.Font("URW Chancery L", 1, 36)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(51, 51, 255));
        jLabel1.setText("Decor Delux");
        jLabel1.setBorder(javax.swing.BorderFactory.createEtchedBorder(new     java.awt.Color(102, 0, 0), new java.awt.Color(102, 102, 102)));
        search.add(jLabel1);
        jLabel1.setBounds(110, 40, 220, 45);

        searchTab.setBackground(new java.awt.Color(204, 204, 204));
        searchPane.setViewportView(searchTab);

        ddPane.addTab("Search", searchPane);

        salesTab.setBackground(new java.awt.Color(204, 204, 204));
        salesPane.setViewportView(salesTab);

        ddPane.addTab("Sales Transaction", salesPane);

        refundTab.setBackground(new java.awt.Color(204, 204, 204));
        refundPane.setViewportView(refundTab);

        ddPane.addTab("Refund Transaction", refundPane);

        adminTab.setBackground(new java.awt.Color(204, 204, 204));
        adminPane.setViewportView(adminTab);

        ddPane.addTab("Administration", adminPane);

        search.add(ddPane);
        ddPane.setBounds(20, 100, 1600, 840);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(search, javax.swing.GroupLayout.PREFERRED_SIZE, 1659, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(40, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(search, javax.swing.GroupLayout.PREFERRED_SIZE, 959, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(217, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new DDMainMenu().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JScrollPane adminPane;
    private javax.swing.JTextPane adminTab;
    private javax.swing.JTabbedPane ddPane;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane refundPane;
    private javax.swing.JTextPane refundTab;
    private javax.swing.JScrollPane salesPane;
    private javax.swing.JTextPane salesTab;
    private javax.swing.JPanel search;
    private javax.swing.JScrollPane searchPane;
    private javax.swing.JTextPane searchTab;
    // End of variables declaration                   

}

sorry Please ignore the above i copied the wrong code!!!!!!!

sorry. i have a swapPanel method in the DDLoginInterface which which removes. suppose to add and enable the JTabbedPane when the Login button is clicked. but only the remove works

package DDFinal.Interface;


import DDFinal.Interface.DDLoginInterface;
import javax.swing.*;


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DDMainMenu.java
 *
 * Created on 04-Nov-2010, 13:21:08
 */



/**
 *
 * @author msamuelm
 */
public class DDMainMenu extends javax.swing.JFrame {
    private DDLoginInterface ddl= new DDLoginInterface();
    private DDSearchInterface ddSearch= new DDSearchInterface();

    /** Creates new form DDMainMenu */
    public DDMainMenu() {
        initComponents();
        ddPane.setEnabled(false);
        searchPane.setViewportView(ddl);
        //sidePanel.add(ddSearch);
        //ddSearch.setBounds(5, 10, 230,250);
        //searchPane.setViewportView(ddSearch);

        
    }

    public JScrollPane getScrollPane (){
        return searchPane;
    }
    public JTabbedPane getPane(){
        return this.ddPane;
    }

    public JPanel getSidePanel(){

        return sidePanel;
    }


    


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        search = new javax.swing.JPanel();
        ddPane = new javax.swing.JTabbedPane();
        searchPane = new javax.swing.JScrollPane();
        searchTab = new javax.swing.JTextPane();
        salesPane = new javax.swing.JScrollPane();
        salesTab = new javax.swing.JTextPane();
        refundPane = new javax.swing.JScrollPane();
        refundTab = new javax.swing.JTextPane();
        adminPane = new javax.swing.JScrollPane();
        adminTab = new javax.swing.JTextPane();
        logoPanel = new javax.swing.JPanel();
        logoImage = new javax.swing.JLabel();
        compantName = new javax.swing.JLabel();
        sidePanel = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Delux Decor Sales System");
        setBackground(java.awt.Color.lightGray);

        search.setBorder(new javax.swing.border.MatteBorder(null));
        search.setLayout(null);

        searchTab.setBackground(new java.awt.Color(204, 204, 204));
        searchTab.setSelectionColor(new java.awt.Color(255, 153, 0));
        searchPane.setViewportView(searchTab);

        ddPane.addTab("Search", searchPane);

        salesTab.setBackground(new java.awt.Color(204, 204, 204));
        salesPane.setViewportView(salesTab);

        ddPane.addTab("Sales Transaction", salesPane);

        refundTab.setBackground(new java.awt.Color(204, 204, 204));
        refundPane.setViewportView(refundTab);

        ddPane.addTab("Refund Transaction", refundPane);

        adminTab.setBackground(new java.awt.Color(204, 204, 204));
        adminPane.setViewportView(adminTab);

        ddPane.addTab("Administration", adminPane);

        search.add(ddPane);
        ddPane.setBounds(250, 110, 1370, 830);

        logoPanel.setBackground(new java.awt.Color(204, 204, 204));
        logoPanel.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED, new java.awt.Color(255, 204, 0), new java.awt.Color(255, 204, 0), new java.awt.Color(0, 0, 0), new java.awt.Color(0, 0, 0)));

        logoImage.setIcon(new javax.swing.ImageIcon("/home/msamuelm/Desktop/house.jpeg")); // NOI18N
        logoImage.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED, new java.awt.Color(255, 204, 0), new java.awt.Color(255, 204, 0), new java.awt.Color(0, 0, 0), new java.awt.Color(0, 0, 0)));

        compantName.setFont(new java.awt.Font("URW Chancery L", 1, 36)); // NOI18N
        compantName.setForeground(new java.awt.Color(0, 0, 204));
        compantName.setText("Decor Delux");
        compantName.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED, new java.awt.Color(255, 204, 0), new java.awt.Color(255, 204, 0), java.awt.Color.black, new java.awt.Color(0, 0, 0)));

        javax.swing.GroupLayout logoPanelLayout = new javax.swing.GroupLayout(logoPanel);
        logoPanel.setLayout(logoPanelLayout);
        logoPanelLayout.setHorizontalGroup(
            logoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(logoPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(logoImage)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(compantName, javax.swing.GroupLayout.PREFERRED_SIZE, 220, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(1275, Short.MAX_VALUE))
        );
        logoPanelLayout.setVerticalGroup(
            logoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(logoPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(logoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(compantName, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(logoImage))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        search.add(logoPanel);
        logoPanel.setBounds(10, 20, 1620, 90);

        sidePanel.setBackground(new java.awt.Color(204, 204, 204));
        sidePanel.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED, new java.awt.Color(255, 204, 0), new java.awt.Color(255, 204, 0), java.awt.Color.black, new java.awt.Color(0, 0, 0)));

        javax.swing.GroupLayout sidePanelLayout = new javax.swing.GroupLayout(sidePanel);
        sidePanel.setLayout(sidePanelLayout);
        sidePanelLayout.setHorizontalGroup(
            sidePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 236, Short.MAX_VALUE)
        );
        sidePanelLayout.setVerticalGroup(
            sidePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 816, Short.MAX_VALUE)
        );

        search.add(sidePanel);
        sidePanel.setBounds(10, 120, 240, 820);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(search, javax.swing.GroupLayout.PREFERRED_SIZE, 1659, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(40, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(search, javax.swing.GroupLayout.PREFERRED_SIZE, 959, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(217, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new DDMainMenu().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JScrollPane adminPane;
    private javax.swing.JTextPane adminTab;
    private javax.swing.JLabel compantName;
    private javax.swing.JTabbedPane ddPane;
    private javax.swing.JLabel logoImage;
    private javax.swing.JPanel logoPanel;
    private javax.swing.JScrollPane refundPane;
    private javax.swing.JTextPane refundTab;
    private javax.swing.JScrollPane salesPane;
    private javax.swing.JTextPane salesTab;
    private javax.swing.JPanel search;
    private javax.swing.JScrollPane searchPane;
    private javax.swing.JTextPane searchTab;
    private javax.swing.JPanel sidePanel;
    // End of variables declaration                   

}
package DDFinal.Interface;

import DDFinal.method.*;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DDLoginInterface.java
 *
 * Created on 04-Nov-2010, 15:21:57
 */
/**
 *
 * @author msamuelm
 */
public class DDLoginInterface extends javax.swing.JPanel {

    private Staff staff = new Staff();
    private DDLogin ddl = new DDLogin();
    private DDMainMenu mainMenu;//= new DDMainMenu();
    private DDSearchInterface ddSearch;

    private boolean enable = false;

    /** Creates new form DDLoginInterface */
    public DDLoginInterface() {
        initComponents();
    }

    public JPanel getPanel() {
        JPanel pane = new DDLoginInterface();
        return pane;
    }


    /** This method is called from within the constructor to
     * initialisAe the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        username = new javax.swing.JLabel();
        password = new javax.swing.JLabel();
        usernameF = new javax.swing.JTextField();
        login = new javax.swing.JButton();
        clear = new javax.swing.JButton();
        passwordF = new javax.swing.JPasswordField();

        setBackground(new java.awt.Color(204, 204, 204));
        setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED, new java.awt.Color(255, 204, 0), new java.awt.Color(255, 204, 0), new java.awt.Color(0, 0, 0), new java.awt.Color(0, 0, 0)));
        setLayout(null);

        jLabel1.setText("Please type your username and password to login");
        add(jLabel1);
        jLabel1.setBounds(60, 70, 312, 15);

        username.setText("Username");
        add(username);
        username.setBounds(61, 132, 62, 15);

        password.setText("Password");
        add(password);
        password.setBounds(61, 190, 60, 15);
        add(usernameF);
        usernameF.setBounds(154, 127, 88, 19);

        login.setText("Login");
        login.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loginActionPerformed(evt);
            }
        });
        add(login);
        login.setBounds(60, 240, 70, 25);

        clear.setText("Clear");
        clear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clearActionPerformed(evt);
            }
        });
        add(clear);
        clear.setBounds(150, 240, 70, 25);
        add(passwordF);
        passwordF.setBounds(150, 190, 109, 19);
    }// </editor-fold>                        

    public void swapPanel(){
                //staff.searchStaff(usernameF.getText());
                System.out.println("hello am in swapPanel1");
                this.setVisible(false);
                ddSearch= new DDSearchInterface();
                mainMenu = new DDMainMenu();
                ddSearch.setBounds(10, 10, 200, 200);
                mainMenu.getSidePanel().add(ddSearch);
                mainMenu.getScrollPane().setEnabled(true);
                
                /**ddSearch= new DDSearchInterface();
                
                System.out.println("hello am in swapPanel2");
                mainMenu.getScrollPane().setViewportView(ddSearch);
                System.out.println("hello am in swapPanel3");
                System.out.println("hello actionPerformed 4");*/
                //this.setVisible(false);
    }
    private void loginActionPerformed(java.awt.event.ActionEvent evt) {                                      
        if (evt.getSource() == login) {
            if (usernameF.getText().length() == 0
                    && passwordF.getText().length() == 0) {
                JOptionPane.showMessageDialog(null, "Please make sure, that a "
                        + "username and password have been entered ");
            } else if (ddl.loggin(usernameF.getText(), passwordF.getText())) {

                System.out.println("hello actionPerformed 2");
                JOptionPane.showMessageDialog(null, "You have logged in sucessfully");
                swapPanel();
                //staff.searchStaff(usernameF.getText());
                //this.setVisible(false);
            } else {
                JOptionPane.showMessageDialog(null, "Incorrect username or password. Please check your username and password or speak to your"
                        + "systems administrator ");
            }

        }
    }                                     

    private void clearActionPerformed(java.awt.event.ActionEvent evt) {                                      
        passwordF.setText(null);
        usernameF.setText(null);
    }                                     
    // Variables declaration - do not modify                     
    private javax.swing.JButton clear;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JButton login;
    private javax.swing.JLabel password;
    private javax.swing.JPasswordField passwordF;
    private javax.swing.JLabel username;
    private javax.swing.JTextField usernameF;
    // End of variables declaration                   
}

I'm not 100% sure what you are trying to do, but when you call getPanel().removeAll() does getPanel return the panel you want to remove? or is it the panel containing the panel you want to remove? Otherwise you might just be emptying the panel (by removing all its components) making it appear to have been removed.

Oh, try by just adding a new JLabel("hello") or something, see if that works.

The other thing i would suggest is to build a small, easy to debug example and get what kind of behaviour you want, it would also be easier for people to help you with, then add the rest/convert the oritional when you get that working.

hi hanvy. Thanks for the suggestion. I will do.
In this program I have a JPanel on the leftside and a JTabbedPane on the right. In the JTabbedPane I have a JPanel for a users to login and when the user successfully logs in then the JPanel disappears. That part of the program is working. When that disappears I want a JPanel to appear in the side panel not in the same location where the other was removed. I willl go and try what tou suggested.

Thanks for your response

When that disappears I want a JPanel to appear in the side panel not in the same location where the other was removed. I willl go and try what tou suggested.

I see! i assumed you would want the new Panel to replace the old one, i see why card layout wouln't work then! I dont really know why adding wouldn't work, try making a frame with just a button that adds a Panel, get that to work then copy what you have done into your actual program.

Post if you find an answer!

The thing is if I do the same code in the same class its working but one I call it in another class its not. anyway will keep trying if I do get it will definitely post it. Thank anyway

Yea and I did what you suggested and same result

can you post the code for the smaller one?

package saiman.quantgcms;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Test extends JFrame{
	JPanel panel1;

	public Test()
	{
		this.setLayout(new FlowLayout());
		
		
		panel1 = new JPanel();
		panel1.setBorder(BorderFactory.createEtchedBorder());
		panel1.add(new JLabel("panel added!"));
		
		JButton addButton = new JButton("add");
		addButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				add(panel1);
				validate();
				repaint();
			}
		});
		add(addButton);
		
		JButton removeButton = new JButton("remove");
		removeButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				remove(panel1);
				validate();
				repaint();
			}
		});
		
		add(addButton);
		add(removeButton);
	}
	
	public static void main(String args[])
	{
		Test test = new Test();
		test.setSize(new Dimension(400,100));
		test.setVisible(true);
    }

}

here is an example of what i think you are trying to do (very, very simply)

sorry I took so long to answer was working.

all the initComponent() are only the GUI so that can be ignored. The TesterMainClass is where the TestLogin and TestSales are added. When its loaded the TestLogin is already showing then when the button is click one JPanel disappears and another appears in a different location. The JPanel is not appearing

import javax.swing.JPanel;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TestMainClass.java
 *
 * Created on 10-Nov-2010, 20:52:52
 */

/**
 *
 * @author Melly
 */
public class TestMainClass extends javax.swing.JFrame {

    /** Creates new form TestMainClass */
    public TestMainClass() {
        initComponents();
        TestLogin tl= new TestLogin();
        jScrollPane1.setViewportView(tl);
    }
    public JPanel getPanel(){
        return sidePanel;
    }

   
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        sidePanel = new javax.swing.JPanel();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextPane1 = new javax.swing.JTextPane();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout sidePanelLayout = new javax.swing.GroupLayout(sidePanel);
        sidePanel.setLayout(sidePanelLayout);
        sidePanelLayout.setHorizontalGroup(
            sidePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        sidePanelLayout.setVerticalGroup(
            sidePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 198, Short.MAX_VALUE)
        );

        jScrollPane1.setViewportView(jTextPane1);

        jTabbedPane1.addTab("tab1", jScrollPane1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(sidePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(48, 48, 48)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 183, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(69, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(sidePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(71, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TestMainClass().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTextPane jTextPane1;
    private javax.swing.JPanel sidePanel;
    // End of variables declaration

}
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TestSales.java
 *
 * Created on 10-Nov-2010, 20:55:52
 */

/**
 *
 * @author Melly
 */
public class TestSales extends javax.swing.JPanel {

    /** Creates new form TestSales */
    public TestSales() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();

        jLabel1.setText("jLabel1");

        jTextField1.setText("jTextField1");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(43, 43, 43)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(34, Short.MAX_VALUE))
        );
    }// </editor-fold>


    // Variables declaration - do not modify
    private javax.swing.JLabel jLabel1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration

}
public class TestLogin extends javax.swing.JPanel {

    /** Creates new form TestLogin */
    public TestLogin() {
        initComponents();
    }

   
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jButton1 = new javax.swing.JButton();

        jButton1.setText("jButton1");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(jButton1)
                .addContainerGap(47, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(58, 58, 58)
                .addComponent(jButton1)
                .addContainerGap(43, Short.MAX_VALUE))
        );
    }// </editor-fold>

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        if (evt.getSource() == jButton1) {
            TestMainClass ts = new TestMainClass();
            TestSales sales = new TestSales();
            this.setVisible(false);
            ts.getPanel().add(sales);
            sales.setBounds(2, 2, 30, 35);
        }
    }
    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    // End of variables declaration
}

You can see what you are doing wrong if you add this:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        if (evt.getSource() == jButton1) {
            TestMainClass ts = new TestMainClass();
            TestSales sales = new TestSales();
            ts.setVisible(true); // added line here
            this.setVisible(false);
            ts.getPanel().add(sales);
            sales.setBounds(2, 2, 30, 35);
        }
    }

this line here makes a new TestMainClass (which is invisible) sets the TestLogin Panel (on the visible testmain class) to invisible and adds the TestSales Panel to the new invisible TestMainClass. Confused? Just try work it out by adding the setvisible line!

setting the new TestMainClass to visible shows that you are actually adding the panel (i put a border on it, for some reason the JLabels arnt showing - but the panel is there)

TestMainClass ts = new TestMainClass();

What I would do, is pass the TestLogin Panel the instance of the TestMainClass it "belongs" to:

TestLogin tl= new TestLogin(this);

so then:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        if (evt.getSource() == jButton1) {
            testMainClass.getPanel().add(sales)
            TestSales sales = new TestSales();

            sales.initMethod()       //I guess this is why there were no buttons

            this.setVisible(false);  //note that this just hides it, not removes it
                                     //but that might be a better option
            ts.getPanel().add(sales);
            sales.setBounds(2, 2, 30, 35);
        }
    }

like that where testMainClass is assigned in the constructor ect and is actually the same instance that you create in the main method

I tried the first part by adding the setVisible(true) but when I do this and click the button it brings up an identical window.

but am confused about the last part because the sale.initMethod() (initComponents), is already called in the constructor- TestSales sales = new TestSales();. and ts.getPanel().add(sales); sales was already added in line 3. but the only class that has a getPanel() is TestMainClass. so i am not too sure what ts is in line 10. I just need that explaining a bit more. just a bit confused

public DDMainMenu() {
        initComponents();
        ddPane.setEnabled(false);
        searchPane.setViewportView(new DDLoginInterface(this));//TODO
    }
public DDLoginInterface(DDMainMenu mainMenu) {//TODO
        this.mainMenu = mainMenu;//TODO
        initComponents();
    }
...
            } else if (ddl.loggin(usernameF.getText(), passwordF.getText())) {
                JOptionPane.showMessageDialog(null, "You have logged in sucessfully");
                mainMenu.forceViewportView();//TODO
                System.out.println("hello actionPerformed 4");
            } else {
...
void forceViewportView() {
        searchPane.setViewportView(new DDSearchInterface());
        ddPane.setEnabled(true);
    }

inconsistencies in method getPanel() - class DDLoginInterface

public JPanel getPanel() {
        // JPanel pane = new DDLoginInterface();//You are just inside this class!
        return this;////TODO              //pane;
    }

I tried the first part by adding the setVisible(true) but when I do this and click the button it brings up an identical window.

but am confused about the last part because the sale.initMethod() (initComponents), is already called in the constructor- TestSales sales = new TestSales();. and ts.getPanel().add(sales); sales was already added in line 3. but the only class that has a getPanel() is TestMainClass. so i am not too sure what ts is in line 10. I just need that explaining a bit more. just a bit confused

Well, im not sure why it was empty then but it is there in the new window (set the panel border to something), but as I was saying, an "identical" window opens up because you make a new one with this line:

TestMainClass ts = new TestMainClass();

this makes a completley new TestMainClass, this is not the TestMainClass you made with the same call in the main function. When you make new frames, a new window opens. It looks the same because it is annother instance of the same class, but it is not actually the instance you want.

You have to call .getPanel() on the TestMainClass instance that your login panel is contained in, you are doing something along the lines of:

public class Test
{
	public void nameCar(Car someCarInstance)
	{
		System.out.println("Name: "+someCarInstance.toString());
	}
	
	public void driveCar()
	{
		Car c = new Car();
		c.drive();
	}
	
	public static void main(String args[])
	{
		Test mc =new Test();
		
		Car car1 = new Car();
		mc.nameCar(car1);
		mc.driveCar();
    }
}

class Car
{
	public void drive()
	{
		System.out.println("Driving car: " + this.toString());
	}
}

In the above code, nameCar() is implemented correctly, whereas driveCar() is wrong, with namecar we pass it the correct instance to name, but in the drivecar() we just make a new car, and drive that. You can see from the output that

Name: Car@19821f
Driving car: Car@addbf1

we are driving a different car to what we created, and wanted to drive. You have to pass a reference (variable? not sure the exact terminology) to any method/class that wishes to use it. If i wanted my Car to know which Test class it belonged to, i would pass it a Test in the constructor

package saiman.quantgcms;

public class Test
{
	public String roadName;
	
	public void makeACar()
	{
		Car car1 = new Car(this); //<- "this" is often used like this
	}
	
	public static void main(String args[])
	{
		Test mc =new Test();
		mc.roadName = "Road1";
		Car car1 = new Car(mc); //pass the car a Test
		car1.getRoadName();
		car1.BADgetRoadName();
    }
}

class Car
{
	Test test;
	
	public Car(Test test)
	{
		this.test = test;
	}
	
	public void drive()
	{
		System.out.println("Driving car: " + this.toString());
	}
	
	//right, because the test object is the one that "created" it
	public void getRoadName()
	{
		System.out.println("RoadName: " + test.roadName );
	}
	
	//wrong, this Test is completley different, new and nothing to do with anything you
	//have made in the past.
	public void BADgetRoadName()
	{
		Test mc =new Test();
		mc.roadName = "Some random road";
		
		System.out.println("RoadName: " + mc.roadName );
	}
	
}

hope that clears it up a bit, its not brilliantly written....

thanks hanvy just had a quick look at what you posted and looks really helpful and it looks like this is what I have done because the code was working in the same class but not in another. will check my code later and update the post when I figured what I need to change.
Thanks for posting quuba, what I was wanting to do is add the DDSearchInterface to a JPanel that at the side of the JtabbedPane but not to the JTabbedPane.

Hey guys.
Have tried and failed have given up for now but if and when I get the answer will post it. I even tried using a static method and just calling the class name to avoid creating different instances but was unsuccessful. although the same method worked in the same class.
Thanks for the help anyway

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.