Solved

adding JPanel to another Panel

Posted on 2004-09-09
10
703 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
Comment Utility
>>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
Comment Utility
can you post the code.
0
 
LVL 4

Author Comment

by:JK2429
Comment Utility
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
 
LVL 6

Expert Comment

by:expertmb
Comment Utility
>>p.add(mainWin);
still dont know where this mainWin is coming from is it the main?
plz post the complete code.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 4

Author Comment

by:JK2429
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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 how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now