Bridge the gap - Local JTable, Global Action?
Hi,
I have the following method in a program that creates a form, runs a query against a database based on the given data and displays the results in a JScrollPane, which is global. I've made the JTable and its TableModel local as well as its customized popup MouseListener. For updates it works okay. Deletions is where it gets muddled. The MouseListener detects a right click on the table and brings up a popup menu to delete a cell, however the popup menu's click is registered by the ActionListener implemented by the class.
I'm stumped as to how to reference the local table from the global actionPerformed(ActionEvent e) method. Is there some sort of method where I can say:
JScrollPane.getTable().getModel().removeRow(
JScrollPane.getTable().getSelectedRow())
in the actionPerformed method?
public void showUpdates()
{
String sel = "Select updated from " +
"gpd_pmEventTable where " +
"id = " + idbx.getText() + " order by updated desc";
//Create custom table model
dataModel dModel = new dataModel(sel);
//assign this model as the table's data model
JTable subResult = new JTable(dModel);
subResult.setDefaultRenderer(java.util.Date.class,
new DateRenderer());
subResult.setPreferredScrollableViewportSize
(dateScrPane.getPreferredSize());
dateScrPane.setViewportView(subResult);
MouseListener popupListener = new PopupListener();
subResult.addMouseListener(popupListener);
}
Thanks
[1561 byte] By [
beverand] at [2007-9-26 12:36:01]

Hi,
You should centralize all Listeners in the most upper global scope of your application. This you optimize you source code, you would not have duplication of source code...
The principle is
this.SomeClass.SomeChildClass.addSomeListsner(this);
of course this line must be in this, the most upper class. "this" must implements all the proper listeners. The best way is to create a method that gets and fetchs all the Component in your application initialy and add the proper listeners.
JRG
j-rg at 2007-7-2 10:01:27 >

I only want to create the variables that are needed when they are needed. I figured that, instead of making a slew of global variables, I would start with every thing that is necessary to the form. As the user wants to do a certain action additional variables, local variables would be referenced in global objects. So instead of always having a table and a mouselistener and a popup menu, all I would have is the line:
if(e.getSource() == delete)
If the user follows a sequence of events that does lead to having a table which has a mouselistener which calls a popup, then if(e.getSource() == delete) will evaluate to true. Is my approach the wrong approach?
I need to know exactly what you want for a behavior... like you want to do what in the ActionPerformed... where it is located? On what the Listener must be and to do what!!!JRG
j-rg at 2007-7-2 10:01:27 >

A user should be able to update, add and "remove" (set inactive) tasks. Currently I have a main form (JFrame)comprised of text fields, buttons, combo boxes, a scroll pane and their respective action listeners - all global and all necessary in the inital presentation of the form. In addition to that I have an additional object that contains a JFrame, JScrollPane and a JTable initalized by a JTable. This object is for a user to view information returned by a generalized query, ex. all records updated between 11/1/2001 and 11/7/2001.
The user can also query by a specific id number.
Next, consider this 1b, the scroll pane in the form which offers up a table of the entries (dates) found in the "EventTable". If there are no dates, there are no rows, if there are no rows there should be no mouselistener and no popup menu to delete rows. I call this procedure whenever the updates one particular field in the form or queries a task. This is the procedure:
public void showUpdates()
{
String sel = "Select updated from gpd_pmEventTable where " +
"id = " + idbx.getText() + " order by updated desc";
//Create custom table model
dataModel dModel = new dataModel(sel);
//assign this model as the table's data model
JTable subResult = new JTable(dModel);
//create popup listener
MouseListener popupListener = new PopupListener();
subResult.setDefaultRenderer(java.util.Date.class, new DateRenderer());
subResult.setPreferredScrollableViewportSize(dateScrPane.getPreferredSize());
dateScrPane.setViewportView(subResult);
subResult.addMouseListener(popupListener);
}
I have a global action listener that listens one of the things it listens for is delete (global), however this delete is put into a local popup table:
class PopupListener extends MouseAdapter
{
//create popup menu
JPopupMenu popup = new JPopupMenu();
public void mousePressed(MouseEvent e)
{
maybeShowPopup(e);
}
public void mouseReleased(MouseEvent e)
{
maybeShowPopup(e);
}
private void maybeShowPopup(MouseEvent e)
{
//add global menu item to local popup menu
popup.add(delete);
if (e.isPopupTrigger())
{
popup.show(e.getComponent(),e.getX(), e.getY());
}
}
}
To remove a row I have these lines in my action listener when delete is pressed.
JTable t = (JTable) dateScrPane.getViewport().getView();((dataModel)t.getModel()).removeRow(t.getSelectedRow());
Third, a user should be able to update add or remove subtask, which may contain data if it exists for that record in the database. This object is similar to the query object except that it has buttons. It is initalized by a string. This object is created only if the user wants to modify or view this information and there is a non-null record in the form. Otherwise the button will not be visible to bring up the subtask form. This feature works but the drawing of the table is very slow.
The main issue that I am having is updating the table local to the query object. I'm trying to figure out how to refresh it from the database. The user can only modify one row at a time, so I'm not looking to redraw the entire table. I've tried revalidate, and this:
((dataModel)qryObj.table.getTableModel())fireTableDataChanged();
as well as the fireTableRowsUpdated(selectedRow, selectedRow), they didn't work.
Any advice you can offer on my design or the table update is greatly appreciated.
Thanks in advance.
