2015-02-23 41 views
0

我有一個JTable 2列,列名和複選框。如果用戶單擊爲數據輸入創建新字段的列名稱旁邊的複選框。我希望取消單擊複選框輸入字段已經消失。Swing刪除JTexField和JLabel

我該怎麼做?

enter image description here

我的代碼添加一個新的領域:

headerTable.getModel().addTableModelListener(new TableModelListener() { 

       @Override 
       public void tableChanged(TableModelEvent e) { 
        if(e.getColumn() >= 0 && e.getFirstRow()>-1){ 
         int id = e.getFirstRow(); 
         String colName = (String)headerTable.getValueAt(e.getFirstRow(), 1); 
         boolean colValue = (boolean)headerTable.getValueAt(e.getFirstRow(), 2); 

         System.out.println("Row : " + e.getFirstRow() + 
            " value :" + headerTable.getValueAt(e.getFirstRow(), 2)); 
         appListener.getColumnId(id); 
         //create texfield 
         if(colValue==false){ 
          System.out.println("Delete"); 
         }       
         jTextField = new JTextField(20); 
         textField.put(id,jTextField); 
         if (textField != null && !textField.isEmpty()) { 
          textField.get(textField.size()-1); 
          System.out.println("Add"); 
         } 
         JLabel kolor1name = new JLabel(colName+": "); 
         operationContent.add(kolor1name,""); 
         operationContent.add(jTextField, "growy, wrap"); 

         revalidate(); 
         repaint(); 
        } 
       } 
      }); 
+0

你當你點擊它時想刪除複選框? – user1627167 2015-02-23 11:27:01

+0

他想取消輸入掩碼(4 textfields),當他取消選中複選框時,我認爲 – dehlen 2015-02-23 11:29:50

+0

選中複選框後,出現的複選框字段會消失。 – lukassz 2015-02-23 14:53:30

回答

1

您可以創建一個矢量所有您想要的字段。當用戶點擊複選框時,您可以將其添加到Vector中。對於如:

Vector fields = new Vector(); 

當你得到通知,你在矢量添加姓名的領域,並調用refreshPanel()方法,它去除了其中的所有領域,並添加新的字段。

// In the tableChanged(...) method 
fields.addElement(colname); 
refreshPanel(); 

然後refreshPanel()方法:

public void refreshPanel() 
{ 
    String fNames[] = (String[]) fields.toArray(); 
    panel.removeAll(); 
    for(String fname : fNames) 
    { 
      //add the fields in `newPanel`, the field name is in `fname` variable 
    } 
    revalidate(); 
    repaint(); 
} 

,並在用戶取消選中的複選框,然後從向量中刪除字段名稱:

fields.removeElement(colname); 
refreshPanel(); 
1

快速正骯髒的方式:

if(colValue){ 
    jTextField = new JTextField(20); 

    textField.put(id,jTextField); 

    if (textField != null && !textField.isEmpty()) { 
     textField.get(textField.size()-1); 
     System.out.println("Add"); 
    } 

    JLabel kolor1name = new JLabel(colName+": "); 
    operationContent.add(kolor1name,""); 
    operationContent.add(jTextField, "growy, wrap"); 

} else {       
    System.out.println("Delete"); 
    Component[] comps = operationContent.getComponents(); 
    operationContent.remove(comps[comps.length - 2]); 
    operationContent.remove(comps[comps.length - 1]); 
} 
revalidate(); 
repaint(); 

但這種方式可以(不總是)引起布點問題(拆除後面板看起來不好)。

更好的辦法:提供面板「operationContent」的全面重新佈局

operationContent.removeAll(); 
// add all the components above the kolor1name 
if(colValue){ 
    jTextField = new JTextField(20); 

    textField.put(id,jTextField); 

    if (textField != null && !textField.isEmpty()) { 
     textField.get(textField.size()-1); 
     System.out.println("Add"); 
    } 

    JLabel kolor1name = new JLabel(colName+": "); 
    operationContent.add(kolor1name,""); 
    operationContent.add(jTextField, "growy, wrap"); 
} 
revalidate() 
repaint(); 

這樣是好的,但在某些情況下也可以提供布點的問題。但是如果你想添加多於一行(例如,當用戶在表格中選擇2個複選框時),它是最適合你的方式。

另一種方法:使用CardLayout。只有當您總是添加一個字段(帶有標籤),而不依賴於表中選擇多少個組合框時,這纔會起作用。

+0

顯示正常,但它總是刪除列表中的最後一個元素,但並非總是如此。例如。我會選擇第二個元素,第三個和第五個元素,然後取消選中第二個元素,並且列表將清除我的第五個元素,而不是第二個元素。 – lukassz 2015-02-23 23:52:13

+0

@ user3193748嘗試第二種方法。只需刪除所有字段,然後根據選定的檢查提供完整的轉發。 – 2015-02-24 08:05:17