Solved

Edit a JTable using Table Model

Posted on 2001-09-12
6
923 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 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
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

Industry Leaders: 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!

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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 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…

691 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