Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

uneditable JTable

Posted on 2000-03-21
5
1,854 Views
Last Modified: 2012-06-21
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.ActionListener;import java.awt.event.ActionEvent;
import javax.swing.table.TableColumn;
import javax.swing.table.DefaultTableModel;

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,columnNames);            
            final JTable table = new JTable(data, columnNames);            JScrollPane tspane = new JScrollPane(table);
            
            TableColumn column=table.getColumnModel().getColumn(0);
            column.setPreferredWidth(15);
            column=table.getColumnModel().getColumn(1);
            column.setPreferredWidth(100);
            column=table.getColumnModel().getColumn(2);
            column.setPreferredWidth(20);
            column=table.getColumnModel().getColumn(3);
            column.setPreferredWidth(15);
            
            
                                                 
      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("combo");      tcombo.addActionListener(this);
      
      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.NORTHWEST, GridBagConstraints.BOTH, insets, 0, 0) ;
      GridBagConstraints constraintlower1 = new GridBagConstraints (0, 3, 1, 1, 1.0, 1.0,
      GridBagConstraints.SOUTHWEST, GridBagConstraints.BOTH, insets, 0, 0) ;
      GridBagConstraints constraintlower2 = new GridBagConstraints (0, 3, 1, 1, 1.0, 1.0,
      GridBagConstraints.SOUTHWEST, 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(ActionEvent e){

            if (e.getActionCommand()=="add"){}            { //define credit card            }
            if (e.getActionCommand()=="delete"){}
            { //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?
0
Comment
Question by:hpchong7
  • 3
  • 2
5 Comments
 
LVL 7

Expert Comment

by:conick
ID: 2641547
You seem to be using the wrong JTable constructor.
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);
0
 
LVL 7

Expert Comment

by:conick
ID: 2641560
Wow that description made absolutely no sense.
But the constructor change is valid.
0
 
LVL 2

Author Comment

by:hpchong7
ID: 2642851
So,how can I make the cell uneditable?
0
 
LVL 7

Accepted Solution

by:
conick earned 18 total points
ID: 2644856
When you modified the DefaultTableModel with the following:
dm=new DefaultTableModel(){
    boolean isCellEditable(int row, int column) {
          return false;
    } ;
}
you modified the model (dm) to not allow editing.
This code basically says: I want to use the normal DefaultTableModel but instead of using the default method of isCellEditable()(ie the one that always returns true), I want to use this one (this one always returns false).

However you never said:  I want to use dm as the model  for the new JTable.
You can do this by using a different JTable constructor:
JTable table= new JTable(dm);
instead of
JTable table= new JTable(data,columnNames);

Have you tried that?  That looks to be the only thing wrong.  All the other code seems to be correct.
0
 
LVL 2

Author Comment

by:hpchong7
ID: 2645366
Thank you very much
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Protect jar file - windows app 2 65
mysql jsp example issue 32 48
going to wrong jsp page 2 33
ejb entity bean example issue 2 16
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question