fabiofalciPJ 31 de mai. de 2005
da uma olhada neste codigo abaixo… ele esta meio bagunçado… mas tu ja pode
ter uma ideia de como fazer o q tu quer
package table.combo ;
import java.awt.Component ;
import java.awt.event.ActionEvent ;
import java.awt.event.ActionListener ;
import java.util.EventObject ;
import javax.swing.JComboBox ;
import javax.swing.JFrame ;
import javax.swing.JScrollPane ;
import javax.swing.JTable ;
import javax.swing.SwingUtilities ;
import javax.swing.event.CellEditorListener ;
import javax.swing.table.AbstractTableModel ;
import javax.swing.table.TableCellEditor ;
import javax.swing.table.TableCellRenderer ;
/**
* @author fabiofalci
*/
public class ComboTable & # 123 ;
public static void main & # 40 ; String args & # 91 ; & # 93 ; & # 41 ; & # 123 ;
SwingUtilities . invokeLater & # 40 ; new Runnable & # 40 ; & # 41 ; & # 123 ;
public void run & # 40 ; & # 41 ; & # 123 ;
new ComboTable & # 40 ; & # 41 ;;
& # 125 ;
& # 125 ; & # 41 ;;
& # 125 ;
public ComboTable & # 40 ; & # 41 ; & # 123 ;
MyModel model = new MyModel & # 40 ; & # 41 ;;
JTable table = new JTable & # 40 ; model & # 41 ;;
table . setDefaultRenderer & # 40 ; JComboBox . class , new ComboRenderer & # 40 ; & # 41 ; & # 41 ;;
table . setDefaultEditor & # 40 ; JComboBox . class , new ComboEditor & # 40 ; & # 41 ; & # 41 ;;
JScrollPane scroller = new JScrollPane & # 40 ; table & # 41 ;;
JFrame f = new JFrame & # 40 ; & # 41 ;;
f . setDefaultCloseOperation & # 40 ; JFrame . EXIT_ON_CLOSE & # 41 ;;
f . getContentPane & # 40 ; & # 41 ;. add & # 40 ; scroller & # 41 ;;
f . setSize & # 40 ; 500 , 500 & # 41 ;;
f . setVisible & # 40 ; true & # 41 ;;
& # 125 ;
& # 125 ;
class ComboRenderer implements TableCellRenderer & # 123 ;
public Component getTableCellRendererComponent & # 40 ; JTable table , Object value , boolean isSelected , boolean hasFocus , int row , int column & # 41 ; & # 123 ;
return & # 40 ; JComboBox & # 41 ; value ;
& # 125 ;
& # 125 ;
class ComboEditor implements TableCellEditor & # 123 ;
private java . util . Vector listeners = new java . util . Vector & # 40 ; & # 41 ;;
public Component getTableCellEditorComponent & # 40 ; JTable table , Object value , boolean isSelected , int row , int column & # 41 ; & # 123 ;
table . setRowSelectionInterval & # 40 ; row , row & # 41 ;;
table . setColumnSelectionInterval & # 40 ; column , column & # 41 ;;
return & # 40 ; JComboBox & # 41 ; value ;
& # 125 ;
public boolean shouldSelectCell & # 40 ; EventObject o & # 41 ; & # 123 ;
return true ;
& # 125 ;
public void removeCellEditorListener & # 40 ; CellEditorListener l & # 41 ; & # 123 ;
listeners . remove & # 40 ; l & # 41 ;;
& # 125 ;
public void addCellEditorListener & # 40 ; CellEditorListener l & # 41 ; & # 123 ;
listeners . add & # 40 ; l & # 41 ;;
& # 125 ;
public void cancelCellEditing & # 40 ; & # 41 ; & # 123 ;
& # 125 ;
public Object getCellEditorValue & # 40 ; & # 41 ; & # 123 ;
return null ;
& # 125 ;
public boolean isCellEditable & # 40 ; EventObject o & # 41 ; & # 123 ;
return true ;
& # 125 ;
public boolean stopCellEditing & # 40 ; & # 41 ; & # 123 ;
return true ;
& # 125 ;
& # 125 ;
class MyModel extends AbstractTableModel implements ActionListener & # 123 ;
String & # 91 ; & # 93 ; headers = & # 123 ; "Nome" , "SobreNome" , "!!!" , "Combo" & # 125 ;;
Class & # 91 ; & # 93 ; columnTypes = & # 123 ; String . class , String . class , JComboBox . class , JComboBox . class & # 125 ;;
Object & # 91 ; & # 93 ; items1 = & # 123 ; "A" , "B" , "C" & # 125 ;;
JComboBox show1 = new JComboBox & # 40 ; items1 & # 41 ;;
Object & # 91 ; & # 93 ; items2 = & # 123 ; "D" , "E" , "F" & # 125 ;;
JComboBox show2 = new JComboBox & # 40 ; items2 & # 41 ;;
Object & # 91 ; & # 93 ; items3 = & # 123 ; "G" , "H" , "I" & # 125 ;;
JComboBox show3 = new JComboBox & # 40 ; items3 & # 41 ;;
Object & # 91 ; & # 93 ; items4 = & # 123 ; "J" , "K" , "L" & # 125 ;;
JComboBox show4 = new JComboBox & # 40 ; items4 & # 41 ;;
Object & # 91 ; & # 93 ; & # 91 ; & # 93 ; data = & # 123 ; & # 123 ; "Pedro" , "Joao" , show1 , show3 & # 125 ;, & # 123 ; "Mario" , "Conhece" , show2 , show4 & # 125 ; & # 125 ;;
public MyModel & # 40 ; & # 41 ; & # 123 ;
show1 . addActionListener & # 40 ; this & # 41 ;;
show2 . addActionListener & # 40 ; this & # 41 ;;
& # 125 ;
public void actionPerformed & # 40 ; ActionEvent e & # 41 ; & # 123 ;
if & # 40 ; e . getSource & # 40 ; & # 41 ; == show1 & # 41 ; & # 123 ;
System . out . println & # 40 ; "show1" & # 41 ;;
& # 125 ; else if & # 40 ; e . getSource & # 40 ; & # 41 ; == show2 & # 41 ; & # 123 ;
System . out . println & # 40 ; "show2" & # 41 ;;
& # 125 ;
& # 125 ;
public int getRowCount & # 40 ; & # 41 ; & # 123 ;
return data . length ;
& # 125 ;
public int getColumnCount & # 40 ; & # 41 ; & # 123 ;
return headers . length ;
& # 125 ;
public Class getColumnClass & # 40 ; int col & # 41 ; & # 123 ;
return columnTypes & # 91 ; col & # 93 ;;
& # 125 ;
public String getColumnName & # 40 ; int col & # 41 ; & # 123 ;
return headers & # 91 ; col & # 93 ;;
& # 125 ;
public boolean isCellEditable & # 40 ; int row , int col & # 41 ; & # 123 ;
return true ;
& # 125 ;
public Object getValueAt & # 40 ; int row , int col & # 41 ; & # 123 ;
return data & # 91 ; row & # 93 ; & # 91 ; col & # 93 ;;
& # 125 ;