• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JTable -Urgent

 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic