Avatar billede fredand Forsker
10. maj 2011 - 12:42 Der er 5 kommentarer og
1 løsning

How to align panel in NORTH_WEST

Hello!

I got a JPanel added into a JFrame.

The idea is that the JPanel should stay in NORTH_WEST when the JFrame is resiezed.

But when I resize, the JPanel floats to CENTER.
Please see image at:

http://dxc6nw.blu.livefilestore.com/y1pJng4kmUhoPMCjqEU8rSGfYUHs6gf_bHZbqCA1lWgETppDaSM8loRoJ83TYItaYpWL30G3pJBpwkUEZdp96X1t8WudULA3NoR/frame.png?psid=1

Best regards
Fredrik
Avatar billede Druesukker Nybegynder
10. maj 2011 - 23:31 #1
Hello Frederik

The easiest way to do this is to set the JFrame layout to setLayout( null ), and then in your JPanel constructor setBounds( 0, 0, 70, 30 ).

But then your JPanel will have a constant size and it is not a very dynamic way to do it.

Another way to do it is to use a GridBagLayout
http://download.oracle.com/javase/tutorial/uiswing/layout/gridbag.html

It is accually not easy to place objects dynamicialy inside JFrames.

Maybe a GridLayout is easiest to start out with. Later on you can try some of the more complex layouts. You can read about the GridLayout here:
http://download.oracle.com/javase/tutorial/uiswing/layout/grid.html
Avatar billede Druesukker Nybegynder
10. maj 2011 - 23:41 #2
If you want to use the GridBagLayout you need some objects for the grid to outweigth your JPanel with the Jlabel.

You can use empty JPanels for this purpose and then set weightx and weighty higher than the JPanel with the Jlabel.
Avatar billede arne_v Ekspert
11. maj 2011 - 03:00 #3
Look at how things should be placed and pick the appropriate layout.

Border/Grid/GridBag/Flow etc..

I doubt that null layout is a good choice.
Avatar billede fredand Forsker
12. maj 2011 - 08:34 #4
Hello guys!

Thank you both for the replies!

Please give svar so I can reward you!

I have to explain that I ran into this problem when I was using a JTabbedPane.

(Then my simple example was not a complete description of the problem.)

How ever I vent for the BorderLayout().

It doesnt look like you can do something like:

getTab(0).setLayout(new BorderLayout());
addTab("Titel", mYPanel, BorderLayout.NORTH);

I finally solved it like:

    private JPanel addPanel(JPanel lastChild, String title)
    {
        JPanel child = new JPanel();
        child.setLayout(new BorderLayout());
        child.add(lastChild, BorderLayout.NORTH);
       
        JPanel parent = new JPanel();
        parent.setLayout(new BorderLayout());
        parent.add(child, BorderLayout.WEST);
       
        TitledBorder titledBorder = BorderFactory.createTitledBorder(title);
        titledBorder.setTitleColor(Color.GRAY);
        titledBorder.setTitleJustification(TitledBorder.LEFT);
        parent.setBorder(titledBorder);
       
        return parent;
    }


and added the Tab like:

addTab(CONNECTION_SETTINGS, addPanel(settingsPanel, SettingsPanel.STEP_1_CONNECTION_SETTINGS) );

I do not know if my nestled panels is best practice but it seems to work.

Best regards
Fredrik
Avatar billede Druesukker Nybegynder
12. maj 2011 - 16:45 #5
When using JTabbedPane you should add a JPanel to manage the layout inside the tab.
eg.:

tabbedPane.addTab( "Title", null, theJPanel, "Tool Tip Text" );
The second parm is for an icon.

You can pick an appropriate layout for theJPanel, and you should add components to theJPanel...
Avatar billede arne_v Ekspert
13. maj 2011 - 03:17 #6
Just give the points to druesukker.

It is very common to have panels inside panels inside panels.
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester