Reputation: 31
I'm using Java swing for the first time, and I have a problem which I can't solved.
I have a JTabbedPane with 3 tabs and I need to change to next tab "automatically" in the end of a computing, which starts with a click in a JButton (within one of the tabs).
I've tried to use setSelectedIndex()
but doesn't work.
After the JButton's ActionEvent
the selected tab (printed in eclipse console) has changed, but in GUI doesn't had effect.
Yes, I've tried the validate()
, revalidate()
methods, even the repaint()
method, but didn't work
That's an example of my code
public class Tab1 extends JPanel {
//when click on this button the computing starts
JButton btn = new JButton("Compute...");
btn.addActionListener(new BtnListener());
add(btn);
}
An example of a JPanel which I'll add to my JTabbedPane
public class Window() {
private JFrame frame;
public Window() {
init();
}
private void init() {
frame = new JFrame();
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
JPanel tab1 = new Tab1();
JPanel tab2 = new Tab2();
JPanel tab3 = new Tab3();
tabbedPane.addTab("tab 1", null, tab1, "tab1");
tabbedPane.addTab("tab2", null, tab2, "tab2");
tabbedPane.addTab("tab3", null, tab3, "tab3");
//add() all components
}
}
Example of my Window
public class BtnListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
//make the computing
//then I need to change to next tab (tab2)
}
}
And my Listener
I hope you understand my problem. Please help me, I really don't know what I do to make it works and I need to do this.
Upvotes: 1
Views: 3783
Reputation: 1194
A change listener is similar to a property change listener. A change listener is registered on an object — typically a component, but it could be another object, like a model — and the listener is notified when the object has changed. The big difference from a property change listener is that a change listener is not notified of what has changed, but simply that the source object has changed. Therefore, a change listener is most useful when it is only necessary to know when an object has changed in any way.
Similarly, you need to register a change listener on a button(or toggle button with states) to be informed when the calculation had stopped and the button's value changes.
//use some flag to determine when calculation process is actually finished
volatile boolean isCalculationEnds = true;
button.addChangeListener((ChangeEvent e) -> {
if (tabbedPane.getSelectedComponent() instanceof CustomPanel && isCalculationEnds) {
//do something
}
if (tabbedPane.getSelectedIndex()==1 && isCalculationEnds){
//do something
});
Upvotes: 0
Reputation: 31
Thanks for answers.
I checked my code and I've found an instance problem. I was changing another instance of my Window instead of what was visible. So, when I fixed that, the updates worked.
Thanks again.
Upvotes: 0
Reputation: 10994
When you switch tabs you needn't to use revalidate()/repaint()
just setSelectedIndex()
.
Here is an example of switching to next tab:
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 TestFrame extends JFrame {
public static void main(String... s) {
new TestFrame();
}
private JTabbedPane tabs;
public TestFrame() {
init();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
private void init() {
tabs = new JTabbedPane();
tabs.addTab("1", new Tab(1));
tabs.addTab("2", new Tab(2));
tabs.addTab("3", new Tab(3));
add(tabs);
}
private class Tab extends JPanel {
public Tab(int i) {
JButton next = new JButton("next");
next.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int selectedIndex = tabs.getSelectedIndex();
int nextIndex = selectedIndex == tabs.getTabCount()-1 ? 0 : selectedIndex+1;
tabs.setSelectedIndex(nextIndex);
}
});
add(new JLabel("tab " + i));
add(next);
}
}
}
Upvotes: 1