Solved

adding JPanel to another Panel

Posted on 2004-09-09
10
736 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
[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
  • 3
  • 3
  • 3
10 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
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!

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

739 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