Solved

TableLayout (info.clearthought.layout.TableLayout) problem

Posted on 2003-12-09
15
668 Views
Last Modified: 2010-05-18
I have not used this TableLayout before, but it doesn't seem too difficult.  The problem is that I cannot center the table or attach a scroll pane to it.  If anyone has had any experience/luck with TableLayout, please let me know how to center the table on a JPanel and how to get a JScrollPane to appear if any cells go out of sight.  To see any code related to the TableLayout, it can be found at http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20812621.html

Thanks,
Chris
0
Comment
Question by:ctjoumas
15 Comments
 
LVL 3

Expert Comment

by:RuadRauFlessa
ID: 9904166
All you have to do is to create your table, create a scroll pane and pass the table object to it, then add that to the center on a pane with a  border layout manager. You won't need much more to get the whole thing going but I will take a closer look for you.
0
 

Author Comment

by:ctjoumas
ID: 9904306
I have been trying different combinations of things...but here is the latest attempt to mimic what you described:

------------------------
        // setup table panel where the table of ImageContainers will reside
        JPanel tablePanel = new JPanel();
        tablePanel.setPreferredSize( new Dimension( 500, 500 ) );
        tablePanel.setBorder( BorderFactory.createEtchedBorder() );
        tablePanel.setBackground( Color.GRAY );
        tablePanel.setLayout( layout );

        // create the scrollpane
        JScrollPane scrollPane = new JScrollPane( tablePanel );

        // create the content pane to add the scrollpane to - centered
        JPanel contentPane = new JPanel( new BorderLayout() );
        contentPane.setBorder( BorderFactory.createEmptyBorder() );
        contentPane.add( scrollPane, BorderLayout.CENTER );

        // add the contentPane to the main JFrame
        getContentPane().add( contentPane );
------------------------

Still getting hte same result.

Thanks,
Chris
0
 
LVL 92

Expert Comment

by:objects
ID: 9906736
what exactly do you mean by 'center the table on a JPanel'?
0
 

Author Comment

by:ctjoumas
ID: 9906900
I want whatever is going to be layed out via the TableLayout to appear centered in my program.  Right now, when the program is loaded and my images are loaded (for example in a 5x5 table), the entire table of images is showing up at the bottom right - some weird place..just not centered.  Additionally, if I maximize the frame, a small portion of the table of images is still offscreen and there is no scroll pane to scroll so that the rest of the images can be seen.  (I'd really rather prefer not to have the scroll pane and just let the tablelayout resize the table of images...but i'll settle for just getting it centered for now)

an example...with X's representing my images:

-----------------------------
|                                  |
|                                  |
|                       X X X X| X X
|                       X X X X| X X
|                       X X X X| X X
|                       X X X X| X X
|                       X X X   |
|                                  |
-----------------------------

Chris
0
 
LVL 92

Expert Comment

by:objects
ID: 9907013
why exactly are you using this TableLayout class which appears to be the cause of your pain?
0
 

Author Comment

by:ctjoumas
ID: 9909195
Well, I searched for something to get done what I wanted (dynamic rows/columns) and it came up :)  Apparently it does the same as GridBagLayout, but easier.  It is actually setup pretty nicely, except for the positioning!  I have not used GridBagLayout either, but I would assume that it is more complicated since TableLayout seems to encapsulate/facilitate it - but I guess it all depends on how famaliar one is with it.
0
 
LVL 92

Expert Comment

by:objects
ID: 9909225
Personally never use GBL, too cryptic for my liking.
What exactly is the layout you are trying to achieve?
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:ctjoumas
ID: 9909270
I am getting a number of images loaded from a custom file from an MRI machine.  The number of images is different with each file...so, one could have 3 and one could have 18.  The outcome is to be a table of images.  For now, I am making the the number of columns be 5.  So, for three images the display should be

X: denotes an image
-: denotes an empty cell

---------------
|                |
|                |
|   XXX--     |
|                |
|                |
---------------

18 images should be:

---------------
|                |
|   XXXXX   |
|   XXXXX   |
|   XXXXX   |
|   XXX--     |
|                |
---------------

Right now I am able to dynamically create the correct amount of cells - so my table looks fine...it is just horrible out of alignment.
0
 
LVL 92

Accepted Solution

by:
objects earned 35 total points
ID: 9909295
Are the images the same size?
Have you considered using GridLayout if all cells in table are same size.
0
 

Author Comment

by:ctjoumas
ID: 9911690
As far as I know, the images are the same size.  I have not yet looked into GridLayout though.  I guess GL can create new rows and cells on the fly?  I will look into that today then.

Chris
0
 
LVL 2

Expert Comment

by:prcsn1
ID: 9918517
TableLayout is a fairly nice gadget for doing pretty much what you want.

It does take a little work to make it behave, though. Oddly though, I'm doing what you want - ie dynamically building a layout. I do manage to get a scroll bar to appear. You do have to define the row sizes though. My code accepts a number of fields with spaces between them. The panel is put inside a scrollPane using something like
   jScrollPaneInput.getViewport().add(jPanelDataEntry, null);

I DO use gridbag (but that because I use JBuilder, which can manage it for me - mostly) and the scroll pane is in a tabbed pane wusing a GridBag.

and here's some code

    double border = 5;
    double gap = 5;
    double[] colSizes = {
        border, TableLayout.PREFERRED, gap, TableLayout.PREFERRED, gap, TableLayout.PREFERRED, gap,
        TableLayout.FILL, border};
    double[] rowSizes = new double[fieldList.size() * 2 + 1 + 2 + 2]; //allow borders, heading, one gap per row, and space for the OK button
    for (int i = 1; i < rowSizes.length - 1; i = i + 2) {
      rowSizes[i] = TableLayout.PREFERRED;
      rowSizes[i + 1] = gap; //yes, I DO know the last one will be overwritten by border .. that's what we want, isn't it?
    }
    rowSizes[0] = border;
    rowSizes[rowSizes.length - 1] = border;

    double[][] sizes = {
        colSizes, rowSizes};
    jPanel.setLayout(new TableLayout(sizes));


I'm not sure this is really goung to help. But it all works very nicely. Took me a while to knock it into shape but works very well.

Simon (in Sydney, where it's supposed to be Summer, but where's the damned sun?)
0
 

Author Comment

by:ctjoumas
ID: 9951500
I'm still trying the table layout...and I'm getting more and more confused :)

I am trying to do this for my rows and columns:

        for ( int i=0; i<cols.length; i++ ) {
            cols[ i ] = TableLayout.FILL;
        }

        for ( int i=0; i<rows.length; i++ ) {
            rows[ i ] = TableLayout.FILL;
        }

Then add the component into the correct row/column :

tablePanel.add( container, String.valueOf( currCol ) + ", " + String.valueOf( currRow ) + ", c, c" );

Adding the constraint of c,c doesn't seem to have any effect....

but, this gets CLOSER to what I want.  The components are all staying on screen and adjusting when I want them to.  But the alignment appears to be in the bottom right of the screen!!

Any ideas why?  I keep tweaking how the rows/columns are constructed and get different results (each just a surprise to me...I have no idea what is going on with it), but nothing gives me my desired result!  There was even one that was very close...but it left out the last row - it all stayed centered though.  Anyway...hopefully you can shed some more light on this!

Chris
0
 

Author Comment

by:ctjoumas
ID: 9951663
Quick question...  what specifies the cell sizes?  Is it when the cols/rows are created?  So, if I do cols[ 0 ] = 100, what is that saying?  that column one will have a width of 100 pixels?  a height of 100 pixels?  a dimension of 100 pixels?  That is kinda hanging me up...

Also...a comment on your code.  Do you need to use the gap stuff?  After you create your table, can't you just use layout.setVGap/layout.setHGap?

Chris
0
 

Author Comment

by:ctjoumas
ID: 9951811
Sorry objects...I forgot to check on GridLayout.  I have used it before, but forgot all about it - and forgot to test it out upon your suggestion :)  The logic in adding to the correct row/column was already in there so all that I had to do was just create the GridLayout and plug it in...

prcsn1 - I appreciate your help...I am still interested in learning that TableLayout, so I might look back into it - seems useful if you know what you are doing (which I obviously do not haha).

Chris
0
 
LVL 92

Expert Comment

by:objects
ID: 9951858
Sorry I couldnt help more with TableLayout usage.

http://www.objects.com.au
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now