How To Focus JtabbedPane create Tabs

3k views Asked by At

I want to Focus recently opened Tab of a JtabbedPane.In my Application,I create any number tabs It Focus on First tab only,Not focus recently create Tab automatically.For Example: When I create First tab The focus on first Tab,then I create Another Tab The Focus transfer to next Created tab Automatically not in First tab only.Please check it once.Thank You.

My Code:

public class TabbedPaneFocus extends javax.swing.JFrame {

JTextArea tx;
int i=0;
JTabbedPane tabbedPane;

public TabbedPaneFocus() {

    initComponents();
    tabbedPane=new CloseButtonTabbedPane();
    add(tabbedPane);
    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.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 512, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(tabbedPane, javax.swing.GroupLayout.DEFAULT_SIZE, 366, Short.MAX_VALUE)
    );

}

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

    jMenuBar1 = new javax.swing.JMenuBar();
    jMenu1 = new javax.swing.JMenu();
    create = new javax.swing.JMenuItem();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jMenu1.setText("File");

    create.setText("Create");
    create.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            createActionPerformed(evt);
        }
    });
    jMenu1.add(create);

    jMenuBar1.add(jMenu1);

    setJMenuBar(jMenuBar1);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 512, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 366, Short.MAX_VALUE)
    );

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

private void createActionPerformed(java.awt.event.ActionEvent evt) {                                       
    try{
        i++;
        tx = new JTextArea();
        tx.setFont(new java.awt.Font("Miriam Fixed", 0, 13));
        JScrollPane  scrollpane=new JScrollPane(tx);
        tabbedPane.add(scrollpane);
        tabbedPane.addTab("Doc"+i, scrollpane);
        tabbedPane.setFocusable(true);
    }
   catch(ArrayIndexOutOfBoundsException aio){   
   }
}                                      

public static void main(String args[]) {
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(TabbedPaneFocus.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(TabbedPaneFocus.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(TabbedPaneFocus.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(TabbedPaneFocus.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    java.awt.EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            new TabbedPaneFocus().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JMenuItem create;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenuBar jMenuBar1;
// End of variables declaration                   
}
1

There are 1 answers

0
Iootu On BEST ANSWER

The problem you are having is not related to focus, because theJTabbedPaneis already the focussed component in the application rather what you need is to change the selected index of the component. This following piece of code does just that, add it to the end of yourcreateActionPerformedmethod:

tabbedPane.setSelectedIndex(i-1);