Hello,
I'm trying to create a Jtabel that will allow the user to alter the cells during runtime in order to edit fields of objects in a database. Unfortunately, I'm unable to get a tabelModel event to fire whenever the tabel is changed by the user during runtime. And, whenever the user edits a cell during runtime, as soon as he/she clicks outside of the cell, it returns to its previous value. So in other words, I need to somehow get the tabelChanged method to be called whenever the user edits or even double clicks on a cell during runtime. Here is the code for the GUI screen:
Its for a school project. Any help would be greatly appreciated.
I'm trying to create a Jtabel that will allow the user to alter the cells during runtime in order to edit fields of objects in a database. Unfortunately, I'm unable to get a tabelModel event to fire whenever the tabel is changed by the user during runtime. And, whenever the user edits a cell during runtime, as soon as he/she clicks outside of the cell, it returns to its previous value. So in other words, I need to somehow get the tabelChanged method to be called whenever the user edits or even double clicks on a cell during runtime. Here is the code for the GUI screen:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.TableModel;
import javax.swing.table.TableColumn;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.*;
import javax.swing.table.*;
public class RecordGUI extends JFrame implements ActionListener, TableModelListener
{
//Create GUI objects which will be used by the methods of the class
JTable table;
JonsTableModel model;
JScrollPane scrollPane;
String data [][];
JButton cmdSearch = new JButton("Search");
JRadioButton optPrice = new JRadioButton("Price");
JRadioButton optSalePrice = new JRadioButton("Sale price");
JRadioButton optPurchased = new JRadioButton("Number purchased");
JRadioButton optSold = new JRadioButton("Number sold");
JRadioButton optDate = new JRadioButton("Date");
JTextField txtStart = new JTextField(8);
JTextField txtEnd = new JTextField(8);
//Create an instance of the layout manager
SpringLayout layout = new SpringLayout();
//Constructs an object of the class, takes no parameters
public RecordGUI(String record) throws IOException
{
//Create the table
model = new JonsTableModel(record);
model.addTableModelListener(this);
table = new JTable(model);
table.setSurrendersFocusOnKeystroke(true);
table.setPreferredScrollableViewportSize(new Dimension (753,400));
table.setSelectionMode(1);
//Alter the sizes of the tableColumns
TableColumn column = null;
for (int i = 0; i < 5; i++)
{
column = table.getColumnModel().getColumn(i);
if (i == 3 || i == 4 || i == 5)
{
column.setPreferredWidth(200); //third column is bigger
}
else
{
column.setPreferredWidth(50);
}//End of if
}//End of for
//Add the table to the scrollpane
scrollPane = new JScrollPane(table);
//Cannot be used in JDK5
//table.setFillsViewportHeight(true);
//Create the button group
ButtonGroup group = new ButtonGroup();
group.add(optPrice);
group.add(optSalePrice);
group.add(optPurchased);
group.add(optSold);
group.add(optDate);
//Set the action listeners
cmdSearch.setActionCommand("search");
//Add the actionlisteners
cmdSearch.addActionListener(this);
//Add the components to the frame
getContentPane().add(scrollPane);
getContentPane().add(cmdSearch);
getContentPane().add(optPrice);
getContentPane().add(optSalePrice);
getContentPane().add(optPurchased);
getContentPane().add(optSold);
getContentPane().add(optDate);
getContentPane().add(txtStart);
getContentPane().add(txtEnd);
//Set the layout for the frame
this.getContentPane().setLayout(layout);
//Set the west and north positions of the components on the frame
layout.putConstraint(SpringLayout.WEST, scrollPane, 15, SpringLayout.WEST, getContentPane());
layout.putConstraint(SpringLayout.NORTH, scrollPane, 130, SpringLayout.NORTH, getContentPane());
layout.putConstraint(SpringLayout.WEST, cmdSearch, 15, SpringLayout.WEST, getContentPane());
layout.putConstraint(SpringLayout.NORTH, cmdSearch, 50, SpringLayout.NORTH, getContentPane());
layout.putConstraint(SpringLayout.WEST, optPrice, 95, SpringLayout.WEST, getContentPane());
layout.putConstraint(SpringLayout.NORTH, optPrice, 90, SpringLayout.NORTH, getContentPane());
layout.putConstraint(SpringLayout.WEST, optSold, 95, SpringLayout.WEST, getContentPane());
layout.putConstraint(SpringLayout.NORTH, optSold, 70, SpringLayout.NORTH, getContentPane());
layout.putConstraint(SpringLayout.WEST, optSalePrice, 95, SpringLayout.WEST, getContentPane());
layout.putConstraint(SpringLayout.NORTH, optSalePrice, 50, SpringLayout.NORTH, getContentPane());
layout.putConstraint(SpringLayout.WEST, optPurchased, 95, SpringLayout.WEST, getContentPane());
layout.putConstraint(SpringLayout.NORTH, optPurchased, 30, SpringLayout.NORTH, getContentPane());
layout.putConstraint(SpringLayout.WEST, optDate, 95, SpringLayout.WEST, getContentPane());
layout.putConstraint(SpringLayout.NORTH, optDate, 10, SpringLayout.NORTH, getContentPane());
layout.putConstraint(SpringLayout.WEST, txtStart, 255, SpringLayout.WEST, getContentPane());
layout.putConstraint(SpringLayout.NORTH, txtStart, 80, SpringLayout.NORTH, getContentPane());
layout.putConstraint(SpringLayout.WEST, txtEnd, 355, SpringLayout.WEST, getContentPane());
layout.putConstraint(SpringLayout.NORTH, txtEnd, 80, SpringLayout.NORTH, getContentPane());
//Set the price button as the default selected button
optDate.setSelected(true);
//Set up methods for the frame
this.setSize(800,600);
this.setLocationRelativeTo(null);
this.setTitle("Add, Delete or Modify Produce");
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}//End of constructor
//Check for a button event
public void actionPerformed(ActionEvent evt)
{
//Check which button was clicked
if (evt.getActionCommand().equals("search"))
{
if(optPrice.isSelected() && safeSearch() == true)
model.search("price",txtStart.getText(),txtEnd.getText());
else if(optSalePrice.isSelected() && safeSearch() == true)
model.search("salePrice",txtStart.getText(),txtEnd.getText());
else if(optPurchased.isSelected() && safeSearch() == true)
model.search("purchased",txtStart.getText(),txtEnd.getText());
else if(optSold.isSelected() && safeSearch() == true)
model.search("sold",txtStart.getText(),txtEnd.getText());
else if(optDate.isSelected() && safeSearch() == true)
{
model.search("date",txtStart.getText(),txtEnd.getText());
}
else
JOptionPane.showMessageDialog(this,"No search type selected.","Error",JOptionPane.ERROR_MESSAGE);
}
else if(evt.getActionCommand().equals("delete"))
{
try
{
model.deleteCell(table.getSelectionModel().getLeadSelectionIndex());
}
catch(IOException e){System.out.println("Unable to delete/nError:/t" + e.getMessage());}
}
repaint();
validate();
}//End of actionPerformed method
Method is not called when the user edits the table during runtime. How do I get this method to be called?
public void tableChanged(TableModelEvent evt)
{
System.out.println("TABLE CHANGED");
}
//Make sure the search is safe
private boolean safeSearch()
{
return true;
}//End of boolean
}//End of class
Its for a school project. Any help would be greatly appreciated.
Last edited: