JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Java SE 討論區  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友   
reply to postflat modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 Jtable中的Jcombobox顯示值在自定內容後,一下拉已選值就會跑掉
larrycon





發文: 7
積分: 0
於 2013-07-17 11:14 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我在官方網站中學到如何在JTABLE中加入combobox (TableRenderDemo)
http://docs.oracle.com/javase/tutorial/uiswing/components/table.html

那我試著改寫combobox後,將內容由原來的
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public void setUpSportColumn(JTable table,TableColumn sportColumn) {
        //Set up the editor for the sport cells.
        JComboBox comboBox = new JComboBox();
        comboBox.addItem("Snowboarding");
        comboBox.addItem("Rowing");
        comboBox.addItem("Knitting");
        comboBox.addItem("Speed reading");
        comboBox.addItem("Pool");
        comboBox.addItem("None of the above");
        sportColumn.setCellEditor(new DefaultCellEditor(comboBox));
 
        //Set up tool tips for the sport cells.
        DefaultTableCellRenderer renderer =
                new DefaultTableCellRenderer();
        renderer.setToolTipText("Click for combo box");
        sportColumn.setCellRenderer(renderer);
}
 
 private Object[][] data = {
        {"Kathy", "Smith","Snowboarding", new Integer(5), new Boolean(false)},
        {"John", "Doe","Rowing", new Integer(3), new Boolean(true)},
        {"Sue", "Black","Knitting", new Integer(2), new Boolean(false)},
        {"Jane", "White","Speed reading", new Integer(20), new Boolean(true)},
        {"Joe", "Brown","Pool", new Integer(10), new Boolean(false)}
        };


改成自訂的CLASS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
public void setUpSportColumn(JTable table,
                                 TableColumn sportColumn) {
        //Set up the editor for the sport cells.      
        JComboBox comboBox = new JComboBox();
        comboBox.addItem(new itemClass("Snowboarding","1"));
        comboBox.addItem(new itemClass("Rowing","2"));
        comboBox.addItem(new itemClass("Knitting","3"));
        comboBox.addItem(new itemClass("Speed reading","4"));
        comboBox.addItem(new itemClass("Pool","5"));
        comboBox.addItem(new itemClass("None of the above","6"));
        sportColumn.setCellEditor(new DefaultCellEditor(comboBox));
 
        //Set up tool tips for the sport cells.
        DefaultTableCellRenderer renderer =
                new DefaultTableCellRenderer();
        renderer.setToolTipText("Click for combo box");
        sportColumn.setCellRenderer(renderer);
    }
 
 private Object[][] data = {
        {"Kathy", "Smith",new itemClass("Snowboarding","1"), new Integer(5), new Boolean(false)},
        {"John", "Doe",new itemClass("Rowing","2"), new Integer(3), new Boolean(true)},
        {"Sue", "Black",new itemClass("Knitting","3"), new Integer(2), new Boolean(false)},
        {"Jane", "White",new itemClass("Speed reading","4"), new Integer(20), new Boolean(true)},
        {"Joe", "Brown",new itemClass("Pool","5"), new Integer(10), new Boolean(false)}
        };
 
class itemClass {
  /**
   * 
   */
  private static final long serialVersionUID = 1;
  String itemCode;
  String itemName;
 
  public itemClass(String itemCode, String itemName) {
    this.itemCode = itemCode;
    this.itemName = itemName;
  }
 
  public String getItemName() {
    return this.itemName;
  }
 
  public String getItemCode() {
    return this.itemCode;
  }
 
  public String toString() {
    if (itemName.equals("")) {
      return "";
    } else {
      return itemCode + "-" + itemName;
    }
  }
}


原先範例中,data有預設的選項,若再次下拉時,會釘在原先的選項,下拉清單也會是該選項
改完之後,
table可以正常顯示,但再次下拉時,原先的選項無論是什麼,都會跳回去第一個,下拉式選項也不是預設的選項,

請問該怎麼在自訂combobbox的object之後,還能正確的操作在JTABLE中的Jcombobox呢?


reply to postreply to post
話題樹型展開
人氣 標題 作者 字數 發文時間
838 Jtable中的Jcombobox顯示值在自定內容後,一下拉已選值就會跑掉 larrycon 3433 2013-07-17 11:14
» JWorld@TW »  Java SE 討論區

reply to postflat modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8