gpt4 book ai didi

java - 在 showOptionDialog 之后将焦点返回到 JTable 内的 JComboBox

转载 作者:行者123 更新时间:2023-11-30 09:15:23 24 4
gpt4 key购买 nike

我在将 JComboBox 用作 JTableCellEditor 时遇到问题。我想在编辑 JComboBox 并按 tab 以显示 OptionsDialog 之后,如果选择了特定选项,焦点将保留在 JComboBox。问题是由于制表符,焦点移动到下一个单元格,我无法将它返回到 JComboBox
下面是我的一个测试用例:

import java.awt.KeyboardFocusManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;

public class TestFocus {

public static void main(String[] args) {

TestFocus test = new TestFocus();
test.go();

}

public void go() {

//create the frame
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// create and add a tabbed pane to the frame
JTabbedPane tabbedPane = new JTabbedPane();
frame.getContentPane().add(tabbedPane);
//create a table and add it to a scroll pane in a new tab
JTable table = new JTable(new DefaultTableModel(new Object[] {"A", "B"}, 5));
JScrollPane scrollPane = new JScrollPane(table);
tabbedPane.addTab("test", scrollPane);

// create a simple JComboBox and set is as table cell editor on column A
Object[] comboElements = {"aaaaa1", "aaaaaa2", "b"};
final JComboBox comboBox = new JComboBox(comboElements);
comboBox.setEditable(true);
table.getColumn("A").setCellEditor(new DefaultCellEditor(comboBox));

// add an action listener for when the combobox is edited to display an options dialog
comboBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("comboBoxEdited")) {
// display an options pane
Object[] options = {"Yes", "No"};
System.out.println(KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner());
int response = JOptionPane.showOptionDialog(SwingUtilities.getWindowAncestor(comboBox),
"Do you want to return the focus to the ComboBox?",
"This is just a test",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
options,
options[0]);
comboBox.requestFocusInWindow();
if (response == 0) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
comboBox.requestFocusInWindow();
}
});
}
System.out.println(KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner());
}

}
});

// pack and show frame
frame.pack();
frame.setVisible(true);

}
}

最佳答案

I'm asking him to confirm the adding

那么您应该创建一个自定义编辑器并覆盖 stopCellEditing() 方法。

这是一个确保输入的数据正好是 5 个字符的示例。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.event.*;
import javax.swing.border.*;
import javax.swing.table.*;

public class TableEdit extends JFrame
{
TableEdit()
{
JTable table = new JTable(5,5);
table.setPreferredScrollableViewportSize(table.getPreferredSize());

JScrollPane scrollpane = new JScrollPane(table);
add(scrollpane);

// Use a custom editor

TableCellEditor fce = new FiveCharacterEditor();
table.setDefaultEditor(Object.class, fce);

add(new JTextField(), BorderLayout.NORTH);
}

class FiveCharacterEditor extends DefaultCellEditor
{
FiveCharacterEditor()
{
super( new JTextField() );
}

public boolean stopCellEditing()
{
JTable table = (JTable)getComponent().getParent();

try
{
String editingValue = (String)getCellEditorValue();

if(editingValue.length() != 5)
{
JTextField textField = (JTextField)getComponent();
textField.setBorder(new LineBorder(Color.red));
textField.selectAll();
textField.requestFocusInWindow();

JOptionPane.showMessageDialog(
null,
"Please enter string with 5 letters.",
"Alert!",JOptionPane.ERROR_MESSAGE);
return false;
}
}
catch(ClassCastException exception)
{
return false;
}

return super.stopCellEditing();
}

public Component getTableCellEditorComponent(
JTable table, Object value, boolean isSelected, int row, int column)
{
Component c = super.getTableCellEditorComponent(
table, value, isSelected, row, column);
((JComponent)c).setBorder(new LineBorder(Color.black));

return c;
}

}

public static void main(String [] args)
{
JFrame frame = new TableEdit();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible(true);
}
}

关于java - 在 showOptionDialog 之后将焦点返回到 JTable 内的 JComboBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19938204/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com