?
Solved

adding JPanel to another Panel

Posted on 2004-09-09
10
Medium Priority
?
750 Views
Last Modified: 2008-01-09
I have a function getTblPnl() which returns a JPanel. //Lets  call this panel p.
I have another JPanel called mainWin.

I already have the following code which will allow me to add onto mainWin. (below)
JPanel p = new JPanel(new FlowLayout(FlowLayout.LEFT));
p.add(mainWin);
add(p);
add(getTblPnl(), BorderLayout.SOUTH);

I would like to decrease the size of p which is in mainWin.  How can I do that?  Also, i need to keep the flowlayout south.

Furthermore, I already have set the size of p in getTblPnl() using the code below:
pT.setPreferredSize(new Dimension(pT.getPreferredSize().width, 435));
But if I decrease the size here, there is a big gap between the first panel and this panel in mainWin.  

How can I decrease the size to 285 w/o the big gap in the mainWin panel?
0
Comment
Question by:JK2429
  • 3
  • 3
  • 3
9 Comments
 
LVL 6

Expert Comment

by:expertmb
ID: 12017936
>>I would like to decrease the size of p which is in mainWin
from the code mainWin is added to p, from the above statement, p is added to mainWin
???
0
 
LVL 6

Expert Comment

by:expertmb
ID: 12017946
can you post the code.
0
 
LVL 4

Author Comment

by:JK2429
ID: 12018104
protected void mainWin()
    {
          setLayout(new BorderLayout());

          GridBagLayout lay= new GridBagLayout();
        GridBagConstraints cst = new GridBagConstraints();
        JPanel main = new JPanel(lay);
....//Here a lot of JPanels, Jcomboboxes, Jtextarea are added.

       JPanel p = new JPanel(new FlowLayout(FlowLayout.LEFT));
       p.add(mainWin);
       add(p);
       add(getTblPnl(), BorderLayout.SOUTH);
}

protected void getTblPnl()
{
  //Here a grid and mroe comboboxes are added.
  pT.add(tableX);
  pT.add(tableY);
  pT.setPreferredSize(new Dimension(pT.getPreferredSize().width, 435));  //edited for table size240
  return pT;
}
0
Independent Software Vendors: 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!

 
LVL 6

Expert Comment

by:expertmb
ID: 12024854
>>p.add(mainWin);
still dont know where this mainWin is coming from is it the main?
plz post the complete code.
0
 
LVL 4

Author Comment

by:JK2429
ID: 12026327
mainWin is the a JPanel.  It is the main JPanel where all others are being combined.
This is a massive application.  But I know that these two are the functions to be used in here.  I have attached the full function code in here.

    protected void mainWin()
    {
          setLayout(new BorderLayout());

          GridBagLayout lay = new GridBagLayout();
        GridBagConstraints cst = new GridBagConstraints();
        JPanel main = new JPanel(lay);

             LayoutUtility.addComponent(new JLabel(constF.getString("dt1"), JLabel.CENTER), main, lay, cst,
                                   0, 0, 1, 1, 0, 0, cst.NONE, cst.EAST, 5, 5, 5, 5);
        LayoutUtility.addComponent(getDt1(), main, lay, cst,
                                   1, 0, 1, 1, 0, 0, cst.NONE, cst.WEST, 5, 5, 5, 5);
        LayoutUtility.addComponent(new JLabel(constF.getString("type"), JLabel.RIGHT), main, lay, cst,
                                   4, 0, 1, 1, 0, 0, cst.NONE, cst.EAST, 5, 5, 5, 5);
        LayoutUtility.addComponent(getType(), main, lay, cst,
                                   5, 0, 1, 1, 0, 0, cst.NONE, cst.WEST, 5, 5, 5, 5);
            LayoutUtility.addComponent(new JLabel(constF.getString("type2"), JLabel.RIGHT), main, lay, cst,
                                   2, 1, 1, 1, 0, 0, cst.NONE, cst.EAST, 5, 5, 5, 5);
        LayoutUtility.addComponent(getType2(), main, lay, cst,
                                   3, 1, 1, 1, 0, 0, cst.NONE, cst.WEST, 5, 5, 5, 5);
        LayoutUtility.addComponent(new JLabel(constF.getString("lvl1"), JLabel.RIGHT), main, lay, cst,
                                   0, 3, 1, 1, 0, 0, cst.NONE, cst.EAST, 5, 5, 5, 5);
        LayoutUtility.addComponent(getLvl1(), main, lay, cst,
                                   1, 3, 1, 1, 0, 0, cst.NONE, cst.WEST, 5, 5, 5, 5);
            LayoutUtility.addComponent(new JLabel(constF.getString("lvl2"), JLabel.RIGHT), main, lay, cst,
                                                   4, 3, 1, 1, 0, 0, cst.NONE, cst.EAST, 5, 5, 5, 5);
            LayoutUtility.addComponent(getLvl2(), main, lay, cst,
                                                   5, 3, 1, 1, 0, 0, cst.NONE, cst.WEST, 5, 5, 5, 5);
          LayoutUtility.addComponent(new JLabel(constF.getString("dbX"), JLabel.RIGHT), main, lay, cst,
                                   0, 5, 1, 1, 0, 0, cst.NONE, cst.EAST, 5, 5, 5, 5);
        LayoutUtility.addComponent(getdbX(), main, lay, cst,
                                   1, 5, 5, 1, 0, 0, cst.BOTH, cst.WEST, 5, 5, 5, 5);
          LayoutUtility.addComponent(new JLabel(constF.getString("mcn"), JLabel.RIGHT), main, lay, cst,
                                   0, 6, 1, 1, 0, 0, cst.NONE, cst.EAST, 5, 5, 5, 5);
        LayoutUtility.addComponent(getMcn(), main, lay, cst,
                                   1, 6, 5, 1, 0, 0, cst.BOTH, cst.WEST, 5, 5, 5, 5);
          LayoutUtility.addComponent(new JLabel(constF.getString("ddX"), JLabel.RIGHT), main, lay, cst,
                                   0, 7, 1, 1, 0, 0, cst.NONE, cst.EAST, 5, 5, 5, 5);
        LayoutUtility.addComponent(getDdX(), main, lay, cst,
                                   1, 7, 5, 1, 0, 0, cst.BOTH, cst.WEST, 5, 5, 5, 5);
          LayoutUtility.addComponent(new JLabel(constF.getString("pvg"), JLabel.RIGHT), main, lay, cst,
                                   0, 8, 1, 1, 0, 0, cst.NONE, cst.EAST, 5, 5, 5, 5);
        LayoutUtility.addComponent(getPvg(), main, lay, cst,
                                   1, 8, 5, 1, 0, 0, cst.BOTH, cst.WEST, 5, 5, 5, 5);
          LayoutUtility.addComponent(new JLabel(constF.getString("m2l"), JLabel.RIGHT), main, lay, cst,
                                   0, 9, 1, 1, 0, 0, cst.NONE, cst.EAST, 5, 5, 5, 5);
        LayoutUtility.addComponent(getM2l() (), main, lay, cst,
                                   1, 9, 5, 1, 0, 0, cst.BOTH, cst.WEST, 5, 5, 5, 5);
            LayoutUtility.addComponent(new JLabel(constF.getString("show"), JLabel.RIGHT), main, lay, cst,
                                   2, 0, 1, 1, 0, 0, cst.EAST, cst.EAST, 5, 5, 5, 5);
        LayoutUtility.addComponent(getShow(), main, lay, cst,
                                   3, 0, 1, 1, 0, 0, cst.NONE, cst.WEST, 5, 5, 5, 5);
            LayoutUtility.addComponent(new JLabel(constF.getString("george"), JLabel.RIGHT), main, lay, cst,
                                   0, 1, 1, 1, 0, 0, cst.NONE, cst.EAST, 5, 5, 5, 5);
        LayoutUtility.addComponent(getGeorge(), main, lay, cst,
                                   1, 1, 1, 1, 0, 0, cst.NONE, cst.WEST, 5, 5, 5, 5);
            LayoutUtility.addComponent(new JLabel(constF.getString("pizlr"), JLabel.RIGHT), main, lay, cst,
                                   4, 1, 1, 1, 0, 0, cst.NONE, cst.EAST, 5, 5, 5, 5);
        LayoutUtility.addComponent(getPizlr(), main, lay, cst,
                                   5, 1, 1, 1, 0, 0, cst.NONE, cst.WEST, 5, 5, 5, 5);
        LayoutUtility.addComponent(new JLabel(constF.getString("wings"), JLabel.RIGHT), main, lay, cst,
                                   2, 3, 1, 1, 0, 0, cst.NONE, cst.EAST, 5, 5, 5, 5);
        LayoutUtility.addComponent(getWings(), main, lay, cst,
                                   3, 3, 1, 1, 0, 0, cst.NONE, cst.WEST, 5, 5, 5, 5);

            
JPanel p = new JPanel(new FlowLayout(FlowLayout.LEFT));
       p.add(mainWin);
       add(p);
       add(getTblPnl(), BorderLayout.SOUTH);
       
            getCheck();
            checkSys();
            AdminMents();
    }

protected JPanel getTblPnl()
    {
          JPanel pT = new JPanel(new BorderLayout(6,6));

          Gdhedo tableCXS = new  Gdhedo(buildCols(),0);

          tCsX     = new JPVTable(tableCXS);
          tCsXConfirm = new JPVTableVw (tCsX);

        tCsXConfirm.setColumnType(DTOr, JPVTable.DATE, new JVDate ());
        tCsXConfirm.setColumnType(DTLv,  JPVTable.DATE, new JVDate ());
        tCsXConfirm.setColumnType(TnoGARS,   JPVTable.TNO, new JVMTNo ("###-###-####"));

          tCsX.setListMode();

        if(Custom.isUserSelected())
        {
            tCsX.getTHead().setRAll(true);
            moveCols();
            tCsX.rtColOrr();
        }
        else
            setColWs();

        pT.add(tCsXConfirm);
        pT.setPreferredSize(new Dimension(pT.getPreferredSize().width, 300));  //edited for table size240

        return pT;
    }
0
 
LVL 3

Expert Comment

by:CI-Ia0s
ID: 12037032
You could use a BoxLayout/Box container (Box = a JPanel with a builtin BoxLayout)... With a box layout you can create defined margins via the use of "struts." With a BoxLayout, your program WILL obey your struts (whereas with preferredSize it usually has its own ideas).

Examples:

//First create the panel with BoxLayout, or just a Box container
JPanel boxPanel = new JPanel();
boxPanel.setLayout(new BoxLayout(boxPanel, BoxLayout.X_AXIS));
//You could do that or the following. It's pretty much the same thing... one or the other, though
Box boxPanel = new Box(BoxLayout.X_AXIS);

int WIDTH_OF_STRUT = 50; //in pixels...
//Create a strut
Component leftMargin = BoxLayout.createHorizontalStrut(WIDTH_OF_STRUT);

JPanel somePanel = new JPanel();

//Add the strut like a normal component and add any other stuff you want
boxPanel.add(leftMargin);
boxPanel.add(somePanel);


You can also make a vertical box within or containing the horizontal box so that you have control over all dimensions (obviously you'd use Y_AXIS and createVerticalStrut). Also, make sure you make your frame non-resizable (frameName.setResizable(false) ) or the user could just go screw up your perfect arrangements after so much hard work... :P

P.S. When using boxes/box layouts, don't use preferred size. This defeats the purpose of the box...
0
 
LVL 4

Author Comment

by:JK2429
ID: 12043125
Because this is already a mature application.  I do not have enough privledges to change the layout of the code.  I can either modify the existing layout or edit it.  
0
 
LVL 3

Accepted Solution

by:
CI-Ia0s earned 200 total points
ID: 12243636
To the author: You do not need to change the layout. You can simply add a Box or JPanel w/ BoxLayout to your existing layout and then add any subcomponents to that.

To Venabili: Sorry, but I never saw that last post! Can we keep it open a bit and see if the author is still around?
0
 
LVL 3

Expert Comment

by:CI-Ia0s
ID: 12255552
Thanks!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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…
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:
Suggested Courses
Course of the Month17 days, 6 hours left to enroll

864 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