Hi, I have a JTable, where on a particular column, I have set the cellEditor as a combobox. When the user clicks on the combo box, I get the ActionEvent for the box, from which I need to popup a dialog box. That dialog box needs to know which row the user clicked on. How does one get that?? Unless the user selects something from the Combobox and table gets updated, thereby generating TableModeListener/tableChanged event, we can get the selected row value. But how does one get the selected row value in the ActionEvent of the combobox?
selvas kumars
Ranch Hand
Joined: Jan 06, 2001
Posts: 115
posted
0
You had tried using table's getSelectedColumn() ,getSelectedRow() method??
anandh
Greenhorn
Joined: Sep 06, 2001
Posts: 22
posted
0
Yes, I did try getSelectedRow().. And I found a strange behavior. When I click on the cell with this combobox editor installed, it gives me -1 as the row index (i.e. no row selected). Then I click outside , and again click on the same cell or any other cell in the same column, I start getting correct row numbers. Weird..any explanation? How do I get over that first getSelectedRow() call returning -1?
raghumohan reddy
Greenhorn
Joined: Oct 07, 2001
Posts: 3
posted
0
hi anandh, add combobox to itemlistener. i am sending the code to u. import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.applet.*; import java.net.*; import java.util.*; import javax.swing.event.*; import javax.swing.table.*; public class AdminEvaluation extends JFrame implements ActionListener,ItemListener { JPanel titlepanel,tablepanel,optionpanel,main; //JLabel lblhead; JScrollPane scrpanel; JButton btnback,btnnext; Vector tablevalues; public static JTable table ; JApplet app; String ideaid; Container con; JComboBox comboBox; public AdminEvaluation() {
//main.add(titlepanel,"North"); main.add(tablepanel,"Center"); main.add(optionpanel,"South"); /* int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS; int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS; scrpanel=new JScrollPane(main,v,h); */ con.add(main, "Center");
} class EditableTableModel extends AbstractTableModel { String colTitles[]; Object dataEntries[][]; int rowCount; public EditableTableModel(String[] colTitles, Object[][] dataEntries) { this.colTitles = colTitles; this.dataEntries = dataEntries; } public int getRowCount(){ return dataEntries.length;} public int getColumnCount(){return colTitles.length;} public Object getValueAt(int row, int column){ return dataEntries[row][column];} public String getColumnName(int column){ return colTitles[column];} public Class getColumnClass(int column){ return getValueAt(0, column).getClass();} public boolean isCellEditable(int row, int column){ return true;} public void setValueAt(Object value, int row, int column){ dataEntries[row][column] = value;} } public void actionPerformed(ActionEvent ae) { if(ae.getSource()==btnnext) {
Originally posted by anandh: Hi, I have a JTable, where on a particular column, I have set the cellEditor as a combobox. When the user clicks on the combo box, I get the ActionEvent for the box, from which I need to popup a dialog box. That dialog box needs to know which row the user clicked on. How does one get that?? Unless the user selects something from the Combobox and table gets updated, thereby generating TableModeListener/tableChanged event, we can get the selected row value. But how does one get the selected row value in the ActionEvent of the combobox?