Solved

Edit a JTable using Table Model

Posted on 2001-09-12
6
921 Views
Last Modified: 2012-08-14
I am using a Table Model that i found to help me create a dynamic table(Based on a database).  However
this model i found does not make the table editable, and i cant figure out what i need to do to fix this.
 I have posted the code below that they gave as an example using this table model. I have changed the code below that says false for editable, so now the cells are editable, but i need some help on retrieving the data when a user changes what is shown.
Thanks!




import javax.swing.table.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.awt.*;

public class Main extends JFrame {
  public Main() {
     super("TableModel Demonstration");

     // create our own custom TableModel
     WineTableModel wineModel = new WineTableModel();
     JTable table = new JTable(wineModel);

     // add rows to our TableModel, each row is represented as a Wine object
     wineModel.addWine(new Wine("Chateau Meyney, St. Estephe", "1994", 18.75f, true));
     wineModel.addWine(new Wine("Chateau Montrose, St. Estephe", "1975", 54.25f, true));
     wineModel.addWine(new Wine("Chateau Gloria, St. Julien", "1993", 22.99f, false));
     wineModel.addWine(new Wine("Chateau Beychevelle, St. Julien", "1970", 61.63f, false));
     wineModel.addWine(new Wine("Chateau La Tour de Mons, Margeaux", "1975", 57.03f, true));
     wineModel.addWine(new Wine("Chateau Brane-Cantenac, Margeaux", "1978", 49.92f, false));

     // create the scroll pane and add the table to it.
     JScrollPane scrollPane = new JScrollPane(table);

     // add the scroll pane to this window.
     getContentPane().add(scrollPane, BorderLayout.CENTER);

     addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
           System.exit(0);
        }
     });
  }

  public static void main(String[] args) {
     Main main = new Main();
     main.pack();
     main.setVisible(true);
  }
}

// a simple object that holds data about a particular wine
class Wine {
  private String  name;
  private String  vintage;
  private float   price;
  private boolean inStock;

  public Wine(String name, String vintage, float price, boolean inStock) {
     this.name = name;
     this.vintage = vintage;
     this.price = price;
     this.inStock = inStock;
  }

  public String getName()     { return name; }
  public String getVintage()  { return vintage; }
  public float  getPrice()    { return price; }
  public boolean getInStock() { return inStock; }

  public String toString() {
     return "[" + name + ", " + vintage + ", " + price + ", " + inStock + "]"; }
}

class WineTableModel extends AbstractTableModel {
  // holds the strings to be displayed in the column headers of our table
  final String[] columnNames = {"Name", "Vintage", "Price", "In stock?"};

  // holds the data types for all our columns
  final Class[] columnClasses = {String.class, String.class, Float.class, Boolean.class};

  // holds our data
  final Vector data = new Vector();
 
  // adds a row
  public void addWine(Wine w) {
     data.addElement(w);
     fireTableRowsInserted(data.size()-1, data.size()-1);
  }

  public int getColumnCount() {
     return columnNames.length;
  }
       
  public int getRowCount() {
     return data.size();
  }

  public String getColumnName(int col) {
     return columnNames[col];
  }

  public Class getColumnClass(int c) {
     return columnClasses[c];
  }

  public Object getValueAt(int row, int col) {
     Wine wine = (Wine) data.elementAt(row);
     if (col == 0)      return wine.getName();
     else if (col == 1) return wine.getVintage();
     else if (col == 2) return new Float(wine.getPrice());
     else if (col == 3) return new Boolean(wine.getInStock());
     else return null;
  }

  public boolean isCellEditable(int row, int col) {
     return false;
  }
}
0
Comment
Question by:epastoor
  • 3
  • 3
6 Comments
 
LVL 16

Expert Comment

by:heyhey_
ID: 6478955
try returning true ?

 public boolean isCellEditable(int row, int col) {
    return false;
 }

0
 

Author Comment

by:epastoor
ID: 6479260
right...thats what iwas saying in my comment that i switched, so now i have it returning true which means i can edit the values, however i dont know how to capture them so i can store them back into the database.  thats where i am looking for help.
0
 
LVL 16

Accepted Solution

by:
heyhey_ earned 50 total points
ID: 6479278
you must override setValueAt and perform the appropriate db related actions

public void setValueAt(Object aValue, int rowIndex, int columnIndex)

0
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.

 

Author Comment

by:epastoor
ID: 6480423
how do i get the value that the user typed in on the screen when the user edited the table cell?
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 6481881
do you still have problems ? :)
0
 

Author Comment

by:epastoor
ID: 6482400
no...i figured it all out..i didnt realize how i could use the setvalue by taking the parameter and then setting my database, so that getvalue would get the correct info.

thanks for your help!
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

830 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