?
Solved

TableLayout (info.clearthought.layout.TableLayout) problem

Posted on 2003-12-09
15
Medium Priority
?
772 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 

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 105 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses
Course of the Month11 days, 14 hours left to enroll

752 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