Solved

adding JPanel to another Panel

Posted on 2004-09-09
10
725 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
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet doXXX methods 3 49
login jsp example 24 50
Custom Java Application connects to database: Which one of the 20? 4 28
Android development question 2 37
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

777 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