wood burning stoves*
The moose likes Swing / AWT / SWT and the fly likes JTable -Urgent Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable -Urgent" Watch "JTable -Urgent" New topic
Author

JTable -Urgent

francis varkey
Ranch Hand

Joined: Sep 13, 2005
Posts: 166
Respected Sir,

I want to disable perticular cell in a JTable.

I wrote a program, but I got null pointer Exception.

Please check,
--------------------------------
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.table.*;
class Text extends JFrame implements ActionListener

{

DefaultTableModel model;
JTable table;
JScrollPane js;
JButton b;
JComboBox comboBox1,comboBox2;
TableColumn dropDownColumn1,dropDownColumn2;
Text()
{
getContentPane().setLayout(null);

b=new JButton("click");
String str[]={"id","name"};
model=new DefaultTableModel(str,1);
table=new JTable(model);
js=new JScrollPane(table,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED ,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);





dropDownColumn1 = table.getColumnModel().getColumn(0);
dropDownColumn2 = table.getColumnModel().getColumn(1);
comboBox1 = new JComboBox();

comboBox1.addItem("Item 1");
comboBox1.addItem("Item 2");
comboBox1.addItem("Item 3");
comboBox1.addItem("Item 4");
comboBox1.addItem("Item 5");

comboBox2 = new JComboBox();

comboBox2.addItem("ABC");
comboBox2.addItem("PQR");
comboBox2.addItem("XYZ");

dropDownColumn1.setCellEditor(new DefaultCellEditor(comboBox1));
dropDownColumn2.setCellEditor(new DefaultCellEditor(comboBox2));


dropDownColumn1.setCellEditor(new DefaultCellEditor(comboBox1));
dropDownColumn2.setCellEditor(new DefaultCellEditor(comboBox2));

Container con=getContentPane();
con.setLayout(new BorderLayout());
con.add(js,BorderLayout.WEST);
con.add(b,BorderLayout.EAST);
setSize(300,300);
setVisible(true);

b.addActionListener(this);


}

public void actionPerformed(ActionEvent ae)
{

JComboBox jf= (JComboBox)table.getEditorComponent();

jf.setEditable(false);
}




public static void main(String[] args)
{
new Text();

}
}

--------------------------------


thanks,
Francis
prashant gour
Ranch Hand

Joined: Feb 07, 2006
Posts: 45
your code is in working condition so write CellRenderer using DefaultTableCellRenderer in which you can disable your particular cell.
 
Don't get me started about those stupid light bulbs.
 
subject: JTable -Urgent
 
Similar Threads
Edit cell with another JTable
JTable selected row problem
JTable and combobox as the cellEditor
jtable combo box value
MultiComponent JTable