Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JPanels in JPanels. No more than one JPanels as lines shown?  RSS feed

 
Daan Heuvelbeuk
Ranch Hand
Posts: 85
MySQL Database Netscape Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my application I use a JFrame to display multiple JPanel's inside JPanel's in.

One of the JPanel's, called MyRecordPanel has zero or more JPanel's called MyRecord. Or in plain English, I have a JPanel in which I want to display records from a database. To control the placement of the different fields I use NO LayoutManager, but use setBounds (x, y, w, h).

What I do not understand is that I only get one record
(And less important, I do not understand why the width of MyRecord is zero in the MyRecordsPanel (see logging))

MyRecordsPanel:

Then MyRecord:

And some logging:

FWIW, I'm trying not to use magic numbers. So:
HEIGHT = 30,
FONTHEIGHT = 14
DAYX = 10
DAYW = 20 (further widths omitted)
DATEX = 35
PROJECTX = 110
ACTIVITYX = 380
TIMEX = 650
CHECKBOXX = 720
And the width of the JPanel is 750
 
Rob Camick
Ranch Hand
Posts: 2787
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I do not understand is that I only get one record


Your "recordsPanel" is using a FlowLayout which calculates the preferred size assuming all components are displayed on a single line. So I would guess when you add the "recordsPanel" to the frame only a single line is visible. Try using a "GridLayout".

I do not understand why the width of MyRecord is zero in the MyRecordsPanel


The layout manager is responsible for determining the size and location of the component. Since you are using a null layout the size is (0, 0).

Don't use a null layout. Swing was designed to be using with layout managers.

FWIW, I'm trying not to use magic numbers


But you are using magic numbers. Just because you assigned them to a variable does not make then less magical. You just randomly picked a width. You don't know if the width is appropriate or not.

Every component is responsible for determining its own preferred size. Again use layout managers properly and you won't need to use magic numbers.

I want to display records from a database.


Then you should be using a JTable. This is a Swing component perfectly designed for this requirement. Check out the section from the Swing tutorial on How to Use Tables for more information.

Then you should also check out the Table From Database Example code found in Table From Database for a working example (well, you will need to change the SQL and database connection code) that demonstrates how this might be done.
 
Daan Heuvelbeuk
Ranch Hand
Posts: 85
MySQL Database Netscape Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:
I want to display records from a database.


Then you should be using a JTable. This is a Swing component perfectly designed for this requirement. Check out the section from the Swing tutorial on How to Use Tables for more information.

Then you should also check out the Table From Database Example code found in Table From Database for a working example (well, you will need to change the SQL and database connection code) that demonstrates how this might be done.


I saw that Swing tutorial. Is it safe to use the TableModel? I was afraid a rogue user could use it to execute a SQL statement using a SQL Injection.

Thanks for the Table From Database page.
 
Rob Camick
Ranch Hand
Posts: 2787
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A TableModel holds data. It has nothing to do with SQL.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!