使用数据库创建jcomponents

时间:2014-02-23 15:41:13

标签: java mysql swing

我想使用数据库动态创建jcomponents。当我打开任何jframe或jpanel组件,如jlabel,jtextfields,jcombobox等应该通过读取数据库行来创建。 我很困惑如何从数据库值引用,即在字符串中引用jcomponent的对象。 这是我的数据库表

enter image description here

    try{
        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/db","root","pass");
        stat = con.createStatement();
        ResultSet rs = stat.executeQuery("select * from design");
        while(rs.next()){
            jTextField1 = new JTextField();
            jTextField1.setSize(rs.getInt("height"),rs.getInt("width"));
            jTextField1.setLocation(rs.getInt("x"), rs.getInt("y"));
        }
        rs.close();
        stat.close();
        con.close();
    }
    catch(Exception e){
        System.out.println(e);
    }

这是演示表。 我知道这不起作用,因为我无法与对象和数据库进行通信。 我想在jframe上打印jcomponents。我会写for循环来多次打印它们。 请帮帮我。

1 个答案:

答案 0 :(得分:2)

首先看看@ AndrewThompson的明智建议:

  

Java GUI可能必须在不同的平台上工作   屏幕分辨率&使用不同的PLAF。因此他们不是   有助于准确放置组件。组织组件   对于健壮的GUI,而是使用布局管理器或其组合   它们,以及布局填充和白色空间的边界。

有一些有用的主题可以理解它的含义:

您非常不鼓励使用setLocation()setBounds()setSize()等方法。但是,在应用于允许自定义表单之前,我已经看过这种方法。但是,您可以存储GridBagLayout的约束,而不是特定的(x,y)坐标和固定的(宽度,高度)。我们假设您有一张这样的表:

enter image description here

我首先从一个类开始从数据库中包装数据:

public class Data {
    private String componentType, text;
    private int column, row, width, height, weightX, weightY;

    public Data(String componentType, int column, int row, int width, int height
                ,int weightX, int weightY, String text) {

        this.componentType = componentType;
        this.column = column;
        this.row = row;
        this.width = width;
        this.height = height;
        this.weightX = weightX;
        this.weightY = weightY;
        this.text = text;
   }

   // getters and setters here
}

由于数据库调用是一项耗时的任务,您必须考虑使用SwingWorker在后台线程中执行数据库调用(耗时的任务),并在Event Dispatch Thread中创建/更新GUI。 / p>

说完之后你可能有这样的事情:

public class Demo {

    private JPanel content;
    private JFrame frame;

    private void createAndShowGUI() {        
        content = new JPanel(new GridBagLayout());

        SwingWorker<Void, Data> worker = new SwingWorker<Void, Data>() {
            @Override
            protected Void doInBackground() {                    
                try{
                   Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/db","root","password");
                   Statement stat = con.createStatement();
                   ResultSet rs = stat.executeQuery("select * from TableName");
                   while(rs.next()){
                      String componentType = rs.getString("component");
                      int column = rs.getInt("x");
                      int row = rs.getInt("y");
                      int width = rs.getInt("width");
                      int height = rs.getInt("height");
                      int weightx = rs.getInt("weightx");
                      int weighty = rs.getInt("weighty");
                      String text = rs.getString("text");
                      Data data = new Data(componentType, column, row, width, height
                                          ,weightx, weighty, text);
                      publish(data);
                  }
                  rs.close();
                  stat.close();
                  con.close();
              } catch(Exception e) {
                  System.out.println(e);
              }

                return null;
            }

            @Override
            protected void process(List<Data> chunks) {
                for(Data data : chunks) {

                    JComponent component = null;
                    if(data.getComponentType().equalsIgnoreCase("JTextField")) {
                        component = new JTextField(data.getText());
                    }

                    if(data.getComponentType().equalsIgnoreCase("JComboBox")) {
                        component = new JComboBox();
                    }

                    if(data.getComponentType().equalsIgnoreCase("JLabel")) {
                        component = new JLabel(data.getText());
                    }

                    if(component != null) {
                        GridBagConstraints constraints = new GridBagConstraints();
                        constraints.gridx = data.getColumn();
                        constraints.gridy = data.getRow();
                        constraints.gridwidth = data.getWidth();
                        constraints.gridheight = data.getHeight();
                        constraints.weightx = data.getWeightX();
                        constraints.weighty = data.getWeightY();

                        constraints.anchor = GridBagConstraints.WEST;
                        constraints.fill = GridBagConstraints.BOTH;
                        constraints.insets = new Insets(8,8,8,8);
                        content.add(component, constraints);
                    }

                }
            }

            @Override
            protected void done() {
                frame = new JFrame("Demo");
                frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                frame.getContentPane().add(content);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        };

        worker.execute();
    }


    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Demo().createAndShowGUI();
            }
        });
    }
}

你会看到这样的事情:

enter image description here

相关问题