1

我有一个小型个人应用程序的问题,我将它们放在一起。我正在将数据加载到 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;
}
}
4

0 回答 0