Solved

Edit a JTable using Table Model

Posted on 2001-09-12
6
920 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Python Assistance 7 81
github account with ecipse 1 47
login jsp example 24 50
Facing this issue for maven proxy setting 2 12
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

777 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