Panel layout

Please advise how to create layout for jPanel like this: jPanel.tiff
this I create directly into jFrame and I can set exact position for each component manually in JDeveloper.
For jPanel I have headache 10 hours, I tried different layouts, but anyway I don't wished result.
public class DCC extends JFrame {
    private JMenuBar menuBar = new JMenuBar();
    private JMenu menuFile = new JMenu();
    private JMenuItem menuFileExit = new JMenuItem();
    private JPanel jPanel1 = new JPanel();
    private JLabel jLabel1 = new JLabel();
    private PaneLayout paneLayout1 = new PaneLayout();
    transient IGateway mFxcmGateway;
    private JToggleButton jToggleButton1 = new JToggleButton();

    private static DecisionCenter_EURUSD_S dc_EURUSD_S;
    private PaneLayout paneLayout2 = new PaneLayout();

    public DCC() {
        try {
            this.mFxcmGateway = DBFXConnect.getGateway();
        } catch (Exception e) {

    private void jbInit() throws Exception {
        this.setSize(new Dimension(1326, 596));
        this.setTitle("DCC 60");
        this.setLocation(500, 100);

        this.getContentPane().add(jPanel1, null);

        menuFileExit.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent ae) {


        jPanel1.setBounds(new Rectangle(15, 10, 1270, 30));
        jPanel1.setSize(new Dimension(1270, 30));
        jPanel1.setPreferredSize(new Dimension(1270, 30));
        jPanel1.setMinimumSize(new Dimension(1270, 30));


        jPanel1.setMaximumSize(new Dimension(1270, 30));
        jLabel1.setSize(new Dimension(80, 30));
        jLabel1.setFont(new Font("Lucida Grande", 1, 14));

        jToggleButton1.setBounds(new Rectangle(190, 40, 120, 40));
        jToggleButton1.setFont(new Font("Lucida Grande", 1, 18));
        jToggleButton1.addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {
                    try {
                    } catch (Exception f) {


    void fileExit_ActionPerformed(ActionEvent e) {

    private void jToggleButton1_mouseClicked(MouseEvent e) throws Exception {

        AbstractButton abstractButton = (AbstractButton)e.getSource();
        boolean flag0s = abstractButton.getModel().isSelected();
        dc_EURUSD_S = new DecisionCenter_EURUSD_S();

        System.out.println("Action - flag0s = " + flag0s + "\n");

        if (flag0s == true) {

        } else {

Open in new window

I want to create one jPanel template and after just multiply it with different values.
Please advice.
Who is Participating?
for_yanConnect With a Mentor Commented:
Judging by the picture, it looks like  you want all elements to be of more or less the same size.
Why would not you simply use GridLayout.

JPanel p = new JPanel();
p.setLayout(new GridLayout(1,9)); -

and then just consecutively add elements one after another

If you want them  to be of different size you may use GridbagLayout,
but this requires a little bit more planning, but if they
are really similar in size, then GridLayout is quite OK.
Do you need all these setBounds, etc.?

If you want then many panel like thsi you can add these panel to another
GridLayout stacking them vertically.

gbcbrAuthor Commented:
Thank you, I tried all other except GridLayout, but this works good for me.
Just please tell me, how I can make template from this panel, so I can make for each pair ready panel with all necessary buttons and fields and just add it to the frame one by one as soon I'll need them?

Well, frankly this sort of thing never worked very nice for me in this way.
You probably want to create JTable for that, which is a lot more pain.
How many panels (lines)  total do you think there would be?
Is it a limited number?

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

gbcbrAuthor Commented:
>>How many panels (lines)  total do you think there would be?
Is it a limited number?
at the moment 10 lines and 11-12 columns
for_yanConnect With a Mentor Commented:
Well, then you may try to create 10 empty JPanels, say make array of JPanels
JPanel [] pp;

pp = new JPanel[10];

 (I mean these long JPanel, which will eventually hold your stuff - each one 9 or like 11-12 pieces).
So you create them at the very beginning, like in constructor and you use  GridLayout(10,1) for the
main JFrame or JPanel that will hold all of them.
and then you create these empty stuff and add them
to your big JFrame.
for(int j=0; j<10; j++){
pp[j] = new JPanel();
pp[j].setLayout)new GridLayout(1,9);
} - somthing like that

And then when you need, you
to populate them one by one you add the
stuff to each of them.

This kind of theoretically should work, but in parctice
I don't thinkit worked well for me - it didn't show up when you just add the elemet
Perhaps after you add tyou need to say repaint()
ither to the whol frame or to each of these panels, but I'm not sure I figured out.

I kind of alwys try to have all elements laid out in the beginning
and then populatte them - maybe it is better to layout all of them
this way and then just setBackground, setLabel and probably setEnable or something for
buttons, then setText for textfields - so that they become filled.
If they are created upfront and you just change their properties
these things work they show up immediately, but creating new elements on the fly - I'm sure you can do it but I didn't have
experience - maybe I didn't know where to say this repaint and to which element to apply it
and was not insistent enough as always found the way
to create them all upfront.

That's why in JTable this will work - you already have JTable and then just populate it,
but that will also require some work with these renderers

I guess, you now understand my point - try first with these array of panels
maybe I did it in older Java - maybe now it would be easier to make it work

gbcbrAuthor Commented:
Thank you, most important for me now that I can adjust positions of elements.
I'll try also these to options.
Yes, let me know how it works.
gbcbrAuthor Commented:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.