• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 882
  • Last Modified:

Correcting a Netbeans Table

Here, I used the GUI Builder in the NetBeans IDE.

In this code, I'm trying to load a table from a database to a table object displayed on the screen. Could you discuss how to do that?
ExpertExchange.zip
0
JCW2
Asked:
JCW2
  • 11
  • 8
  • 7
14 Solutions
 
for_yanCommented:

So what about this code?
I set up a project in Netbeans.

Does not execute for me - I guess I need some resources.
And anyway haow could I access database.

Do you have any specific questions?



0
 
JCW2Author Commented:
In the initComponents method, I tried to produce some code to get data from the sql table to the displayed table (relates to a nested for loop and a try block in this method). Do you have any ideas or suggestions with regards in how to do this?
0
 
JCW2Author Commented:
The code I'm referring to:
jTable1 = new javax.swing.JTable();
try
{
    ResultSetTableModel model = new ResultSetTableModel("jdbc:mysql://localhost:3306/university", "root", "194157", "select * from instructor");

    // link to database
    // tableModel = new ResultSetTableModel(url, username, password, defaultQuery);
    // I need some algorithm to insert into the table
    // new Object = tableModel

    // use algorithm to load values into table

    // i = width
    // j = length

    Object obj[][] = new Object[model.getColumnCount()][model.getRowCount()];

    for(int i = 0; i < obj.length; i++)
    {
        for(int j = 0; j < obj[0].length; j++)
        {
            // instantiate Object
            obj[i][j] = model.getValueAt(i, j);
        }
    }
jTable1.setModel(new javax.swing.table.DefaultTableModel(

    /*
    new Object [][] {
        {null, null, null, null},
        {null, null, null, null},
        {null, null, null, null},
        {null, null, null, null}
    }*/ obj ,
    new String [] {
        "Title 1", "Title 2", "Title 3", "Title 4"
    }    //         );

));
}
catch(Exception e)
{
    System.out.println("Error.");
}
jTable1.setName("jTable1"); // NOI18N



jScrollPane2.setViewportView(jTable1);

Open in new window

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!

 
for_yanCommented:
It is difficult to understand what you want - i would think about it in separate part -
you use database to retrieve data - store it uin ArrayList, Hashmap and then
use these collections to provide data which will be expposed through datamodel
mehods.

Say, you create vector of vectors - in fact vectir of rows of your table and then create datamodel instance using constructor
with  Vector of Vectors as parameter.

is your question abouut how to connect to database? this is one issue, o
or how to create data model?
Please, elaborate


 
0
 
for_yanCommented:
0
 
objectsCommented:
try this:


    ResultSetTableModel model = new ResultSetTableModel("jdbc:mysql://localhost:3306/university", "root", "194157", "select * from instructor");
    jTable1.setModel(model);
0
 
for_yanCommented:
0
 
JCW2Author Commented:
I'm trying to connect to the database.
0
 
objectsCommented:
> I'm trying to connect to the database.

the ResultSetTableModel handles that for you.
You should just need to use it as your tables model
0
 
JCW2Author Commented:
Could you tell me how that would work?
0
 
for_yanCommented:
Your ResultSetTableModel extends TableModel - it retrieves data form
database and then responds to all those methods which JTable would be using
to retrieve information necessary for population of JTable.

Then you really don't need to do anything if someone already wrote this class
which will provide all necessary information to JTable. Just setModel
as objects suggested and it should work.

0
 
objectsCommented:
> Could you tell me how that would work?


I posted how above


    ResultSetTableModel model = new ResultSetTableModel("jdbc:mysql://localhost:3306/university", "root", "194157", "select * from instructor");
    jTable1.setModel(model);
0
 
for_yanCommented:
Think of the TableModel or any class which extends TableModel as some entity which
JTable uses when it draws data in the cells. If you would start drawing table you would need
to know how many columns there would be, what ar the column names,
how many rows and what is the data in each of the rows.
All these questions absolutely match the methods of TableModel.
All these mthods are implemneted in this class which you have ResultySetTableModel - this
is equivlent to saying that the instance of this class is capable of answering
to all these questions. It is importnat to understand, that
JTable is not interested how the instance of TableModel will get these data.
Your instance which you have in these class is really very fancy - it draws the data difrectly
from database table. Now set this instance as model of your JTable and Jtable
will be able to populate ciolumns, ciolumn names and contents of all the cells.

Ask questions if somethindg is not clear about it.
0
 
JCW2Author Commented:
Do you mean write "jTable1.setModel(model);"? What do I do with this?
0
 
JCW2Author Commented:
The above comment was written before I discovered for_yan's most recent post.
0
 
objectsCommented:
> Do you mean write "jTable1.setModel(model);"? What do I do with this?

yes, replace the code you posted above with simply this:


jTable1 = new javax.swing.JTable();
ResultSetTableModel model = new ResultSetTableModel("jdbc:mysql://localhost:3306/university", "root", "194157", "select * from instructor");
jTable1.setName("jTable1"); // NOI18N
jScrollPane2.setViewportView(jTable1);

0
 
for_yanCommented:
In this way you create link between your JTable and the source of your data
represented by your instance of ResultSetTableModel

So in your code in DesktopApplication1View after
ResultSetTableModel model = new ResultSetTableModel("jdbc:mysql://localhost:3306/university", "root", "194157", "select * from instructor");

you say

jTable1.setModel(model);

0
 
JCW2Author Commented:
I've done that, with a "jTable1.setModel(model);" written (in the default spot). Some of the code doesn't move, in case you don't know that. When I run the code, I'm still getting an empty box with an error reading "WARNING: No saved state for javax.swing.JTable[jTable1,0,0,180x0,alignmentX=0.0,alignmentY=0.0,border=,flags=251658568,maximumSize=,minimumSize=,preferredSize=,autoCreateColumnsFromModel=true,autoResizeMode=AUTO_RESIZE_SUBSEQUENT_COLUMNS,cellSelectionEnabled=false,editingColumn=-1,editingRow=-1,gridColor=javax.swing.plaf.ColorUIResource[r=128,g=128,b=128],preferredViewportSize=java.awt.Dimension[width=450,height=400],rowHeight=16,rowMargin=1,rowSelectionAllowed=true,selectionBackground=javax.swing.plaf.ColorUIResource[r=10,g=36,b=106],selectionForeground=javax.swing.plaf.ColorUIResource[r=255,g=255,b=255],showHorizontalLines=true,showVerticalLines=true]
"
0
 
for_yanCommented:
So do you mean that some cells of the table are populated, but some do not contain expected data?
0
 
JCW2Author Commented:
No, some of the text can't move in Netbeans IDE in this situation. When I run the program, I happen to have a gray box for a table.
0
 
objectsCommented:
you shouldn't be changing things in your initComponents() method. your changes should be made in your constructor
0
 
JCW2Author Commented:
How do I change the table from there; is it about the same?
0
 
objectsCommented:
yes same as my first comment, just set the model after calling initComponents()

initComponents();
ResultSetTableModel model = new ResultSetTableModel("jdbc:mysql://localhost:3306/university", "root", "194157", "select * from instructor");
jTable1.setModel(model);
0
 
JCW2Author Commented:
I didn't intend to accept my own comment as a solution.
0
 
JCW2Author Commented:
Thank you for your help.
0
 
objectsCommented:
> I didn't intend to accept my own comment as a solution.

you're only meant to accept the comment that answered your q. not *all* comments :)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 11
  • 8
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now