Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JTable -Urgent  RSS feed

 
francis varkey
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
your code is in working condition so write CellRenderer using DefaultTableCellRenderer in which you can disable your particular cell.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!