关闭选项卡后获取焦点选项卡 [英] Get the in-focus tab after closing a tab
问题描述
在 JTabbedPane 中,我将定制的 Data 对象关联到每个添加的选项卡.I also have a corresponding Metadata object that shows up in another panel when the tab is selected.我现在遇到的问题是当一个选项卡关闭时,元数据面板会在刚刚关闭的选项卡中显示数据对象的元数据.理想情况下,我希望面板显示用户看到的焦点选项卡的元数据.但是,关闭选项卡的行为意味着选定选项卡"是正在关闭的选项卡,因此 tabpane.getSelectedIndex()
将不起作用.关闭选项卡后如何获取焦点选项卡?提前致谢!
Devil is in the detail,你没有提供.
我做了一个快速测试,发现 ChangeListener
在 ContainerListener
之前被调用,这真的很痛苦,但是,它总是报告正确的索引.>
因此,您需要做的是将两者结合在一起,以便在调用时两者都会更新元数据窗格.
import java.awt.EventQueue;导入 java.awt.GridBagLayout;导入 java.awt.event.ActionEvent;导入 java.awt.event.ActionListener;导入 java.awt.event.ContainerEvent;导入 java.awt.event.ContainerListener;导入 javax.swing.JButton;导入 javax.swing.JFrame;导入 javax.swing.JPanel;导入 javax.swing.JTabbedPane;公共类测试{公共静态无效主(字符串 [] args){新测试();}公共测试(){EventQueue.invokeLater(new Runnable() {@覆盖公共无效运行(){JTabbedPane tabbedPane = new JTabbedPane();tabbedPane.addTab("One", new TabPane(tabbedPane));tabbedPane.addTab("Two", new TabPane(tabbedPane));tabbedPane.addTab("三", new TabPane(tabbedPane));tabbedPane.addTab("四", new TabPane(tabbedPane));tabbedPane.addContainerListener(new ContainerListener() {@覆盖public void componentAdded(ContainerEvent e) {}@覆盖public void componentRemoved(ContainerEvent e) {System.out.println("已移除" + e.getChild());}});tabbedPane.addChangeListener(new ChangeListener() {@覆盖public void stateChanged(ChangeEvent e) {System.out.println(tabbedPane.getSelectedIndex());}});JFrame frame = new JFrame();frame.add(tabbedPane);框架.pack();frame.setLocationRelativeTo(null);frame.setVisible(true);}});}公共类 TabPane 扩展 JPanel {私人 JTabbedPane 父级;公共 TabPane(JTabbedPane 父级){this.parent = 父母;setLayout(new GridBagLayout());JButton btn = new JButton("关闭");添加(btn);btn.addActionListener(new ActionListener() {@覆盖public void actionPerformed(ActionEvent e) {parent.remove(TabPane.this);}});}}}
In a JTabbedPane, I associated a custom-made Data object to each added tab. I also have a corresponding Metadata object that shows up in another panel when the tab is selected. The problem I have now is when a tab is closed, the metadata panel shows the metadata of the Data object in the tab that just gets closed. Ideally, I want the panel to show the metadata for the in-focus tab that the user sees. However, the act of closing a tab means the "selected tab" is the tab being closed, so tabpane.getSelectedIndex()
would not work. How can I get the tab that is in focus after closing a tab? Thank you in advance!
Devil is in the detail, which you provided none.
I did a quick test and discovered that, ChangeListener
is called before ContainerListener
, which is a real pain, but, it was always reporting the correct index.
So, what you need to do is marry the two together, so that, both will update the meta data pane when they are called.
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ContainerEvent;
import java.awt.event.ContainerListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab("One", new TabPane(tabbedPane));
tabbedPane.addTab("Two", new TabPane(tabbedPane));
tabbedPane.addTab("Three", new TabPane(tabbedPane));
tabbedPane.addTab("Four", new TabPane(tabbedPane));
tabbedPane.addContainerListener(new ContainerListener() {
@Override
public void componentAdded(ContainerEvent e) {
}
@Override
public void componentRemoved(ContainerEvent e) {
System.out.println("Removed " + e.getChild());
}
});
tabbedPane.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
System.out.println(tabbedPane.getSelectedIndex());
}
});
JFrame frame = new JFrame();
frame.add(tabbedPane);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TabPane extends JPanel {
private JTabbedPane parent;
public TabPane(JTabbedPane parent) {
this.parent = parent;
setLayout(new GridBagLayout());
JButton btn = new JButton("Close");
add(btn);
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
parent.remove(TabPane.this);
}
});
}
}
}
这篇关于关闭选项卡后获取焦点选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!