?
Solved

Swing and a table model/view problem

Posted on 2003-02-20
30
Medium Priority
?
178 Views
Last Modified: 2013-11-23
I'm stuck on a problem involving Swing and a table model/view.

For an expert to help solving it, it will probably involving having to look at a few source code files,
which might be too much to post here.  The problem is well-defined and easily reproducible, so it may not actually be a difficult fix, but again, I'm stuck.  I can't write a test program to reproduce the problem without basically duplicating the program.

I thought what I would do is make the app available for download, and if someone can look at it and see if it is of interest. We could then continue from there through email.

You can install the app from:
http://usda-ars.nmsu.edu/slik-eco/Temp/

To create the problem:
Start the app
Click the "Add Texture" button
Type in any string and click OK
Then click the "Add Layer" button
<a new row appears in the table>
Just for fun, click the "Add Layer" button again
<a new row appears in the table>
Now, double click on any cell in of the rows
Then click the "Add Layer" button once more.

The problem is that another row is not added
I've traced through the code and all the data is there
(in this case for 3 rows) but for some reason,
editing a cell, and clicking Add layer causes the row to not display.

I am also open to increasing points because of the extra effort of downloading, etc.


0
Comment
Question by:omom
[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
  • 11
  • 10
  • 9
30 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 7989709
Maybe this is just a shot in the dark, but if you have, by any chance, implemented your own CellEditor, make sure that you call cancelCellEditing() and stopCellEditing()
0
 
LVL 1

Expert Comment

by:membersh
ID: 7991618
I have tried ur application. Actually the rows are added & u can see the rows if u click randomly on the table. U have to stop cellEditing b4 adding the row to the table. add table.stopCellEditing() in ur actionperformed code for the button, where stopCellEditing() is cellEditor.fireEditingStopped().
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 7993832
So it looks like my shot in the dark was a hit then... ;-)
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:omom
ID: 7998850
CEHJ:
You were first on board and your shot in the dark (great movie) appears to be on target, so I'll address this to you.

In the code for the panel, i have:
   myTable = new MyJTable(myTableModel, myColumnModel);
   myTable.setDefaultEditor(String.class, new DefaultCellEditor(new JTextField()));

MyTableModel extends AbstractTableModel (which implements TableModel),
I added cancelCellEditing() and stopCellEditing()
(also, i already had overrides shouldSelectCell() and isCellEditable() )

I am unable to call fireEditingStopped() from stopCellEditing() because fireEditingStopped() is not a member of TableModel,
but rather of AbstractCellEditor (superclass of DefaultCellEditor)

Do i need to subclass DefaultCellEditor in order to call fireEditingStopped() or is there a way to do it as is?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8000661
Maybe i ought to take a look at your code. I'd sooner see it as a jar file than that installer though if possible.

Questions that spring to mind immediately without seeing it though:


a. Why
>>myTable.setDefaultEditor(String.class, new DefaultCellEditor(new JTextField()));

as this is already the default editor?

b. Why have you not used one of the ready-made models such as DefaultTableModel btw?
0
 

Author Comment

by:omom
ID: 8009575
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8010588
And the answers to my questions?
0
 

Author Comment

by:omom
ID: 8011061
sorry...the answer to both is basically because the example(s) i used as a template did it this way.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8011128
So what happened when you tried membersh's suggestion?
0
 

Author Comment

by:omom
ID: 8011622
I am unable to call fireEditingStopped() from stopCellEditing() because fireEditingStopped() is not a member of TableModel, but rather of AbstractCellEditor (superclass of DefaultCellEditor)
0
 

Author Comment

by:omom
ID: 8011732
I am unable to call fireEditingStopped() from stopCellEditing() because fireEditingStopped() is not a member of TableModel, but rather of AbstractCellEditor (superclass of DefaultCellEditor)
0
 
LVL 1

Expert Comment

by:membersh
ID: 8013631
Try this, add
    public void stopCellEditing()
    {
        MyTableCellEditor cellEditor = (MyTableCellEditor)getDefaultEditor(Object.class);
        cellEditor.fireEditingStopped();
    }
to DataJTable class,

setDefaultEditor(Object.class,new MyTableCellEditor(TF));
to third constructor, & finally add subclass
   private class MyTableCellEditor extends DefaultCellEditor
     {
         JComponent m_editComp = null;

             public MyTableCellEditor(JTextField textEdit)
          {
               super(textEdit);
               m_editComp = textEdit;
          }

        public Object getCellEditorValue()
        {
            Object oValue;
             oValue = super.getCellEditorValue();
            return oValue;
        }

          public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
          {
               Component editComponent = super.getTableCellEditorComponent(table, value, isSelected, row, column);
               if(value != null)
               {
                   ((JTextField)editComponent).setText(value.toString());
               }
                 return editComponent;
          }

          public void fireEditingStopped()
          {
              super.fireEditingStopped();
         }
     }
0
 
LVL 1

Expert Comment

by:membersh
ID: 8013642
oops, its not subclass, its inner class. Add it to DataJTable
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8016024
Unfortunately, that zip file has things missing from it. My general advice is to use the default classes wherever possible unless you have a good reason not to. This will also reduce the number of lines of code.

The advice to always extend AbstractTableModel is *bad* advice ;-)
Using DefaultTableModel will also give you useful functionality such as the ability to delete and insert rows.
0
 

Author Comment

by:omom
ID: 8067631
membersh:
I implemented you suggestions and it doesn't seem to fix the problem.  stopCellEditing() and then fireEditingStopped() are called (trace through it), but no dice.
0
 
LVL 1

Expert Comment

by:membersh
ID: 8069233
You are calling stopCellEditing() from mouseClicked() or some method in MouseEventHandler_AddLayer class. Am I right ? I mean u need to make sure that b4 u use setValueAt(), no more editing is allowed in any cell in the table ? In ur setAllComponents() method u r adding all the rows again, [which I think is not necessary. can just add the last row instead] so it is mandatory.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8070955
Of course, all of this is taken care of for you if you use the default classes.
0
 
LVL 1

Expert Comment

by:membersh
ID: 8071001
CellEditing is not stopped by default in default classes. As in this case, row is added at MouseClick/MouseRelease or ... & thus has to be stopped at that point...
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8071022
Well, i'd have to see the code to say, but i'm prepared to believe you, although one is more used to cell editing using the keyboard.
0
 

Author Comment

by:omom
ID: 8072907
I am calling stopCellEditing() on mouseExited() from the table panel. I do this (instead of mouseClicked() for the AddLayer button) because I wanted the save entered data in the case of using the Delete Layer button (and other buttons I might add...eg Duplicate Layer)

0
 
LVL 1

Expert Comment

by:membersh
ID: 8077023
U can do stopCellEditing any no. of times. So try it out. Also in ur SoilTexturePanel constructor, try commenting

  dataTable.setDefaultEditor(String.class,new DefaultCellEditor(new JTextField()));
This may override the DefaultEditor which u r setting in DataJTable. Good luck.

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8079139
>>dataTable.setDefaultEditor(String.class,new DefaultCellEditor(new JTextField()));

As i've said before, that's the default editor anyway, so commenting or uncommenting i doubt will have any effect at all.
0
 
LVL 1

Expert Comment

by:membersh
ID: 8079238
That's exactly what i am trying to say too... If omom has implemented the solution suggested by me ie., has added MyTableCellEditor class, then that is overridden by this statement, thereby Call to stopCellEditing will not work as expected..
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8079264
OK - maybe i'm losing track of where he is now!
0
 

Author Comment

by:omom
ID: 8080968
hmmm..its better.  now i can change any one cell in a row, then click Add Layer and the layer is added and is visible.  If i change more than 2 cells, the problem is the same.

i have updated the soil.zip file (in the same location) so that it contains all source code, jars, and images to build the entire project.
0
 
LVL 1

Expert Comment

by:membersh
ID: 8100382
I had look at ur code. One last try. Put      
  try
        {
             dataTable.stopCellEditing();            
        }
        catch (Exception e)
        { }
at beginning of method setAllComponents() in soilTexturePanel.java. [I haven't compiled, so make changes if necessary.] Even though it appears redundant, pls try this & let us know the result.
0
 

Author Comment

by:omom
ID: 8100556
Added:
try
{
  dataTable.stopCellEditing();            
}
catch (Exception e)
{
  System.out.println (">>>>>>>>>> SoilTexturePanel:setAllComponents(): exception: " + e);
}

The exception is never thrown/caught. :(

0
 
LVL 1

Accepted Solution

by:
membersh earned 1200 total points
ID: 8101285
I changed soilTableModel.setValueAt (soilLayer.getHorizon(),i, Soil.COLUMN_HORIZON);   to
dataTable.setValueAt (soilLayer.getHorizon(),i, (Soil.COLUMN_HORIZON -1)); etc., at all places.
Then added   soilTableModel.fireTableDataChanged(); after
  soilTableModel.setRowCount(soilLayerVector.size()); in
setAllComponents(). &   dataTable.repaint() ; as last statement in setAllComponents()
Compiled & run the prog. Its working as expected.
0
 

Author Comment

by:omom
ID: 8103173
indeed, it works.
it seems to defeat the MVC architecture to do dataTable.setValueAt instead of soilTableModel.setValueAt.
Maybe this is a design flaw of my code.
0
 

Author Comment

by:omom
ID: 8103182
thanks for your efforts!
0

Featured Post

Technology Partners: 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

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…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month9 days, 23 hours left to enroll

762 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