我試圖動態創建jlabels和jtextfields,然後添加到jPanel,用戶可以在其中將數據插入到數據庫中的表中。動態創建Jlabel的顏色
我從數據庫中檢索所有表格屬性的名稱,然後我必須爲每個這些名稱創建一個jlabel和一個jtext。它的結果應該是如下所示(如果在JPanel的底部沒有更多的空間應該只創建的標籤5):
我試圖使用的GroupLayout和SequentialGroup但我不能實現類似的東西。我取得了一些完全尷尬的東西,我無法理解並解決它。
稱爲「ESTADO」應該是下的「SIGLA」標籤和帶標籤的標籤「插入表是:」文本應是在屏幕的左手側,如在第一圖片。
我的代碼是:
ArrayList<String[]> tableAtts = sql.getTableAttributeNames(table);
javax.swing.GroupLayout jPDataInsertionLayout = new javax.swing.GroupLayout(jPDataInsertion);
jPDataInsertion.setLayout(jPDataInsertionLayout);
GroupLayout.SequentialGroup shgp = jPDataInsertionLayout.createSequentialGroup();
GroupLayout.SequentialGroup svgp = jPDataInsertionLayout.createSequentialGroup();
jPDataInsertionLayout.setHorizontalGroup(
jPDataInsertionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
.addGroup(shgp));
jPDataInsertionLayout.setVerticalGroup(
jPDataInsertionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
.addGroup(svgp));
//shgp.addContainerGap();
svgp.addContainerGap().addComponent(jLInsertingTable);
for (String[] values : tableAtts) {
JLabel jLTableAtt = new JLabel(values[0]);
JTextField jTFTableAtt = new JTextField();
jTFTableAtt.setColumns(13);
shgp.addComponent(true,jLTableAtt)
.addGap(18, 18, 18)
.addComponent(jTFTableAtt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE);
svgp.addGap(18, 18, 18)
.addGroup(jPDataInsertionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLTableAtt, javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTFTableAtt,javax.swing.GroupLayout.Alignment.BASELINE ,javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE));
}
shgp.addComponent(jLInsertingTable);
編輯:
使用什麼#rcook說,我改變了團體和設法創造我想要的東西。 這裏是我設法創建代碼:
ArrayList<String[]> tableAtts = sql.getTableAttributeNames(table);
ArrayList<JLabel> attLabels = new ArrayList<>();
ArrayList<JTextField> attFields = new ArrayList<>();
javax.swing.GroupLayout jPDataInsertionLayout = new javax.swing.GroupLayout(jPDataInsertion);
jPDataInsertion.setLayout(jPDataInsertionLayout);
GroupLayout.SequentialGroup hsg = jPDataInsertionLayout.createSequentialGroup();
ArrayList<String[]> tempTableAtt = new ArrayList<>(tableAtts);
for(int i = 0; (!tempTableAtt.isEmpty()) && i < COLUMN_SIZE ; i++){
GroupLayout.ParallelGroup innerhpg = jPDataInsertionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false);
for(int j = 0; (!tempTableAtt.isEmpty()) && j < ROW_SIZE ; j++){
String[] att = tempTableAtt.get(0);
GroupLayout.SequentialGroup innerhsg = jPDataInsertionLayout.createSequentialGroup();
JLabel jLTableAtt = new JLabel(att[0]);
JTextField jTFTableAtt = new JTextField();
jTFTableAtt.setColumns(13);
attLabels.add(jLTableAtt);
attFields.add(jTFTableAtt);
innerhsg.addComponent(jLTableAtt)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTFTableAtt);
innerhpg.addGroup(javax.swing.GroupLayout.Alignment.LEADING, innerhsg);
tempTableAtt.remove(att);
}
hsg.addGap(18,18,18).addGroup(innerhpg);
}
jPDataInsertionLayout.setHorizontalGroup(
jPDataInsertionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPDataInsertionLayout.createSequentialGroup()
.addContainerGap()
.addGroup(jPDataInsertionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLInsertingTable)
.addGroup(hsg))));
GroupLayout.SequentialGroup vsg = jPDataInsertionLayout.createSequentialGroup();
vsg.addContainerGap()
.addComponent(jLInsertingTable);
for(int i = 0; i < tableAtts.size() && i < ROW_SIZE; i++){
GroupLayout.ParallelGroup vpg = jPDataInsertionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE);
vpg.addComponent(attLabels.get(i))
.addComponent(attFields.get(i), javax.swing.GroupLayout.PREFERRED_SIZE,javax.swing.GroupLayout.DEFAULT_SIZE,javax.swing.GroupLayout.PREFERRED_SIZE);
for(int j = 0; (tableAtts.size() > ROW_SIZE) && ((i+(ROW_SIZE*(j+1))) < tableAtts.size()) && j < COLUMN_SIZE; j++){
vpg.addComponent(attLabels.get(i+(ROW_SIZE*(j+1))))
.addComponent(attFields.get(i+(ROW_SIZE*(j+1))), javax.swing.GroupLayout.PREFERRED_SIZE,javax.swing.GroupLayout.DEFAULT_SIZE,javax.swing.GroupLayout.PREFERRED_SIZE);
}
vsg.addGap(18, 18, 18).addGroup(vpg);
}
jPDataInsertionLayout.setVerticalGroup(
jPDataInsertionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(vsg));
我不知道這是否是最好的解決方案,但它的工作原理。
我強烈建議看看JGoodies的['FormLayout'](http://www.jgoodies.com/freeware/libraries/forms/)。使用'LayoutManager'輕鬆製作「表格」 – Robin
看起來像更適合2列表('JTable')的數據。爲了更快地獲得更好的幫助,請發佈[SSCCE](http://sscce.org/)。 –