hpchong7
asked on
uneditable JTable
Dear,
How to set a JTable uneditable?I tried to override the isCellEditable method but had erro when compile.Here is the code:
import java.awt.*; //for action and window events
import java.net.*;
import java.io.*;
import java.util.*;
import java.security.cert.*;
import java.security.*;
import javax.swing.*;import java.lang.*;
import sun.misc.*;
import java.awt.event.ActionListe ner;import java.awt.event.ActionEvent ;
import javax.swing.table.TableCol umn;
import javax.swing.table.DefaultT ableModel;
public class wtab1 extends DefaultTableModel implements ActionListener{ public ImageIcon icon;
public JPanel apanel; public static JTable table;
public static JButton cbutton1; //add
public static JButton cbutton2; //delete
public static JLabel label1;
public static JLabel label2; public static JComboBox tcombo;
public static JScrollPane areaSPane; public static DefaultTableModel dm; public wtab1(JTabbedPane tabbedPane)
{ icon = new ImageIcon("middle.gif"); apanel=new JPanel();
apanel.setLayout(null);
Dimension preferred = new Dimension (30, 25) ;
Dimension max = new Dimension (Integer.MAX_VALUE, 25) ;
dm=new DefaultTableModel(){
boolean isCellEditable(int row, int column) {
return false;
} ;
//dm=new DefaultTableModel();
//dm.isCellEditable=false;
Object[][] data = {
{"Master",
"0000-0000-0000-0000","12/ 2001", new Boolean(false)},
{"Master",
"0000-0000-0000-0000","12/ 2001", new Boolean(false)},
{"Master",
"0000-0000-0000-0000","12/ 2001", new Boolean(false)},
{"Master",
"0000-0000-0000-0000","12/ 2001", new Boolean(false)},
};
String[] columnNames = {"Type",
"Number",
"Valid to",
"Certify?"};
dm.setDataVector(data,colu mnNames);
final JTable table = new JTable(data, columnNames); JScrollPane tspane = new JScrollPane(table);
TableColumn column=table.getColumnMode l().getCol umn(0);
column.setPreferredWidth(1 5);
column=table.getColumnMode l().getCol umn(1);
column.setPreferredWidth(1 00);
column=table.getColumnMode l().getCol umn(2);
column.setPreferredWidth(2 0);
column=table.getColumnMode l().getCol umn(3);
column.setPreferredWidth(1 5);
cbutton1 = new JButton ("Add") ;
cbutton1.setPreferredSize (preferred) ;
cbutton1.setMaximumSize (max) ;
cbutton1.setActionCommand( "add"); cbutton1.addActionListener (this);
cbutton2 = new JButton ("Delete") ;
cbutton2.setPreferredSize (preferred) ;
cbutton2.setMaximumSize (max) ;
cbutton2.setActionCommand( "Delete"); cbutton2.addActionListener (this);
Box lower = Box.createHorizontalBox() ;
lower.add (cbutton1);
lower.add (cbutton2);
label1 = new JLabel("Credit card use:");
label2 = new JLabel(" ");
String[] card={"temp1","temp2"}; //the credit cards,will be a vector
tcombo = new JComboBox(card);
Dimension sz=new Dimension();
sz.width=20;
sz.height=25;
tcombo.setPreferredSize (sz) ;
tcombo.setMaximumSize (max) ;
tcombo.setSelectedIndex(0) ;
tcombo.setActionCommand("c ombo"); tcombo.addActionListener(t his);
Box lower2 = Box.createHorizontalBox() ;
lower2.add (label1);
lower2.add (label2);
lower2.add (tcombo);
// Main
Insets insets = new Insets (5, 5, 5, 5) ;
GridBagConstraints constraintupper = new GridBagConstraints (0, 0, 1, 3, 1.0, 4.0,
GridBagConstraints.NORTHWE ST, GridBagConstraints.BOTH, insets, 0, 0) ;
GridBagConstraints constraintlower1 = new GridBagConstraints (0, 3, 1, 1, 1.0, 1.0,
GridBagConstraints.SOUTHWE ST, GridBagConstraints.BOTH, insets, 0, 0) ;
GridBagConstraints constraintlower2 = new GridBagConstraints (0, 3, 1, 1, 1.0, 1.0,
GridBagConstraints.SOUTHWE ST, GridBagConstraints.BOTH, insets, 0, 0) ;
apanel.setLayout (new GridBagLayout()) ;
apanel.add (tspane, constraintupper) ;
apanel.add (lower, constraintlower1) ;
apanel.add(lower2, constraintlower2);
tabbedPane.addTab("Credit card",icon,apanel,"Credit card info");
} public void actionPerformed(ActionEven t e){
if (e.getActionCommand()=="ad d"){} { //define credit card }
if (e.getActionCommand()=="de lete"){}
{ //delete credit card }
}
}
I would appreciate it very much if you can pt out what's wrong with the code.Thanks!BTW,how to detect a selection from user?
How to set a JTable uneditable?I tried to override the isCellEditable method but had erro when compile.Here is the code:
import java.awt.*; //for action and window events
import java.net.*;
import java.io.*;
import java.util.*;
import java.security.cert.*;
import java.security.*;
import javax.swing.*;import java.lang.*;
import sun.misc.*;
import java.awt.event.ActionListe
import javax.swing.table.TableCol
import javax.swing.table.DefaultT
public class wtab1 extends DefaultTableModel implements ActionListener{ public ImageIcon icon;
public JPanel apanel; public static JTable table;
public static JButton cbutton1; //add
public static JButton cbutton2; //delete
public static JLabel label1;
public static JLabel label2; public static JComboBox tcombo;
public static JScrollPane areaSPane; public static DefaultTableModel dm; public wtab1(JTabbedPane tabbedPane)
{ icon = new ImageIcon("middle.gif"); apanel=new JPanel();
apanel.setLayout(null);
Dimension preferred = new Dimension (30, 25) ;
Dimension max = new Dimension (Integer.MAX_VALUE, 25) ;
dm=new DefaultTableModel(){
boolean isCellEditable(int row, int column) {
return false;
} ;
//dm=new DefaultTableModel();
//dm.isCellEditable=false;
Object[][] data = {
{"Master",
"0000-0000-0000-0000","12/
{"Master",
"0000-0000-0000-0000","12/
{"Master",
"0000-0000-0000-0000","12/
{"Master",
"0000-0000-0000-0000","12/
};
String[] columnNames = {"Type",
"Number",
"Valid to",
"Certify?"};
dm.setDataVector(data,colu
final JTable table = new JTable(data, columnNames); JScrollPane tspane = new JScrollPane(table);
TableColumn column=table.getColumnMode
column.setPreferredWidth(1
column=table.getColumnMode
column.setPreferredWidth(1
column=table.getColumnMode
column.setPreferredWidth(2
column=table.getColumnMode
column.setPreferredWidth(1
cbutton1 = new JButton ("Add") ;
cbutton1.setPreferredSize (preferred) ;
cbutton1.setMaximumSize (max) ;
cbutton1.setActionCommand(
cbutton2 = new JButton ("Delete") ;
cbutton2.setPreferredSize (preferred) ;
cbutton2.setMaximumSize (max) ;
cbutton2.setActionCommand(
Box lower = Box.createHorizontalBox() ;
lower.add (cbutton1);
lower.add (cbutton2);
label1 = new JLabel("Credit card use:");
label2 = new JLabel(" ");
String[] card={"temp1","temp2"}; //the credit cards,will be a vector
tcombo = new JComboBox(card);
Dimension sz=new Dimension();
sz.width=20;
sz.height=25;
tcombo.setPreferredSize (sz) ;
tcombo.setMaximumSize (max) ;
tcombo.setSelectedIndex(0)
tcombo.setActionCommand("c
Box lower2 = Box.createHorizontalBox() ;
lower2.add (label1);
lower2.add (label2);
lower2.add (tcombo);
// Main
Insets insets = new Insets (5, 5, 5, 5) ;
GridBagConstraints constraintupper = new GridBagConstraints (0, 0, 1, 3, 1.0, 4.0,
GridBagConstraints.NORTHWE
GridBagConstraints constraintlower1 = new GridBagConstraints (0, 3, 1, 1, 1.0, 1.0,
GridBagConstraints.SOUTHWE
GridBagConstraints constraintlower2 = new GridBagConstraints (0, 3, 1, 1, 1.0, 1.0,
GridBagConstraints.SOUTHWE
apanel.setLayout (new GridBagLayout()) ;
apanel.add (tspane, constraintupper) ;
apanel.add (lower, constraintlower1) ;
apanel.add(lower2, constraintlower2);
tabbedPane.addTab("Credit card",icon,apanel,"Credit card info");
} public void actionPerformed(ActionEven
if (e.getActionCommand()=="ad
if (e.getActionCommand()=="de
{ //delete credit card }
}
}
I would appreciate it very much if you can pt out what's wrong with the code.Thanks!BTW,how to detect a selection from user?
Wow that description made absolutely no sense.
But the constructor change is valid.
But the constructor change is valid.
ASKER
So,how can I make the cell uneditable?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you very much
You assign columns and data to the model and assign the columns and data to the table (View), but you never assign the model to the view.
You assign the data to the JTable but never actually use the new model that you created. When you just assign the data a default model is created (without your changes).
Use
JTable table= new JTable(dm);
instead of
JTable table= new JTable(data,columns);