How to change the focus between tabs in a JTabbedPane?

1

I'm trying to do the following routine. On my "main" screen, I have a% w / w of% with two tabs, where the focus is on the first tab.

I need to open a JTabbedPane and, when I close it, cause the focus to go to the second tab of my JInernalFrame . However, the way I tried to do it returns a JTabbedPane and does not give focus to tab 02.

I made a very simple example, which illustrates well what I'm trying to do:

import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;

public class TelaPrincipal extends JFrame {

    public static void main(String[] args) {
        Runnable startApp = () -> {
            TelaPrincipal fc = new TelaPrincipal();
            fc.setVisible(true);
        };

        SwingUtilities.invokeLater(startApp);
    }

    private JTabbedPane jTabbedPane = new JTabbedPane();
    private JButton button = new JButton("Click");
    private JDesktopPane jdp = new JDesktopPane();

    public TelaPrincipal() {
        add(montaTela());
        setSize(700, 400);
        action();
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    private JPanel montaTela() {
        JPanel painel = new JPanel();
        painel.setLayout(new BorderLayout());

        jTabbedPane.add("Aba 01", new JLabel("→ Aba 01"));
        jTabbedPane.add("Aba 02", new JLabel("→ Aba 02"));
        jTabbedPane.setPreferredSize(new Dimension(500, 150));

        painel.add(jTabbedPane, BorderLayout.NORTH);
        painel.add(jdp, BorderLayout.CENTER);
        painel.add(button, BorderLayout.SOUTH);
        return painel;
    }

    public JTabbedPane getjTabbedPane() {
        return jTabbedPane;
    }

    private void action() {
        button.addActionListener(e -> {
            Tela tela = new Tela();
            jdp.add(tela);
            tela.setVisible(true);
        });
    }

    // ---------------------------------------------
    class Tela extends JInternalFrame {

        private JButton close = new JButton("Close");

        public Tela() {
            setTitle("Tela interna");
            add(montaTela());
            setSize(300, 100);
            actionClose();
            setVisible(true);
            setLocationRelativeTo(null);
        }

        private JPanel montaTela() {
            JPanel painel = new JPanel();
            painel.setLayout(new BorderLayout());
            painel.add(new JLabel("Apenas para demonstrar .."), BorderLayout.NORTH);
            painel.add(close, BorderLayout.CENTER);
            return painel;
        }

        private void actionClose() {
            close.addActionListener(e -> {
                dispose();
                TelaPrincipal tlp = new TelaPrincipal();
                //tlp.getjTabbedPane().getTabComponentAt(1).getParent().requestFocus();
                tlp.getjTabbedPane().getTabComponentAt(1).requestFocus();
            });
        }
    }
}
    
asked by anonymous 23.03.2018 / 19:47

1 answer

1

What you're trying to do is not change focus, it's change selection, it's different things, if it's not that and really is what was explained in the question, it does not make much sense.

Assuming you actually change the selected tab, simply modify it as below using the setSelectedIndex() , since this is a innerclass : / p>

private void actionClose() {
    close.addActionListener(e -> {
        dispose();
        getjTabbedPane().setSelectedIndex(1);
    });
}

Testing:

Just warning that you were creating a new instance of the main window, which was another error. Once the class is internal, you can access the methods and attributes directly. If the class is created separately, you will need to pass the instance of the main screen between JInternalFrame .

    
23.03.2018 / 20:10