sql >> Base de Datos >  >> RDS >> Mysql

crear jcomponents usando la base de datos

En primer lugar, vea este sabio consejo de @AndrewThompson:

Hay algunos temas útiles para entender lo que significa aquí:

Verá el uso de métodos como setLocation() , setBounds() o setSize() está muy desanimado. Sin embargo, he visto este enfoque aplicado antes para permitir la personalización de formularios. Pero en lugar de coordenadas específicas (x, y) y fijas (ancho, alto), puede almacenar restricciones para GridBagLayout . Digamos que tienes una tabla como esta:

Comenzaría primero con una clase para envolver datos de la base de datos:

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
}

Como las llamadas a la base de datos son una tarea que consume mucho tiempo, debe considerar usar un SwingWorker para hacer la llamada a la base de datos (tarea que requiere mucho tiempo) en un subproceso en segundo plano y crear/actualizar su GUI en Hilo de envío de eventos .

Habiendo dicho esto, es posible que tenga algo como esto:

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();
            }
        });
    }
}

Y verás algo como esto: