Solved

Edit a JTable using Table Model

Posted on 2001-09-12
6
918 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 Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
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.

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now