views:

1698

answers:

3

Java Newbie here. I have a JFrame that I added to my netbeans project, and I've added the following method to it, which creates a JTable. Problem is, for some reason when I call this method, the JTable isn't displayed. Any suggestions?

public void showFromVectors(Vector colNames, Vector data) {     
    jt = new javax.swing.JTable(data, colNames);
    sp = new javax.swing.JScrollPane(jt);
    //NB: "this" refers to my class DBGridForm, which extends JFrame
    this.add(sp,java.awt.BorderLayout.CENTER);
    this.setSize(640,480);
}

The method is called in the following context:

DBGridForm gf = new DBGridForm(); //DBGridForm extends JFrame
DBReader.outMatchesTable(gf);
gf.setVisible(true);

... where DBReader.outMatchesTable() is defined as

static public void outMatchesTable(DBGridForm gf) {
    DBReader ddb = new DBReader();
    ddb.readMatchesTable(null);
    gf.showFromVectors(ddb.lastRsltColNames, ddb.lastRsltData);
}

My guess is I'm overlooking something, either about the swing classes I'm using, or about Java. Any ideas?

+1  A: 

"this" in your context is unclear. Is it inside an applet? a JFrame?

You may be having a layout issue, make sure you've called setLayout on your class with a new borderlayout.

In a swing application, you'd want to use getRootContentPane().add() instead of a raw add(), depending on the version.

Java tutorial on adding top-level content: http://java.sun.com/docs/books/tutorial/uiswing/components/toplevel.html

davenpcj
"this" refers to the DBGridForm, which extends JFrame. I thought that was obvious when I said that I (paraphrasing) "created a JFrame with netbeans, and added the following method to it". Looks like it wasnt as obvious as I had thought
Graza
OK, calling "this.setLayout(new java.awt.BorderLayout());" before adding the scroll pane did the trick.I think the default layout added by netbeans must be something different - this is really the first time I've used netbeans, all previous experience was doing things from scratch...
Graza
Default layout is a flowlayout, I think.
davenpcj
A: 

If you are not running on the event thread, it could be a problem--I've seen that cause stuff not to display.

If this code is called in response to an AWT event (mouse click, button press, ...) then that's not the problem, but if it's still the same thread that started your app, or this code is running off a timer, could very well be.

Bill K
You can use the java event thread to fire off things asynchronously. http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html
davenpcj
A: 

Bill - do you know what you are even talking about? Graza has the perfect solution.

Samurai