• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help on Displaying Query Result in JTable

 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gratelful if you can help me on this. I create a tablemodel class that extends the abstractTableModel for implementing the JTable. When I run the FBN program, the JTable will initially display only the header, but not any row. It is not until the user provides a critera and displays the result in rows.
Inside my TableModel, I have the appended method for setting the value at a cell given a row and a column. Also, the rowData is set to initialized to nothing.
Now the problem comes up. My program bombs at the if statement inside the setValeAt method when displaying the query result. Of course, it bombs because the rowData is set to nothing initally unless I set it to "" i.e. String[][] rowData = {{"", "", ""}, {"","".... to make the JTable display empty rows. However, I cannot have the JTable displaying 10000 of empty rows if the query result contains 10000 records. I know I have missed something. How can I tackle this?
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Conceptually, your model shouldn't care whether the rows contain empty data or not. If it can display it, then do so. Your method checks whether the row and column exists and then display the string representation. What you're missing is the check on the array itself. Check whether rowData is null and also check whether the indexes are valid in that it doesn't go beyond the length of the arrays.
<pre>
public void setValueAt(Object obj, int r, int c)
{
if (rowData != null && r < rowData.length && c < rowData[r].length)
{
rowData[r][c] = obj.toString();
fireTableCellUpdated(r,c);
}
}
</pre>
I'm not sure why you check the schema which I assume contains the field headings, etc.
 
Rudy Yeung
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sam,
The problem comes up when the row I want to put in does not exist. For example, I provide a criteria to the database, and it returns me 10 records. This means that I need to have 10 rows in the JTable to display the records. Unfortutely, the rowData[][] in my table model is set to {}. Therefore, the program will bomb. How am I gonna to handle this. The only way I can think of is to know the number of records in the database in advance and then allocate the rowData in the table model constructor i.e. rowData[][] = new String[recCount][colCount]. But doing this will display all the empty cells. I know there must be a better way to handle this.
 
Rudy Yeung
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's rephrase my question this way. How do you real time removing a row from or adding a row to the JTable upon user interaction? When I say removing a row, I am not talking about removing the contents in a row and leaving it blank. I am talking about physically removing the row. After you remove the row, and then later on, you decide to add it back, how are you going to handle the adding then?
 
Rudy Yeung
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hohoho, I solve the problem myself. It gets something to do with the methods in the abstract table model. Thank you.
 
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you tried using "setModel"? AND making sure you implment all the the appropriate methods to display the contents?...
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rudy I am trying to implement your example to my JTable how did you get it to work any other replies welcome.
Thanks Randy
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all
I also had the problem before discussed here. But, after trying many times, i finally gave up to implements setValue()method in table model, i just used a vector to hold my table Data, every time the vector get a new element, i just call fireTableRowInserted() method. in this way table will auto increse a new row. of couse the vector initialed as null and table initialed with two empty rows. using fireXXXXXX method will help lot, because it made tabe reload data with calling getValue() method(a key method)
help this help
David
 
Rudy Yeung
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Randy,
You also need to implement getColumnName(), getRowCount(), getColumnCount() to do the job. For the table updating on the screen, you can use the fireXXXXX methods under AbstractTableModel to insert and delete the rows.
 
Randy Valadez
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks very much I will work on that..
 
Randy Valadez
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a class FlyTableModel I have put it in the suncertify.db package but methods not found also I will include my errors with this class when I compile...How do I get the reference to DataClass
package suncertify.db;
import javax.swing.table.*;
import suncertify.db.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.lang.Throwable.*;
import java.io.*;
public class FlyTableModel extends AbstractTableModel
{

Data fly = new Data("db.db");
public int getColumnCount()
{
return getFieldInfo().length;
}
public int getRowCount()
{
return getRecordCount();
}
public Object getValueAt(int row, int col)
{
String[] values = (String[])valueDInfo.toArray(new String[0]);
return values[row][col];
}
public String getColumnName(int col)
{
return getFieldInfo()[col].getName();
}
}
errors////////////////////////////////////errors/////
C:\scjd\starting\suncertify\db\FlyTableModel.java:13: Exception java.io.IOException can't be thrown in initializer.
Data fly = new Data("db.db");
^
C:\scjd\starting\suncertify\db\FlyTableModel.java:16: Method getFieldInfo() not found in class suncertify.db.FlyTableModel.
return getFieldInfo().length;
^
C:\scjd\starting\suncertify\db\FlyTableModel.java:23: Method getRecordCount() not found in class suncertify.db.FlyTableModel.
return getRecordCount();
^
C:\scjd\starting\suncertify\db\FlyTableModel.java:29: Undefined variable or class name: valueDInfo
String[] values = (String[])valueDInfo.toArray(new String[0]);
^
C:\scjd\starting\suncertify\db\FlyTableModel.java:31: [] can only be applied to arrays. It can't be applied to java.lang.String.
return values[row][col];
^
C:\scjd\starting\suncertify\db\FlyTableModel.java:39: Method getFieldInfo() not found in class suncertify.db.FlyTableModel.
return getFieldInfo()[col].getName();
^
6 errors
Process completed with exit code 1
Thanks in Advance Randy
also when I want to populate my JTable from the client is this appropiate::::::given my above class:::::
FlyTableModel model = new FlytableModel();
JTable table = new JTable(model);
 
Randy Valadez
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a class FlyTableModel I have put it in the suncertify.db package but methods not found also I will include my errors with this class when I compile...How do I get the reference to DataClass
package suncertify.db;
import javax.swing.table.*;
import suncertify.db.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.lang.Throwable.*;
import java.io.*;
public class FlyTableModel extends AbstractTableModel
{

Data fly = new Data("db.db");
public int getColumnCount()
{
return getFieldInfo().length;
}
public int getRowCount()
{
return getRecordCount();
}
public Object getValueAt(int row, int col)
{
String[] values = (String[])valueDInfo.toArray(new String[0]);
return values[row][col];
}
public String getColumnName(int col)
{
return getFieldInfo()[col].getName();
}
}
errors////////////////////////////////////errors/////
C:\scjd\starting\suncertify\db\FlyTableModel.java:13: Exception java.io.IOException can't be thrown in initializer.
Data fly = new Data("db.db");
^
C:\scjd\starting\suncertify\db\FlyTableModel.java:16: Method getFieldInfo() not found in class suncertify.db.FlyTableModel.
return getFieldInfo().length;
^
C:\scjd\starting\suncertify\db\FlyTableModel.java:23: Method getRecordCount() not found in class suncertify.db.FlyTableModel.
return getRecordCount();
^
C:\scjd\starting\suncertify\db\FlyTableModel.java:29: Undefined variable or class name: valueDInfo
String[] values = (String[])valueDInfo.toArray(new String[0]);
^
C:\scjd\starting\suncertify\db\FlyTableModel.java:31: [] can only be applied to arrays. It can't be applied to java.lang.String.
return values[row][col];
^
C:\scjd\starting\suncertify\db\FlyTableModel.java:39: Method getFieldInfo() not found in class suncertify.db.FlyTableModel.
return getFieldInfo()[col].getName();
^
6 errors
Process completed with exit code 1
Thanks in Advance Randy
also when I want to populate my JTable from the client is this appropiate::::::given my above class:::::
FlyTableModel model = new FlytableModel();
JTable table = new JTable(model);
 
Randy Valadez
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a class FlyTableModel I have put it in the suncertify.db package but methods not found also I will include my errors with this class when I compile...How do I get the reference to DataClass
package suncertify.db;
import javax.swing.table.*;
import suncertify.db.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.lang.Throwable.*;
import java.io.*;
public class FlyTableModel extends AbstractTableModel
{

Data fly = new Data("db.db");
public int getColumnCount()
{
return getFieldInfo().length;
}
public int getRowCount()
{
return getRecordCount();
}
public Object getValueAt(int row, int col)
{
String[] values = (String[])valueDInfo.toArray(new String[0]);
return values[row][col];
}
public String getColumnName(int col)
{
return getFieldInfo()[col].getName();
}
}
errors////////////////////////////////////errors/////
C:\scjd\starting\suncertify\db\FlyTableModel.java:13: Exception java.io.IOException can't be thrown in initializer.
Data fly = new Data("db.db");
^
C:\scjd\starting\suncertify\db\FlyTableModel.java:16: Method getFieldInfo() not found in class suncertify.db.FlyTableModel.
return getFieldInfo().length;
^
C:\scjd\starting\suncertify\db\FlyTableModel.java:23: Method getRecordCount() not found in class suncertify.db.FlyTableModel.
return getRecordCount();
^
C:\scjd\starting\suncertify\db\FlyTableModel.java:29: Undefined variable or class name: valueDInfo
String[] values = (String[])valueDInfo.toArray(new String[0]);
^
C:\scjd\starting\suncertify\db\FlyTableModel.java:31: [] can only be applied to arrays. It can't be applied to java.lang.String.
return values[row][col];
^
C:\scjd\starting\suncertify\db\FlyTableModel.java:39: Method getFieldInfo() not found in class suncertify.db.FlyTableModel.
return getFieldInfo()[col].getName();
^
6 errors
Process completed with exit code 1
Thanks in Advance Randy
also when I want to populate my JTable from the client is this appropiate::::::given my above class:::::
FlyTableModel model = new FlytableModel();
JTable table = new JTable(model);
 
Randy Valadez
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry about that.
 
Rudy Yeung
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Randy,
Your methods getFieldInfo() is belonging to data class. So your program should have something like fly.getFieldInfo(). Also, construction the data object requires catch the IOException, and this must have to be done within a method, not within the static initializer. If I were you, I would not have creatied the data object inside the table model class because the table model is supposed to handle the M part of the MVC i.e. the content of the GUI table components. This is just my thoughts.
Rudy
 
Randy Valadez
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Rudy I am going back to the sun site and reread the JTable Models..
Thanks Randy
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic