Close a JtabbedPane with JInternalFrame Inside

414 views Asked by At

How can I close a JTabbedPane with an JInternalFrame included when I click a button inside this form?

My project has 3 JForms, 1 TabbedPane, 1 JInternalFrame and 1 Main window (to register data). To create my JTabbedPanes I use the method addTab() and from that form I have the button to close which calls another window (Main registry), I used the dispose() and setVisible(false) methods but they don't do anything.

An example (there are 3 clases -Administrador, -MenuPrincipal, -ventanaRegistro) :

First Class a JFrame and on top a JTabbedPane (this is where I create my tabs)

public class MenuPrincipal extends javax.swing.JFrame 
{

    Administrador ventanaAdministrador = new Administrador();

    void showTabs()
    {
        JTabbedPane.addTab("Administrador", ventanaAdministrador);
    }

    public MenuPrincipal() 
    {
        initComponents();
        showTabs();
    }
}

Second Class a JInternalFrame with a single button

public class Administrador extends javax.swing.JInternalFrame 
{
    void showAdminWindow()
    {
        ventanaRegistro ventanaRegistro = new ventanaRegistro();
        ventanaRegistro.setVisible(true); 
    }

    void closeThisWindow()
    {
        this.dispose();
    }
    public Administrador() 
    {
        initComponents();
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)    {                                         
        showAdminWindow();
        closeThisWindow();
    }
}

Third Class a simple JFrame. This is just code to register data.

The real problem is that my method closeThisWindow() doesn't close the second Jtabbed window, and I want to make it that when I click a button (which is on the JTabbedPane) make the third class visible and the Second Class invisible/Close it.

1

There are 1 answers

0
c0der On BEST ANSWER

See if the following code does what you expect. If it doesn't please elaborate so I can improve my answer.

import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.WindowConstants;


public class MenuPrincipal extends javax.swing.JPanel {

    Administrador ventanaAdministrador = new Administrador();

    public MenuPrincipal()

    {
        setPreferredSize(new Dimension(290, 200));

        JTabbedPane tabbedPane = new JTabbedPane();
        tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
        ventanaAdministrador.setPreferredSize(new Dimension(350, 100));
        tabbedPane.addTab("Admin", ventanaAdministrador);
        tabbedPane.addTab("2nd Tab", new JPanel());
        add(tabbedPane);
    }


    /**
     * @param args
     */
    public static void main(String[] args) {

        JFrame testFrame = new JFrame("Main Test Window");
        testFrame.setDefaultCloseOperation(WindowConstants.
                     DISPOSE_ON_CLOSE);
        testFrame.setPreferredSize(new Dimension(450, 350));
        MenuPrincipal menuP = new MenuPrincipal();
        testFrame.getContentPane().add(menuP);

        testFrame.pack();
        testFrame.setVisible(true);

    }
}

And:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

public class Administrador extends javax.swing.JInternalFrame {

  void showAdminWindow()
  {
    JFrame ventanaRegistro = new JFrame("Admin Window");
    ventanaRegistro.
    setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

    ventanaRegistro.setPreferredSize(new Dimension(350, 200));
    ventanaRegistro.pack();
    ventanaRegistro.setVisible(true);
  }

  void closeThisWindow()
  {
    Window window = SwingUtilities.getWindowAncestor(this);
    window.dispose();
  }

  public Administrador()
  {

    JPanel panel = new JPanel();
    getContentPane().add(panel, BorderLayout.NORTH);

    JButton closeWindowBttn = new JButton("Close Window and open a new  one");
    closeWindowBttn.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent arg0) {
            closeWindowActionPerformed();
        }
     });
     panel.add(closeWindowBttn);
  }

  private void closeWindowActionPerformed()    {
    showAdminWindow();
    closeThisWindow();

 }

}