简体   繁体   English

摇摆自定义组件

[英]Swing Custom Component

I want to create custom component using swing. 我想使用swing创建自定义组件。 Also I want to connect database to that components. 我也想将数据库连接到该组件。

What I want to do, 我想做的事,

I want to create a custom JComboBox . 我想创建一个自定义JComboBox That combo box name is warehouse . 该组合框名称为Warehouse Also all warehouses in database should be bind to that combo box. 同样,数据库中的所有仓库都应绑定到该组合框。

When I design a form I can use that combo box for warehouse. 设计表单时,可以使用该组合框进行仓库存储。 When I run the program all warehouse will be loaded to combo automatically. 当我运行该程序时,所有仓库都将自动加载为combo。 Because that combo has internal mechanism to load warehouses. 因为该组合具有加载仓库的内部机制。 (How do I write that thing?) (我该怎么写?)

How do I do that? 我怎么做?

I have done a similar thing working with a SQL Database. 我在使用SQL数据库时也做了类似的事情。 Working with a database you need to make use of the ResultSet interface to run your query, and Statement class to create a statement. 使用数据库时,您需要使用ResultSet接口来运行查询,并使用Statement类来创建语句。

public void makeCombo() throws SQLException{
public JComboBox warehouse = new JComboBox();           
try{
            Connection conn = Connect.getConnection();
            String query = "Select ?? FROM ??";
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(query);
            if (rs!=null){
                while (rs.next()){
                    String name = rs.getString(1);
                    warehouse.addItem(name);
                }

            }
            else{
                System.err.println ("Empty combo");
                warehouse.addItem("Empty Combo");
            }
        }
        catch(Exception e){
            e.printStackTrace();
        }

    }

This should populate the warehouse combo with the results from the query. 这应该使用查询结果填充仓库组合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM