wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes JTable and combobox  as the cellEditor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable and combobox  as the cellEditor" Watch "JTable and combobox  as the cellEditor" New topic
Author

JTable and combobox as the cellEditor

anandh
Greenhorn

Joined: Sep 06, 2001
Posts: 22
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
You had tried using table's getSelectedColumn() ,getSelectedRow()
method??
anandh
Greenhorn

Joined: Sep 06, 2001
Posts: 22
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
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()
{

con=getContentPane();
this.app=app;
this.ideaid=ideaid;
main=new JPanel();
main.setSize(300,300);
//JScrollPane scroll = new JScrollPane(main, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
main.setLayout(new BorderLayout());
//main.setBackground(java.awt.Color.white);
//titlepanel=new JPanel();
//titlepanel.setBackground(java.awt.Color.white);
tablepanel=new JPanel();
//tablepanel.setBackground(java.awt.Color.white);
optionpanel=new JPanel();
//optionpanel.setBackground(java.awt.Color.white);

//lblhead=new JLabel("Administrator Evaluation");
//lblhead.setFont(new Font("Serif", Font.BOLD, 25));
//titlepanel.add(lblhead);

String[] colTitles = {"Critera","Marks","Critera Description"};
Object[][] dataEntries ={
{"1. Imp ", "1", "Feasibility and ease of implementation"},
{"2. C&B ", "5", "Cost - benefit ratio "},
{"3. CP ", "6", "Commercial potential"},
{"4. Str ", "3", "Fit with Strategy"},
{"5. SP ", "6", "Social potential"},
{"6. CA ", "2", "Competitive advantage"},
{"7. Nov ", "1", "Novelty"},
{"8. EMS ", "1", "Enhances market share"},
{"9. EF", "1", "Environmetal friendly "},
{"10. EIF ", "1", "Employee interests "},
};
TableModel model = new EditableTableModel(colTitles, dataEntries);
table = new JTable(model);
table.createDefaultColumnsFromModel();
String[] num = {"1","2","3","4","5","6","7","8","9","10"};
comboBox = new JComboBox(num);
comboBox.addItemListener(this);
table.getColumnModel().getColumn(1).setCellEditor(new DefaultCellEditor(comboBox));
//table.setSize(50,50);

table.getColumn("Critera").setMaxWidth(75);
table.getColumn("Marks").setMaxWidth(75);
table.getColumn("Critera Description").setMaxWidth(250);

//str=(String)table.getValueAt(0,1);

table.setPreferredScrollableViewportSize(new Dimension(400, 160));

JScrollPane scrollPane = new JScrollPane(table);
tablepanel.add(scrollPane);

//optionpanel.add(new ReviewCon());
btnback=new JButton("Back");
btnback.addActionListener(this);

btnnext=new JButton("Next");
btnnext.addActionListener(this);
optionpanel.add(btnback);
optionpanel.add(btnnext);
JPanel ex=new JPanel();

//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)
{

// JOptionPane.showMessageDialog(this,str);

//Adding table values into vectors
tablevalues=new Vector();
tablevalues.addElement((String)table.getValueAt(0,1));
tablevalues.addElement((String)table.getValueAt(1,1));
tablevalues.addElement((String)table.getValueAt(2,1));
tablevalues.addElement((String)table.getValueAt(3,1));
tablevalues.addElement((String)table.getValueAt(4,1));
tablevalues.addElement((String)table.getValueAt(5,1));
tablevalues.addElement((String)table.getValueAt(6,1));
tablevalues.addElement((String)table.getValueAt(7,1));
tablevalues.addElement((String)table.getValueAt(8,1));
tablevalues.addElement((String)table.getValueAt(9,1));

}
if(ae.getSource()==btnback)
{


}
}
public void itemStateChanged(ItemEvent ie)
{
if(ie.getSource()==comboBox)
{

int col=table.getSelectedColumn();
int row=table.getSelectedRow();
String rvv=comboBox.getSelectedItem().toString();
String ss=col+"--"+row;
table.setValueAt(rvv,row,2);
table.repaint();
}
}
public static void main(String args[])
{
AdminEvaluation ae=new AdminEvaluation();
ae.setSize(400,400);
ae.setVisible(true);
}
}

if u have any doubt pls mail to my id : raghumohan@passindien.com
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?

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTable and combobox as the cellEditor