2013-04-09 97 views
2

我有顯示錶中的小程序&代碼由兩列組成: -JTable中選擇監聽器

  1. 圖像圖標
  2. 描述

這裏是我的代碼:

import javax.swing.table.*; 

    public class TableIcon extends JFrame 
    { 
    public TableIcon() 
    { 
    ImageIcon aboutIcon = new ImageIcon("about16.gif"); 
    ImageIcon addIcon = new ImageIcon("add16.gif"); 
    ImageIcon copyIcon = new ImageIcon("copy16.gif"); 

    String[] columnNames = {"Picture", "Description"}; 
    Object[][] data = 
    { 
     {aboutIcon, "About"}, 
     {addIcon, "Add"}, 
     {copyIcon, "Copy"}, 
    }; 

    DefaultTableModel model = new DefaultTableModel(data, columnNames); 
    JTable table = new JTable(model) 
    { 
     // Returning the Class of each column will allow different 
     // renderers to be used based on Class 
     public Class getColumnClass(int column) 
     { 
      return getValueAt(0, column).getClass(); 
     } 
    }; 
    table.setPreferredScrollableViewportSize(table.getPreferredSize()); 

    JScrollPane scrollPane = new JScrollPane(table); 
    getContentPane().add(scrollPane); 
} 

public static void main(String[] args) 
{ 
    TableIcon frame = new TableIcon(); 
    frame.setDefaultCloseOperation(EXIT_ON_CLOSE); 
    frame.pack(); 
    frame.setVisible(true); 
    } 

} 

現在我想知道的是如何實現選擇監聽器或鼠標監聽器ev在我的桌子上,這樣它應該從我的表格中選擇一個特定的圖像並顯示在文本區域或文本字段(我的表格包含圖像文件的路徑)?

我可以在表格上添加文字框&表格嗎?如有需要,請隨時提問。

回答

3

閱讀有關How to Write a List Selection Listener的Swing教程的部分。

您不能將一個文本字段添加到表中,但可以將一個文本字段和一個表添加到同一個框架。

+0

或者他可以觸發表格的給定單元格的編輯以使文本字段出現。我的要求不清楚。 – 2013-04-09 16:49:51

+0

@GuillaumePolet我更關心的是將動作監聽器添加到我的表中,它就是它 – puneetverma0711 2013-04-09 18:01:15

+0

表沒有ActionListener。 – camickr 2013-04-09 18:09:32

4

在我的代碼中,我有一個表,我設置單選模式;在我的情況下,在How to Write a List Selection Listener(使用forMinSelectionIndex到getMaxSelectionIndex的for循環)中描述的監聽器沒有用,因爲釋放鼠標按鈕我確信我只選擇了一行。

所以我已經解決如下:

.... 

int iSelectedIndex =-1; 

.... 

JTable jtable = new JTable(tableModel); // tableModel defined elsewhere 
jtable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 

ListSelectionModel selectionModel = jtable.getSelectionModel(); 

selectionModel.addListSelectionListener(new ListSelectionListener() { 
    public void valueChanged(ListSelectionEvent e) { 
     handleSelectionEvent(e); 
    } 
}); 

.... 

protected void handleSelectionEvent(ListSelectionEvent e) { 
    if (e.getValueIsAdjusting()) 
     return; 

    // e.getSource() returns an object like this 
    // javax.swing.DefaultListSelectionModel 1052752867 ={11} 
    // where 11 is the index of selected element when mouse button is released 

    String strSource= e.getSource().toString(); 
    int start = strSource.indexOf("{")+1, 
     stop = strSource.length()-1; 
    iSelectedIndex = Integer.parseInt(strSource.substring(start, stop)); 
} 

我覺得這個解決方案,即不需要爲起點之間循環,並停下來檢查哪些元素是selectes,更適合當桌子處於單選擇模式

+10

請不要這樣做!如果表處於單選模式,那麼'e.getFirstIndex()'或'e.getLastIndex()'會爲您提供選定的行。 然後調用'ListSelectionModel'的任何方法,例如'((ListSelectionModel)e.getSource())。isSelectedIndex(e.getFirstIndex())'。爲了代碼的可理解性... – Matthieu 2015-03-30 11:04:24

3

這個怎麼樣?

 protected void handleSelectionEvent(ListSelectionEvent e) { 
      if (e.getValueIsAdjusting()) 
       return; 

      final DefaultListSelectionModel target = (DefaultListSelectionModel)e.getSource(); 
      iSelectedIndex = target.getAnchorSelectionIndex(); 
     }