I want to ask if there is a possible way to switch the tab panels from a click of a button in another class. I have 2 class and classA
is in default package and the other one is in another package. My button is placed in classB
and my tabbed panes are in classA
.
How can I change tabs by clicking the button in my classB
?
If passing variables are not going to work between the default package and another package its okay I'll just put classB
in the same package so it'll be easier to access variables from classA
.
** EDIT **
Okay I made a short ver of my code and this is the result:
Test.java
package test;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import btn.button;
public class Test {
JFrame frame = new JFrame();
JTabbedPane tabpane = new JTabbedPane();
JPanel panel1 = new JPanel();
GridBagConstraints c = new GridBagConstraints();
final static boolean shouldFill = true;
GridBagLayout grid = new GridBagLayout();
public Test() {
// TODO code application logic here
// public button switch;
frame.setVisible(true);
frame.setSize(821,421);
final Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
int x=(int)((dimension.getWidth() - frame.getWidth())/2);
int y=(int)((dimension.getHeight() - frame.getHeight())/2);
frame.setLocation(x, y);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel container = new JPanel();
container.setLayout(new BorderLayout());
container.setBackground(new Color(0,128,0));
tabpane = new JTabbedPane(); //Tab Pane
tabpane.setBackground(new Color(0,128,0));
tabpane.setSize(801,351);
JPanel jp1 = new JPanel(); // First Tab Panel
jp1.setBackground(new Color(0,128,0));
jp1.setLayout(grid);
JButton popout = new JButton("MORE");
popout.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
button button = new button();
}
});
jp1.add(popout);
JPanel jp2 = new JPanel(); // Second Tab Panel
jp2.setBackground(new Color(0,128,0));
jp2.setLayout(grid);
JPanel jp3 = new JPanel(); // Third Tab Panel
jp3.setBackground(new Color(0,128,0));
jp3.setLayout(grid);
tabpane.addTab("Tab 1", jp1);
//add selectedIndex here (1);
tabpane.addTab("Tab 2", jp2);
//add selectedIndex here (2);
tabpane.addTab("Tab 3", jp3);
//add selectedIndex here (3);
frame.add(container);
container.add(tabpane, BorderLayout.NORTH);
}
public static void main(String[] args){
//Use the event dispatch thread for Swing components
EventQueue.invokeLater(new Runnable()
{
@Override
public void run()
{
new Test();
}
});
}
}
button.java
package btn;
import java.awt.Color;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class button {
JFrame frame2 = new JFrame();
GridBagLayout grid = new GridBagLayout();
public button(){
frame2.setVisible(true);
frame2.setSize(200,200);
JPanel jp = new JPanel(); // First Tab Panel
jp.setBackground(new Color(0,128,0));
jp.setLayout(grid);
JButton btn = new JButton("SWITCH TO PANEL 2");
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//what should i put here???
}
});
jp.add(btn);
frame2.add(jp);
}
}
I know I'm missing the set selected index for each tab but I don't know how to replicate that because in my project those tabs are in a separate java file. So to shorten it I made it in one java file. So yeah. As you can see I have the more button and then the switch button for tab 2. My question is how can I switch to panel 2 by clicking the button in the pop out?