2

JComboBoxのとして使用される に問題がCellEditorありJTableます。を編集してJComboBoxを押しtabて表示した後OptionsDialog、特定のオプションが選択されている場合は、フォーカスを に残しておきますJComboBox。問題は、タブのためにフォーカスが次のセルに移動し、それを戻すことができないことです。JComboBox
以下は私のテストケースの1つです:

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);

    }
}
4

1 に答える 1