-2

I had a problem and found a solution, but I can't understand why this following code is not working. What I want to do is, that the tab is switching when a button gets pressed in the "Administration.class". I got a JTabbedPane which is added in the "Main.class". It is declared as a private variable.

private JTabbedPane JPT;

then I have a simple method, which shall change the tab, when called:

public void SetPane() {
    JTP.setSelectedIndex(2);
}

Then I am creating a object of the Main.class in the Administration.class and calling this method, when the button is pressed:

btnRework.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Main m = new Main();
            m.SetPane();
        }
    });

But this is not working. Nothing happens, when the button gets clicked, I even get not error.

This works: Main.class

public static JTabbedPane JTP;

Administration.class

btnRework.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Main.JTP.setSelectedIndex(2);
        }
    });

Why example first is not working, and maybe you guys have a link, where I could have a look to a content which is describing this problem. If you just wanna handle a variable and their methods, what is the better way? Thanks in advance!

Main.class:

public class Main extends JDialog {

private static final long serialVersionUID = 1L;

private Home home = new Home();
private Insertation insertation = new Insertation();
private Edit edit = new Edit();
private Administration administration = new Administration();
private Addition addition = new Addition();
public static JTabbedPane JTP;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    try {
        Main dialog = new Main();
        dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        dialog.setVisible(true);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

/**
 * Create the dialog.
 */
public Main() {
    super(null, java.awt.Dialog.ModalityType.TOOLKIT_MODAL);
    setResizable(false);
    setSize(1030, 720);
    getContentPane().setLayout(new BorderLayout());
    setTitle("Ebay Manager");

    JTP = new JTabbedPane(JTabbedPane.TOP, JTabbedPane.SCROLL_TAB_LAYOUT);
    JTP.addTab("Home", home);
    JTP.addTab("Inserat", insertation);
    JTP.addTab("Bearbeitung", edit);
    JTP.addTab("Verwaltung", administration);
    JTP.addTab("Zusatz", addition);
    getContentPane().add(JTP);
}

}

Administration.class:

public class Administration extends JPanel {

private static final long serialVersionUID = 1L;
private JButton btnRework;
private Main main;

/**
 * Create the panel.
 */
public Administration() {
    setLayout(null);
    setSize(1000, 650);
    setDoubleBuffered(true);

    btnRework = new JButton("Bearbeiten");
    btnRework.setBounds(42, 571, 116, 32);
    add(btnRework);
    btnRework.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            main.SetPane();
        }
    });
}

}

Simon Pio.
  • 115
  • 1
  • 14
  • You basically want to read about the difference between static and non-static fields (static things are the same for all objects of a class, non-static fields can be different for each of the objects). In general, you should be avoiding static fields. – GhostCat Mar 21 '16 at 23:56
  • Except for constants. – Majora320 Mar 22 '16 at 00:02

3 Answers3

0
Main m = new Main();
m.SetPane();

Here, you are creating an instance of Main but you are not adding to it anywhere.

Edit:

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

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

public class Administrator extends JFrame {
    private Main main;
    public Administrator() {
        main = new Main();

        add(main);

        JButton button = new JButton("Set");
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                main.setPane();
            }
        });

        add(button, BorderLayout.SOUTH);
    }

    public static void main(String[] args) {
        Administrator adminFrame = new Administrator();
        adminFrame.setSize(400, 400);
        adminFrame.setVisible(true);

    }
}

class Main extends JPanel {
    private JTabbedPane jtp;

    public Main() {
        super(new BorderLayout());
        jtp = new JTabbedPane();

        jtp.add("tab1", new JLabel("Content1"));
        jtp.add("tab2", new JLabel("Content2"));

        add(jtp);
    }

    public void setPane() {
        jtp.setSelectedIndex(1);
    }
}
rdonuk
  • 3,921
  • 21
  • 39
  • I did this, just to be able to call the method SetPane(). Bad programming style? – Simon Pio. Mar 22 '16 at 00:06
  • The problem isn't that you're calling `setPane()`, it's which object you're calling `setPane()` _on_. – Paul Hicks Mar 22 '16 at 00:07
  • I added a sample code. Not a good layout but it is working as you want, check this. – rdonuk Mar 22 '16 at 00:21
  • You mean, he is not working with the variable JTP in the Main.class? – Simon Pio. Mar 22 '16 at 00:24
  • What do you mean "he is not working with the variable JTP in the Main.class"? – rdonuk Mar 22 '16 at 00:28
  • I am not sure if this is fine. I just have a JDialog and JPanels, but no JFrame. But it works, thanks – Simon Pio. Mar 22 '16 at 00:42
  • You can do it many ways, but don't make `JTabbedPane` static. Prefer define it as an instance member. Using static fields is a bad practice. – rdonuk Mar 22 '16 at 00:46
  • The problem is, only a static field is working. All other answers didn't work. I don't know why – Simon Pio. Mar 22 '16 at 00:58
  • Reason is obvious, you are setting the index of a wrong `JTabbedPane` instance. Every time you call `new Main()` a tabbed pane instance creating. So if you call `main.setPane()` it is setting the instance of the new tabbed pane. – rdonuk Mar 22 '16 at 01:08
  • rodnuk! Nice, now I got it! Okay, so in this case only a static field is possible? – Simon Pio. Mar 22 '16 at 01:16
  • Nope :) Reach the right instance and use it. This is the better way. If you share more code, or a small runnable code which reproduce the error, I can help you. – rdonuk Mar 22 '16 at 01:21
0

You are switching panes in a new undrawn frame that you create and throw away inside your action listener. You need to switch panes in the frame that you have (presumably) packed and drawn in your application.

So instead of

Main m = new Main();
m.SetPane();

you need

myMainFrame.SetPane();

Exactly how you create and remember myMainFrame is beyond the scope of this answer (unless you add more code to your question).

Paul Hicks
  • 13,289
  • 5
  • 51
  • 78
  • Is the action listener on a button in the Main frame or in the JTP tabbed pane? Assuming that `Main` is a `JFrame` then you probably want something like `((Main) ((JTabbedPane) e.getSource()).getParent()).setPane()` – Paul Hicks Mar 22 '16 at 00:22
0

I found my own solution because of the answer of rdonuk. Since I can't make another instance of the Main.class I set the JTabbedPane as variable in the Administration.class when creating the Main.class. Just a easy setter method. Thanks for all answers!

Simon Pio.
  • 115
  • 1
  • 14