2

I've created and application in which there are two tabs titled Tabbedpane 1 and Tabbedpane 2. In one tab body Tabbedpane 1 contains a JInternalFrame in which there is a search button. On clicking the button another JInternalFrame opens within the main JInternalFrame.

Can anyone please tell me how to close the said opened JInternalFrame ie the search JInternalFrame while clicking on to the second tab titled Tabbedpane 2

I've tried .setClosed(true) and .setVisible(true) on property change still it does'nt works for me.

Nidhish Krishnan
  • 20,593
  • 6
  • 63
  • 76

1 Answers1

3

Use a ChangeListener which will get notified when JTabbedPane state changes (i.e tabs switched):

final JTabbedPane tabbedPane = new JTabbedPane();

tabbedPane.addChangeListener(new ChangeListener() {
    public void stateChanged(ChangeEvent e) {
       //tab has been changed
    }
});

The problem is we will need a way to monitor the previous tab i.e the tab we were on before it was changed this can be done via:

tabbedPane.addChangeListener(new ChangeListener() {
    int prev_index = 0;
    int curr_index = 0;

    public void stateChanged(ChangeEvent e) {
        prev_index = curr_index;
        curr_index = tabbedPane.getSelectedIndex();

        System.out.println("Tab (Current): " + curr_index);
        System.out.println("Tab (Previous): " + prev_index);
    }
});

UPDATE 1:

To close JInternalFrame I'd suggest calling dispose() on its instance

UPDATE 2:

Here is your fixed code, basically added a getter for Search class, thus JIFrame1 has a getSearch() method which allows us to gaim access to the Search classes current instance created in JIFrame1, in changedState(..) I call jiFrame1.getSearch().dispose() which will make sure we dispose of the instance that has already been created:

import java.awt.Container;
import java.awt.event.MouseListener;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Sample extends javax.swing.JFrame {

    JIFrame1 jiframe1 = new JIFrame1();

    public Sample() {
        initComponents();

        Container jiframe1cont = tab1;
        for (MouseListener listener : ((javax.swing.plaf.basic.BasicInternalFrameUI) jiframe1.getUI()).getNorthPane().getMouseListeners()) {
            ((javax.swing.plaf.basic.BasicInternalFrameUI) jiframe1.getUI()).getNorthPane().removeMouseListener(listener);
        }
        jiframe1.setLocation(10, 10);
        jiframe1cont.add(jiframe1);
        jiframe1.setVisible(true);

        JIFrame2 jiframe2 = new JIFrame2();
        Container jiframe2cont = tab2;
        for (MouseListener listener : ((javax.swing.plaf.basic.BasicInternalFrameUI) jiframe2.getUI()).getNorthPane().getMouseListeners()) {
            ((javax.swing.plaf.basic.BasicInternalFrameUI) jiframe2.getUI()).getNorthPane().removeMouseListener(listener);
        }
        jiframe2.setLocation(10, 10);
        jiframe2cont.add(jiframe2);
        jiframe2.setVisible(true);
    }

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

        tab1 = new javax.swing.JDesktopPane();
        tab2 = new javax.swing.JDesktopPane();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        tabbedPane.setTabPlacement(javax.swing.JTabbedPane.LEFT);
        tabbedPane.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                tabbedPaneStateChanged(evt);
            }
        });

        tab1.setBackground(new java.awt.Color(240, 240, 240));
        tabbedPane.addTab("Tabbedpane 1", tab1);

        tab2.setBackground(javax.swing.UIManager.getDefaults().getColor("Button.background"));
        tabbedPane.addTab("Tabbedpane 2", tab2);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(tabbedPane, javax.swing.GroupLayout.DEFAULT_SIZE, 731, Short.MAX_VALUE));
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(tabbedPane, javax.swing.GroupLayout.DEFAULT_SIZE, 503, Short.MAX_VALUE));

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

    private void tabbedPaneStateChanged(javax.swing.event.ChangeEvent evt) {
        try {
            jiframe1.getSearch().dispose();
        } catch (Exception ex) {
            Logger.getLogger(Sample.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Sample().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JDesktopPane tab1;
    private javax.swing.JDesktopPane tab2;
    private final javax.swing.JTabbedPane tabbedPane = new javax.swing.JTabbedPane();
    // End of variables declaration
}

class Search extends javax.swing.JInternalFrame {

    public Search() {
        initComponents();
    }

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

        jTextField1 = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();

        jTextField1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
                new Object[][]{
                    {null, null, null, null},
                    {null, null, null, null},
                    {null, null, null, null},
                    {null, null, null, null}
                },
                new String[]{
                    "Title 1", "Title 2", "Title 3", "Title 4"
                }));
        jScrollPane1.setViewportView(jTable1);

        jButton1.setText("CLOSE");
        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(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 385, Short.MAX_VALUE)
                .addContainerGap())
                .addGroup(layout.createSequentialGroup()
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(205, Short.MAX_VALUE))))
                .addGroup(layout.createSequentialGroup()
                .addGap(158, 158, 158)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(172, Short.MAX_VALUE)));
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));

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

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        this.dispose();
    }
    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration
}

class JIFrame1 extends javax.swing.JInternalFrame {

    public JIFrame1() {
        initComponents();
    }

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

        pane = new javax.swing.JDesktopPane();
        jButton1 = new javax.swing.JButton();

        pane.setBackground(new java.awt.Color(240, 240, 240));

        jButton1.setText("SEARCH");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jButton1.setBounds(170, 60, 90, 23);
        pane.add(jButton1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(pane, javax.swing.GroupLayout.DEFAULT_SIZE, 567, Short.MAX_VALUE));
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(pane, javax.swing.GroupLayout.DEFAULT_SIZE, 322, Short.MAX_VALUE));

        pack();
    }// </editor-fold>
    private Search search;

    public Search getSearch() {
        return search;
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        search = new Search();
        Container searchcont = pane;
        for (MouseListener listener : ((javax.swing.plaf.basic.BasicInternalFrameUI) search.getUI()).getNorthPane().getMouseListeners()) {
            ((javax.swing.plaf.basic.BasicInternalFrameUI) search.getUI()).getNorthPane().removeMouseListener(listener);
        }
        search.setLocation(10, 10);
        searchcont.add(search);
        search.setVisible(true);
    }
    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JDesktopPane pane;
    // End of variables declaration
}

class JIFrame2 extends javax.swing.JInternalFrame {

    public JIFrame2() {
        initComponents();
    }

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

        jLabel1 = new javax.swing.JLabel();

        jLabel1.setText("JIFrame2..............");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                .addGap(100, 100, 100)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(137, Short.MAX_VALUE)));
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                .addGap(86, 86, 86)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(145, Short.MAX_VALUE)));

        pack();
    }// </editor-fold>
    // Variables declaration - do not modify
    private javax.swing.JLabel jLabel1;
    // End of variables declaration
}

UPDATE 3:

The reason for NPE is here:

private void tabbedPaneStateChanged(javax.swing.event.ChangeEvent evt) {
        try {
            jiframe1.getSearch().dispose();
        } catch (Exception ex) {
            Logger.getLogger(Sample.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

change to this:

private void tabbedPaneStateChanged(javax.swing.event.ChangeEvent evt) {
    try {
        Search s=jiframe1.getSearch();
        if(s!=null)
        s.dispose();
    } catch (Exception ex) {
        Logger.getLogger(Sample.class.getName()).log(Level.SEVERE, null, ex);
    }
}
David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
  • ok but how to close the opened search JInternalFrame . i tried try { new Search().setClosed(true); } catch (PropertyVetoException ex) { Logger.getLogger(Sample.class.getName()).log(Level.SEVERE, null, ex); } – Nidhish Krishnan Dec 11 '12 at 16:53
  • @NidhinKrishnan Also not by using the `new` keyword you create a new instance of Search each time you should call `dispose()` on the previously created instance – David Kroukamp Dec 11 '12 at 17:05
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/20960/discussion-between-nidhinkrishnan-and-david-kroukamp) – Nidhish Krishnan Dec 11 '12 at 17:40
  • @NidhinKrishnan dont have the time right now sorry, but i fixed the code see update 3 – David Kroukamp Dec 11 '12 at 17:55