?
Solved

Panel layout

Posted on 2011-02-26
8
Medium Priority
?
362 Views
Last Modified: 2012-05-11
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 {
            jbInit();
            this.mFxcmGateway = DBFXConnect.getGateway();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void jbInit() throws Exception {
        
        this.setJMenuBar(menuBar);
        this.getContentPane().setLayout(null);
        this.setSize(new Dimension(1326, 596));
        this.setTitle("DCC 60");
        this.setAlwaysOnTop(true);
        this.setLocation(500, 100);
        this.setVisible(true);

        this.getContentPane().add(jPanel1, null);
        
        menuFile.setText("File");
        menuFile.add(menuFileExit);

        menuFileExit.setText("Exit");
        menuFileExit.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    fileExit_ActionPerformed(ae);
                }
            });

        menuBar.add(menuFile);

        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));
        jPanel1.setLayout(paneLayout1);
        jLabel1.setText("EUR/USD");
        jLabel1.setSize(new Dimension(80, 30));
        jLabel1.setFont(new Font("Lucida Grande", 1, 14));
        jLabel1.setForeground(Color.blue);
        jLabel1.setLayout(null);

        jToggleButton1.setText("SELL");
        jToggleButton1.setBounds(new Rectangle(190, 40, 120, 40));
        jToggleButton1.setBackground(Color.red);
        jToggleButton1.setOpaque(true);
        jToggleButton1.setFont(new Font("Lucida Grande", 1, 18));
        jToggleButton1.setBorderPainted(false);
        jToggleButton1.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
        jToggleButton1.setLayout(paneLayout2);
        jToggleButton1.addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {
                    try {
                        jToggleButton1_mouseClicked(e);
                    } catch (Exception f) {
                        f.printStackTrace();
                    }
                }
            });


    }

    void fileExit_ActionPerformed(ActionEvent e) {
        mFxcmGateway.logout();
        System.exit(0);
    }

    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();
        dc_EURUSD_S.aflags(flag0s);

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

        if (flag0s == true) {
            jToggleButton1.setBackground(Color.green);

        } else {
            jToggleButton1.setBackground(Color.red);
        }
    }
}

Open in new window

I want to create one jPanel template and after just multiply it with different values.
Please advice.
0
Comment
Question by:gbcbr
[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
  • 4
  • 4
8 Comments
 
LVL 47

Accepted Solution

by:
for_yan earned 2000 total points
ID: 34989293
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.

Just
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.


0
 

Author Comment

by:gbcbr
ID: 34990578
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?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34990608

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?

0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

Author Comment

by:gbcbr
ID: 34990613
>>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
0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 2000 total points
ID: 34990652
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);
mainPanel.add(pp[j]);
} - 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




0
 

Author Closing Comment

by:gbcbr
ID: 34990667
Thank you, most important for me now that I can adjust positions of elements.
I'll try also these to options.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34990673
Yes, let me know how it works.
0
 

Author Comment

by:gbcbr
ID: 34990674
OK
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

762 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