同步组合盒Java秋千

发布于 2025-02-03 12:14:32 字数 1491 浏览 2 评论 0原文

我想在Java swing 中创建同步组合框。当我选择其中一个省时,Kabupaten(区)组合框将包含所选省的所有地区。但是有一个问题。区组合盒仅选择省份组合框的第一个条目,当我在省份组合框中选择另一个条目时不会更改。为了获得完全同步的组合框,我需要做什么? tia。

public void getProvince() {
    DBConnection dBConnection = new DBConnection();
    dBConnection.connection();
    Statement statement = null;
    try {
        statement = dBConnection.con.createStatement();
        String sql = "SELECT * FROM provinsi";
        ResultSet rs = statement.executeQuery(sql);
        while (rs.next()) {
            String provinceName = rs.getString("nama");
            String provinceId = rs.getString("id");
            provinsiCombo.addItem(provinceId);
        }
        dBConnection.con.close();
    } catch (SQLException e) {
        JOptionPane.showMessageDialog(null, "Gagal Terhubung");
    }
}

public void getKabupaten() {
    DBConnection dBConnection = new DBConnection();
    dBConnection.connection();
    Statement statement = null;
    try {
        statement = dBConnection.con.createStatement();
        String sql = "SELECT * FROM kabupaten WHERE id_prov = '"
                + provinsiCombo.getSelectedItem()
                + "'";
        ResultSet rs = statement.executeQuery(sql);
        while (rs.next()) {
            String kabupaten = rs.getString("nama");
            kabupatenCombo.addItem(kabupaten);
        }
        dBConnection.con.close();
    } catch (SQLException e) {
        JOptionPane.showMessageDialog(null, "Gagal Terhubung");
    }
}

I want to create synchronized combo box in Java Swing. When I choose one of the provinces, the kabupaten (district) combo box will contain all the districts of the selected province. But there's a problem. The district combo box only selects first entry of province combo box and doesn't change when I select another entry in province combo box. What I need to do in order to get fully synchronized combo box? TIA.

public void getProvince() {
    DBConnection dBConnection = new DBConnection();
    dBConnection.connection();
    Statement statement = null;
    try {
        statement = dBConnection.con.createStatement();
        String sql = "SELECT * FROM provinsi";
        ResultSet rs = statement.executeQuery(sql);
        while (rs.next()) {
            String provinceName = rs.getString("nama");
            String provinceId = rs.getString("id");
            provinsiCombo.addItem(provinceId);
        }
        dBConnection.con.close();
    } catch (SQLException e) {
        JOptionPane.showMessageDialog(null, "Gagal Terhubung");
    }
}

public void getKabupaten() {
    DBConnection dBConnection = new DBConnection();
    dBConnection.connection();
    Statement statement = null;
    try {
        statement = dBConnection.con.createStatement();
        String sql = "SELECT * FROM kabupaten WHERE id_prov = '"
                + provinsiCombo.getSelectedItem()
                + "'";
        ResultSet rs = statement.executeQuery(sql);
        while (rs.next()) {
            String kabupaten = rs.getString("nama");
            kabupatenCombo.addItem(kabupaten);
        }
        dBConnection.con.close();
    } catch (SQLException e) {
        JOptionPane.showMessageDialog(null, "Gagal Terhubung");
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月寒剑心 2025-02-10 12:14:32

如果要根据省份组合框的价值动态加载值到District Combobox,则需要使用侦听器。示例如下。最好将加载值从数据库移动到分开类,并在您的应用程序中使用加载值。

更新的代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ItemEvent;

public class SyncronizedComboboxExample extends JPanel
{
  String[] provinces= {"A","B","C"};
  String[] districtsForA = {"l","m","n"};
  String[] districtsForB = {"x","y","z"};
  String[] districtsForC = {"p","q","r"};

  public SyncronizedComboboxExample() {

    JComboBox provincesComboBox = new JComboBox(provinces);

    final DefaultComboBoxModel model = new DefaultComboBoxModel(districtsForA);
    JComboBox districtsComboBox = new JComboBox(model);

    provincesComboBox.addItemListener(e -> {
      if(e.getStateChange() == ItemEvent.SELECTED){
        DefaultComboBoxModel comboBoxModel;

          if(provincesComboBox.getSelectedItem().equals("A")){
            comboBoxModel = new DefaultComboBoxModel(districtsForA);
            districtsComboBox.setModel(comboBoxModel);
          }
          if(provincesComboBox.getSelectedItem().equals("B")){
            comboBoxModel = new DefaultComboBoxModel(districtsForB);
            districtsComboBox.setModel(comboBoxModel);
          }
          if(provincesComboBox.getSelectedItem().equals("C")){
            comboBoxModel = new DefaultComboBoxModel(districtsForC);
            districtsComboBox.setModel(comboBoxModel);
          }

      }
    });

    add(provincesComboBox, BorderLayout.PAGE_START);
    add(districtsComboBox, BorderLayout.AFTER_LAST_LINE);
    setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
  }

  private static void createAndShowGUI() {
    //Create and set up the window.
    JFrame frame = new JFrame("CustomComboBoxDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Create and set up the content pane.
    JComponent newContentPane = new SyncronizedComboboxExample();
    newContentPane.setOpaque(true); //content panes must be opaque
    frame.setContentPane(newContentPane);

    //Display the window.
    frame.pack();
    frame.setVisible(true);
  }

  public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(() -> createAndShowGUI());
  }

}

If you want to load values dynamically to district combobox based on the value of provinces combo box, you need to use listeners. Example is mentioned below. Better to move loading values from DB to separate class and used the loaded values in your application.

Updated code:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ItemEvent;

public class SyncronizedComboboxExample extends JPanel
{
  String[] provinces= {"A","B","C"};
  String[] districtsForA = {"l","m","n"};
  String[] districtsForB = {"x","y","z"};
  String[] districtsForC = {"p","q","r"};

  public SyncronizedComboboxExample() {

    JComboBox provincesComboBox = new JComboBox(provinces);

    final DefaultComboBoxModel model = new DefaultComboBoxModel(districtsForA);
    JComboBox districtsComboBox = new JComboBox(model);

    provincesComboBox.addItemListener(e -> {
      if(e.getStateChange() == ItemEvent.SELECTED){
        DefaultComboBoxModel comboBoxModel;

          if(provincesComboBox.getSelectedItem().equals("A")){
            comboBoxModel = new DefaultComboBoxModel(districtsForA);
            districtsComboBox.setModel(comboBoxModel);
          }
          if(provincesComboBox.getSelectedItem().equals("B")){
            comboBoxModel = new DefaultComboBoxModel(districtsForB);
            districtsComboBox.setModel(comboBoxModel);
          }
          if(provincesComboBox.getSelectedItem().equals("C")){
            comboBoxModel = new DefaultComboBoxModel(districtsForC);
            districtsComboBox.setModel(comboBoxModel);
          }

      }
    });

    add(provincesComboBox, BorderLayout.PAGE_START);
    add(districtsComboBox, BorderLayout.AFTER_LAST_LINE);
    setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
  }

  private static void createAndShowGUI() {
    //Create and set up the window.
    JFrame frame = new JFrame("CustomComboBoxDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Create and set up the content pane.
    JComponent newContentPane = new SyncronizedComboboxExample();
    newContentPane.setOpaque(true); //content panes must be opaque
    frame.setContentPane(newContentPane);

    //Display the window.
    frame.pack();
    frame.setVisible(true);
  }

  public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(() -> createAndShowGUI());
  }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文