java swing jtable jtextarea

java - JTable selección oyente



swing jtextarea (3)

Lea la sección del tutorial de Swing sobre Cómo escribir un oyente de selección de lista .

No puede agregar un campo de texto a la tabla, pero puede agregar un campo de texto y una tabla al mismo marco.

Tengo un código que muestra Table en los applets y consta de dos columnas:

  1. imagen icono
  2. descripción

Aquí está mi código:

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); } }

Ahora lo que quiero saber es cómo puedo implementar el evento listener de oyente de selección o ratón en mi tabla, de modo que seleccione una imagen particular de mi tabla y la visualice en área de texto o campo de texto (mi tabla contiene la ruta del archivo de imagen).

¿Puedo agregar un campo de texto en la tabla & tabla en el marco? Por favor, siéntase libre de hacer preguntas si es necesario.


En mi código tengo una tabla donde establezco el modo de selección única; en mi caso, el oyente descrito en Cómo escribir un escucha de selección de lista (con un bucle for desde getMinSelectionIndex hasta getMaxSelectionIndex) no es útil porque al soltar el botón del mouse estoy seguro de que tengo solo una fila seleccionada.

Así que lo he resuelto de la siguiente manera:

.... 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)); }

Creo que esta solución, que no requiere un bucle for entre inicio y fin para verificar qué elemento se selecciona, es más adecuado cuando la tabla está en modo de selección única


¿Qué tal esto?

protected void handleSelectionEvent(ListSelectionEvent e) { if (e.getValueIsAdjusting()) return; final DefaultListSelectionModel target = (DefaultListSelectionModel)e.getSource(); iSelectedIndex = target.getAnchorSelectionIndex(); }