?
Solved

Edit a JTable using Table Model

Posted on 2001-09-12
6
Medium Priority
?
925 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
[X]
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
  • 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 200 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses
Course of the Month11 days, 11 hours left to enroll

752 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