当焦点定向到另一个组件时,JComboBox停止响应

时间:2013-07-15 19:56:34

标签: jcombobox tablelayout scrollpane

我遇到了小型个人应用程序的问题。我正在将数据加载到JDialog中。在JDialog中,我正在加载多个JPanel,每个JPanel包含一个TableLayout。我正在从加载到JDialog的数据中动态生成行。每行包含一个JComboBox和一个JTextField,用于对该特定数据集的状态发表评论。

我遇到的问题与正在创建的JComboBox组件有关。当JDialog窗口打开时,JComboBox组件是可见的并且正常工作。虽然,只要我将焦点指向不同类型的组件,JComboBox组件就会停止响应鼠标点击。

JComboBox组件没有特殊的逻辑。它们只是填充了四个不同的字符串值,用户应该可以从中选择。这不需要ChangeListener。

另请注意,我将所有内容包装在JDialog中的ScrollPane中。

以下是我在JPanel的TableLayout中创建JComboBox组件的代码:

    private JPanel createColumnPanel() {
    int columnCount = report.getColumns().size();
    int rowCount = (columnCount * 2) - 1;

    SpringLayout layout = new SpringLayout();
    JPanel padding = new JPanel(layout);

    TitledBorder border = BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(), "Column Differences: ");
    border.setTitleJustification(TitledBorder.LEFT);

    double[] columns = new double[] {3, 75, 3, 175, 3, 150, 3, 100, 3, 125};
    double[] rows = new double[rowCount];

    for(int i = 0; i < rowCount; i++) {
        rows[i] = 20;
        if (i + 1 < rowCount)
            rows[i + 1] = 5;
        i++;
    }

    double size[][] = {columns, rows};

    JPanel panel = new JPanel(new TableLayout(size));

    columnStatus = new ArrayList<JComboBox<String>>();

    for(int i = 0; i < columnCount; i++) {
        JComboBox<String> comboBox = new JComboBox<String>(statusLiterals);
        comboBox.setEnabled(true);
        columnStatus.add(comboBox);
    }

    for (int i = 0; i < columnCount; i++) {
        panel.add(new JLabel(report.getColumns().get(i).getSchema().toString()), 1 + ", " + (i * 2));
        panel.add(new JLabel(report.getColumns().get(i).getTableName()), 3 + ", " + (i * 2));
        panel.add(new JLabel(report.getColumns().get(i).getColumnName()), 5 + ", " + (i * 2));
        panel.add(columnStatus.get(i), 7 + ", " + (i * 2));
        panel.add(new JTextField(report.getColumns().get(i).getRequestor()), 9 + ", " + (i * 2));
    }

    panel.setBorder(border);
    panel.setVisible(true);
    panel.setAlignmentX(LEFT_ALIGNMENT);
    panel.setBackground(Color.WHITE);

    layout.putConstraint(SpringLayout.NORTH, panel, 10, SpringLayout.NORTH, padding);
    layout.putConstraint(SpringLayout.SOUTH, padding, 10, SpringLayout.SOUTH, panel);
    layout.putConstraint(SpringLayout.EAST, padding, 10, SpringLayout.EAST, panel);
    layout.putConstraint(SpringLayout.WEST, panel, 10, SpringLayout.WEST, padding);

    padding.add(panel, SwingConstants.CENTER);

    padding.setBackground(Color.WHITE);

    return padding;
}

以下是此特定JDialog的完整代码:

public class ManageRequestsDialog extends JDialog {

private static final long serialVersionUID = 1L;

private ComparisonReport report;

private JPanel outerPanel;
private ScrollPane scrollPane;
private JPanel innerPanel;

private JPanel databasePanel;
private JPanel tablePanel;
private JPanel columnPanel;

private List<JLabel> databaseSchemas = null;
private List<JComboBox<String>> databaseStatus = null;
private List<JTextField> databaseRequestors = null;

private List<JLabel> tableSchemas = null;
private List<JLabel> tableTableNames = null;
private List<JComboBox<String>> tableStatus = null;
private List<JTextField> tableRequestors = null;

private List<JLabel> columnSchemas = null;
private List<JLabel> columnTableNames = null;
private List<JLabel> columnColumnNames = null;
private List<JComboBox<String>> columnStatus = null;
private List<JTextField> columnRequestors = null;

private String[] statusLiterals = DifferenceStatus.getLiterals();

public ManageRequestsDialog (ComparisonReport report) {
    super();
    this.report = report;

    outerPanel = createOuterPanel();
    add(outerPanel);

    setLayout(new GridLayout(1, 1));
    setModal(true);
    setSize(700, 500);
    setBackground(Color.WHITE);
    setResizable(false);
    setLocationRelativeTo(null);
    setVisible(true);
}

private JPanel createOuterPanel() {
    JPanel panel = new JPanel(new GridLayout(1, 1));

    scrollPane = createScrollPane();
    panel.add(scrollPane);

    panel.setVisible(true);

    panel.setBackground(Color.WHITE);

    return panel;
}

private ScrollPane createScrollPane() {
    ScrollPane scrollPane = new ScrollPane();

    innerPanel = createInnerPanel();
    scrollPane.add(innerPanel);

    scrollPane.setVisible(true);
    scrollPane.setBackground(Color.WHITE);

    return scrollPane;
}

private JPanel createInnerPanel() {
    JPanel panel = new JPanel(new BorderLayout());

    if (report.getDatabases().size() > 0) {
        databasePanel = createDatabasePanel();
        panel.add(databasePanel, BorderLayout.NORTH);
    }

    if (report.getTables().size() > 0) {
        tablePanel = createTablePanel();
        panel.add(tablePanel, BorderLayout.CENTER);
    }

    if (report.getColumns().size() > 0) {
        columnPanel = createColumnPanel();
        panel.add(columnPanel, BorderLayout.SOUTH);
    }

    panel.setVisible(true);
    panel.setBackground(Color.WHITE);

    return panel;
}

private JPanel createDatabasePanel() {
    int databaseCount = report.getDatabases().size();
    int rowCount = (databaseCount * 2) - 1;

    SpringLayout layout = new SpringLayout();
    JPanel padding = new JPanel(layout);

    TitledBorder border = BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(), "Database Differences: ");
    border.setTitleJustification(TitledBorder.LEFT);

    double[] columns = new double[] {3, 75, 3, 75, 3, 150};
    double[] rows = new double[rowCount];

    for(int i = 0; i < rowCount; i++) {
        rows[i] = 20;
        if (i + 1 < rowCount)
            rows[i + 1] = 5;
        i++;
    }

    double size[][] = {columns, rows};

    JPanel panel = new JPanel(new TableLayout(size));

    databaseStatus = new ArrayList<JComboBox<String>>();

    for(int i = 0; i < databaseCount; i++) {
        JComboBox<String> comboBox = new JComboBox<String>(statusLiterals);
        comboBox.setEnabled(true);
        databaseStatus.add(comboBox);
    }

    for (int i = 0; i < databaseCount; i++) {
        panel.add(new JLabel(report.getDatabases().get(i).getSchema().toString()), 1 + ", " + (i * 2));
        panel.add(databaseStatus.get(i), 3 + ", " + (i * 2));
        panel.add(new JTextField(report.getDatabases().get(i).getRequestor()), 5 + ", " + (i * 2));
    }

    panel.setBorder(border);
    panel.setVisible(true);
    panel.setAlignmentX(LEFT_ALIGNMENT);
    panel.setBackground(Color.WHITE);

    layout.putConstraint(SpringLayout.NORTH, panel, 10, SpringLayout.NORTH, padding);
    layout.putConstraint(SpringLayout.SOUTH, padding, 10, SpringLayout.SOUTH, panel);
    layout.putConstraint(SpringLayout.EAST, padding, 10, SpringLayout.EAST, panel);
    layout.putConstraint(SpringLayout.WEST, panel, 10, SpringLayout.WEST, padding);

    padding.add(panel, SwingConstants.CENTER);

    padding.setBackground(Color.WHITE);

    return padding;
}

private JPanel createTablePanel() {
    int tableCount = report.getTables().size();
    int rowCount = (tableCount * 2) - 1;

    SpringLayout layout = new SpringLayout();
    JPanel padding = new JPanel(layout);

    TitledBorder border = BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(), "Table Differences: ");
    border.setTitleJustification(TitledBorder.LEFT);

    double[] columns = new double[] {3, 75, 3, 175, 3, 100, 3, 150};
    double[] rows = new double[rowCount];

    for(int i = 0; i < rowCount; i++) {
        rows[i] = 20;
        if (i + 1 < rowCount)
            rows[i + 1] = 5;
        i++;
    }

    double size[][] = {columns, rows};

    JPanel panel = new JPanel(new TableLayout(size));

    tableStatus = new ArrayList<JComboBox<String>>();

    for(int i = 0; i < tableCount; i++) {
        JComboBox<String> comboBox = new JComboBox<String>(statusLiterals);
        comboBox.setEnabled(true);
        tableStatus.add(comboBox);
    }

    for (int i = 0; i < tableCount; i++) {
        panel.add(new JLabel(report.getTables().get(i).getSchema().toString()), 1 + ", " + (i * 2));
        panel.add(new JLabel(report.getTables().get(i).getTableName()), 3 + ", " + (i * 2));
        panel.add(tableStatus.get(i), 5 + ", " + (i * 2));
        panel.add(new JTextField(report.getTables().get(i).getRequestor()), 7 + ", " + (i * 2));
    }

    panel.setBorder(border);
    panel.setVisible(true);
    panel.setAlignmentX(LEFT_ALIGNMENT);
    panel.setBackground(Color.WHITE);

    layout.putConstraint(SpringLayout.NORTH, panel, 10, SpringLayout.NORTH, padding);
    layout.putConstraint(SpringLayout.SOUTH, padding, 10, SpringLayout.SOUTH, panel);
    layout.putConstraint(SpringLayout.EAST, padding, 10, SpringLayout.EAST, panel);
    layout.putConstraint(SpringLayout.WEST, panel, 10, SpringLayout.WEST, padding);

    padding.add(panel, SwingConstants.CENTER);

    padding.setBackground(Color.WHITE);

    return padding;
}

private JPanel createColumnPanel() {
    int columnCount = report.getColumns().size();
    int rowCount = (columnCount * 2) - 1;

    SpringLayout layout = new SpringLayout();
    JPanel padding = new JPanel(layout);

    TitledBorder border = BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(), "Column Differences: ");
    border.setTitleJustification(TitledBorder.LEFT);

    double[] columns = new double[] {3, 75, 3, 175, 3, 150, 3, 100, 3, 125};
    double[] rows = new double[rowCount];

    for(int i = 0; i < rowCount; i++) {
        rows[i] = 20;
        if (i + 1 < rowCount)
            rows[i + 1] = 5;
        i++;
    }

    double size[][] = {columns, rows};

    JPanel panel = new JPanel(new TableLayout(size));

    columnStatus = new ArrayList<JComboBox<String>>();

    for(int i = 0; i < columnCount; i++) {
        JComboBox<String> comboBox = new JComboBox<String>(statusLiterals);
        comboBox.setEnabled(true);
        columnStatus.add(comboBox);
    }

    for (int i = 0; i < columnCount; i++) {
        panel.add(new JLabel(report.getColumns().get(i).getSchema().toString()), 1 + ", " + (i * 2));
        panel.add(new JLabel(report.getColumns().get(i).getTableName()), 3 + ", " + (i * 2));
        panel.add(new JLabel(report.getColumns().get(i).getColumnName()), 5 + ", " + (i * 2));
        panel.add(columnStatus.get(i), 7 + ", " + (i * 2));
        panel.add(new JTextField(report.getColumns().get(i).getRequestor()), 9 + ", " + (i * 2));
    }

    panel.setBorder(border);
    panel.setVisible(true);
    panel.setAlignmentX(LEFT_ALIGNMENT);
    panel.setBackground(Color.WHITE);

    layout.putConstraint(SpringLayout.NORTH, panel, 10, SpringLayout.NORTH, padding);
    layout.putConstraint(SpringLayout.SOUTH, padding, 10, SpringLayout.SOUTH, panel);
    layout.putConstraint(SpringLayout.EAST, padding, 10, SpringLayout.EAST, panel);
    layout.putConstraint(SpringLayout.WEST, panel, 10, SpringLayout.WEST, padding);

    padding.add(panel, SwingConstants.CENTER);

    padding.setBackground(Color.WHITE);

    return padding;
}
}

0 个答案:

没有答案