We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Correcting a Netbeans Table

JCW2
JCW2 asked
on
Medium Priority
906 Views
Last Modified: 2012-05-11
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
Comment
Watch Question

Awarded 2011
Awarded 2011
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

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?

Author

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

Awarded 2011
Awarded 2011
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Awarded 2011
Awarded 2011
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Awarded 2011
Awarded 2011
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
I'm trying to connect to the database.
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Could you tell me how that would work?
Awarded 2011
Awarded 2011
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Awarded 2011
Awarded 2011
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Do you mean write "jTable1.setModel(model);"? What do I do with this?

Author

Commented:
The above comment was written before I discovered for_yan's most recent post.
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Awarded 2011
Awarded 2011
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

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]
"
Awarded 2011
Awarded 2011
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

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.
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
How do I change the table from there; is it about the same?
Java Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
I didn't intend to accept my own comment as a solution.

Author

Commented:
Thank you for your help.
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> 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 :)
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.