2014-02-23 28 views
1

我想使用數據庫動態創建jcomponents。當我打開jlabel等任何jframe或jpanel組件時,應通過讀取數據庫行來創建jtextfields,jcombobox等。 我很困惑如何從數據庫值中引用,即在字符串中引用jcomponent的對象。 這是我的數據庫表使用數據庫創建jcomponents

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。我會寫循環來多次打印它們。 請幫助我。

+1

那你試試這麼遠嗎?至少是否解決了數據庫連接部分?任何代碼顯示? – dic19

+0

我也在嘗試請不要關閉此問題。我使用正常的結果集爲例如:'jtextfield a = new jtextfiled();''和'a.setSize(rs.getInt(height),rs.getInt(width));' –

+0

請投票重新打開,因爲這個問題已被編輯關閉後。 – dic19

回答

2

首先看到@ AndrewThompson的明智忠告:

Java的圖形用戶界面可能需要在多個平臺上工作,使用不同PLAFs不同 屏幕分辨率&。因此,他們不是 有利於組件的準確放置。要組織可靠的GUI組件 ,請改爲使用佈局管理器或它們的組合,以及用於空白區域的佈局填充&邊框。

有一些有用的主題,明白它的意思在這裏:

你會看到使用諸如setLocation(),setBounds()setSize()的方法是非常不鼓勵的。不過,在應用允許定製表單之前,我已經看到了這種方法。但是,您可以存儲GridBagLayout的約束,而不是特定的(x,y)座標和固定的(寬度,高度)。比方說,你有這樣的表:你必須

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 
} 

由於數據庫調用耗時的任務:

enter image description here

我帶班從DB包數據第一次啓動考慮使用SwingWorker在後臺線程中執行數據庫調用(耗時的任務),並在Event Dispatch Thread中創建/更新GUI。

話雖如此,你可能有這樣的事情:

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

+0

什麼是發佈(Data); –

+0

'publish()'是一個SwingWorker類的方法。它將一個對象放在隊列中,當調用process()方法時將處理該對象。您需要閱讀SwingWorker類,以及它如何同步後臺線程和Event Dispatch Thread。這關係到Swing的併發性。 @Ashish – dic19

+0

什麼是重量和重量在表中。我們爲什麼要使用它。 –