簡體   English   中英

單擊(鼠標事件)行時如何在jTable單元格中設置Image?

[英]how set Image in jTable cell when click(Mouse Event) the row?

單擊(鼠標事件)行時如何在jTable單元格中設置圖像?如果選擇第一行,圖像將顯示在該行中。然后單擊第二行,圖像將顯示在第二行中?使用表格單元格渲染器或准備渲染器?

如果只希望圖像顯示在表格單元格中,請為ImageIcon使用默認渲染器 ,並確保TableModel為該列返回ImageIcon.class

如果您希望圖像顯示為響應單擊,請考慮使用TablePopupEditor的變體和TablePopupEditor setClickCountToStart(1)並將圖像作為Icon

這是關於在JTable中顯示圖像的第四個問題,所以我猜您已經知道該怎么做。

因此,如果要在選擇更改時更新行,則需要使用ListSelectionListener。 然后,當偵聽器觸發時,您將需要更新TableModel以從上一行中刪除圖標,並更新當前行中的圖標。

JList:先前選擇的項目顯示您可以獲取要更新的行號。

最好的方法是使自己擁有表格單元格渲染器。

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            if(isSelected){
               return new Image(); // if selected
            }
return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); // if not selected do the normal stuff
}

這樣的事情。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM