How to change the focus between tabs on a Jtabbedpane?

Asked

Viewed 606 times

1

I am trying to do the following routine. On my "main" screen, I own a JTabbedPane with two tabs, in which the focus is on the first tab.

I need to open one JInernalFrame and, by closing it, make the focus go to the second tab of my JTabbedPane. However, the way I tried to make me returns a NullPointerException and does not give focus to tab 02.

I made a very simple example, more that 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();
            });
        }
    }
}
  • It doesn’t make much sense. If you want the focus to go to the next tab, change the selection for it. That way it will never work.

  • @Article It may not make sense to be a very simple example, more, the issue of changing tabs, is that in one I will have a table of installments and in the other of payments, when pay a installment for the screen that opens, it goes to the tab of payments. I implemented the classes as an example, but it didn’t work.

  • See the answer below.

1 answer

1


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

Assuming you are actually changing the selected tab, just modify as below using the method setSelectedIndex(), since it is a innerclass:

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

Testing:

inserir a descrição da imagem aqui

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.