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();
}
});
}
}