TableLayout (info.clearthought.layout.TableLayout) problem

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
ctjoumasAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RuadRauFlessaCommented:
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
ctjoumasAuthor Commented:
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
objectsCommented:
what exactly do you mean by 'center the table on a JPanel'?
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

ctjoumasAuthor Commented:
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
objectsCommented:
why exactly are you using this TableLayout class which appears to be the cause of your pain?
0
ctjoumasAuthor Commented:
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
objectsCommented:
Personally never use GBL, too cryptic for my liking.
What exactly is the layout you are trying to achieve?
0
ctjoumasAuthor Commented:
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
objectsCommented:
Are the images the same size?
Have you considered using GridLayout if all cells in table are same size.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ctjoumasAuthor Commented:
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
prcsn1Commented:
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
ctjoumasAuthor Commented:
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
ctjoumasAuthor Commented:
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
ctjoumasAuthor Commented:
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
objectsCommented:
Sorry I couldnt help more with TableLayout usage.

http://www.objects.com.au
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.