Solved

Panel layout

Posted on 2011-02-26
8
358 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
  • 4
  • 4
8 Comments
 
LVL 47

Accepted Solution

by:
for_yan earned 500 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
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…
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 about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

919 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

18 Experts available now in Live!

Get 1:1 Help Now