[英]Focus lost when updating a JDialog
我有:
JScrollPane
的JTable
,其中包含項列表 JDialog
的JPanel
,顯示與所選項目相關的信息 該代碼按預期方式工作(信息已更新),但每次更改選擇時JTable
失去焦點,而JDialog
會獲得焦點。 所以我添加了一個table.requestFocusInWindow
但是JTable
仍然失去了焦點,盡管調用返回true。
如何確保JDialog
得到更新,但JTable
不會失去焦點?
ps:我的最終目標是能夠使用箭頭(上/下)瀏覽表,並在JDialog中查看信息更新-此刻,我需要單擊行來執行此操作。
編輯
請參閱下面的SSCCE,它復制了我的問題(更改選擇但丟失焦點時,JDialog的內容也會更改)。
public class TestTable extends JTable {
public static JFrame f = new JFrame();
public static JTextField text = new JTextField();
public static JDialog dialog;
public static void main(String[] args) {
f.setSize(300, 300);
f.setLocation(300, 300);
f.setResizable(false);
showPopup();
final JScrollPane jScrollPane = new JScrollPane();
jScrollPane.getViewport().add(new TestTable());
f.add(jScrollPane);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public TestTable() {
super();
setModel(new TestTableModel());
getSelectionModel().addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
ListSelectionModel lsm = (ListSelectionModel) e.getSource();
int row = lsm.getAnchorSelectionIndex();
Object item = getModel().getValueAt(row, 0);
text.setText(item.toString());
dialog.setVisible(true);
TestTable.this.requestFocusInWindow(); //DOES NOT DO ANYTHING
}
});
setCellSelectionEnabled(false);
}
public class TestTableModel extends DefaultTableModel {
public TestTableModel() {
super(new String[]{"DATA"}, 3);
setValueAt(Double.valueOf(-0.1), 0, 0);
setValueAt(Double.valueOf(+0.1), 1, 0);
setValueAt(Double.valueOf(0), 2, 0);
}
}
private static void showPopup() {
dialog = new JDialog(f, "Title");
dialog.setContentPane(text);
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.pack();
dialog.setLocationRelativeTo(null);
dialog.setVisible(true);
}
}
您的對話框中一定有一些動態的焦點,因為我嘗試過使用JLabel,它不會引起任何問題。 如果需要解決此問題,則始終可以在JTable父框架上調用toFront()
。 如果對您沒有幫助,請嘗試編輯此SSCCE以重現您的問題。
請參見以下代碼(在標簽上注釋一下grabFocus,以使自己確信對話框中有某些內容可以吸引焦點):
import java.awt.Component;
import java.util.Vector;
import javax.swing.FocusManager;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
public class Test {
public static class MyTableModel extends DefaultTableModel {
private int count;
public MyTableModel() {
addColumn("Test");
}
public void insertNewRow() {
Vector<String> rowData = createNewRowData();
insertRow(0, rowData);
}
private Vector<String> createNewRowData() {
Vector<String> data = new Vector<String>(1);
data.add("Hello-" + count++);
return data;
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Test().initUI();
}
});
}
public void initUI() {
final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final MyTableModel model = new MyTableModel();
final JTable table = new JTable(model);
for (int i = 0; i < 100; i++) {
model.insertNewRow();
}
final JLabel label = new JLabel();
JDialog dialog = new JDialog(frame, false);
dialog.add(label);
dialog.setSize(300, 50);
dialog.setLocation(400, 0);
dialog.setVisible(true);
table.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
final Component focused = FocusManager.getCurrentManager().getFocusOwner();
int index = table.getSelectionModel().getLeadSelectionIndex();
if (index > -1) {
Object valueAt = model.getValueAt(index, 0);
label.setText("Current row selected is: " + valueAt);
} else {
label.setText("No selection");
}
label.grabFocus();
if (focused != null) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
frame.toFront();
}
});
}
}
});
final JScrollPane scroll = new JScrollPane(table);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
frame.add(scroll);
frame.pack();
frame.setVisible(true);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.