Skip to main content

Please help me

7 replies [Last post]
darolla
Offline
Joined: 2008-07-18

Hello,

I am trying to put some buttons one below the other.

f.setLayout(new BoxLayout(BoxLayout.Y_AXIS));

the problem is that all buttons are as wide as the screen, and they are starting from the top.

what I need is:
- small buttons
- all one below the other
- all buttons centered x and y

I tried to put the buttons into a container (boxlayout), and the container into form (borderlayout).

but now the buttons are as wide as the screen again.

I tried a gridlayout but I didnt find a way to fill cells with empty components nor putting my buttons into specific cells.

please help me.

greetings frmo germany
marco schmitz

ps: I'd like to have something like this:

--------------------------------------------------
| |
| |
| button1 |
| button2 |
| button3 |
| |
| |
--------------------------------------------------

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
asterfr
Offline
Joined: 2008-07-17

Does an other way to do it easily ?

darolla
Offline
Joined: 2008-07-18

to be honest, I dont understand why its trying to scroll? there are only 3 buttons... and without the class Spring there arent any scrollbars...

thanks :)
marco

darolla
Offline
Joined: 2008-07-18

sorry, my text painting didnt work.

lets try again

----------------------------------------------------
|...................................................|
|...................................................|
|...................button1.....................|
|...................button2.....................|
|...................button3.....................|
|...................................................|
|...................................................|
----------------------------------------------------

kawaiimomo
Offline
Joined: 2008-07-10

Hi there,
to align the Buttons:

Button b = new Button();
b.setAlignment(Component.CENTER);

to get the desired size, you can hardcode it by overriding calcPreferredSize(). Yo can also adjust margin and padding combined with every button dimensions.

b.getStyle.setMargin()
b.getStyle.setPadding()

Hope that helps.

Shai Almog

Thanks kawaiimomo,
this is indeed the better/simpler approach to solving this issue.
There is also a more elaborate approach (I tend to make things
difficult...):
Form frm = new Form("Buttons");
Container c = new Container(new BoxLayout
(BoxLayout.Y_AXIS));
frm.setLayout(new BorderLayout());
c.addComponent(new Button("Button A"));
c.addComponent(new Button("Button B"));
c.addComponent(new Button("Button C"));
frm.addComponent(BorderLayout.CENTER, c);

class Spring extends Label {
public Spring() {
getStyle().setBgTransparency(0);
}

protected Dimension calcPreferredSize() {
return new Dimension(Display.getInstance
().getDisplayWidth() / 3, 10);
}
}

frm.addComponent(BorderLayout.EAST, new Spring());
frm.addComponent(BorderLayout.WEST, new Spring());
frm.show();



> Hi there,
> to align the Buttons:
>
> Button b = new Button();
> b.setAlignment(Component.CENTER);
>
> to get the desired size, you can hardcode it by overriding
> calcPreferredSize(). Yo can also adjust margin and padding combined
> with every button dimensions.
>
> b.getStyle.setMargin()
> b.getStyle.setPadding()
>
> Hope that helps.
> [Message sent by forum member 'kawaiimomo' (kawaiimomo)]
>
> http://forums.java.net/jive/thread.jspa?messageID=287540
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@lwuit.dev.java.net
> For additional commands, e-mail: users-help@lwuit.dev.java.net
>

Shai Almog
http://lwuit.blogspot.com/

[att1.html]
[Picture 78.png]

darolla
Offline
Joined: 2008-07-18

Hello shai and kawaiimomo,

thanks a lot for the advice.

I played around with it a bit. After a little correction I've got only 1 problem left: the scrollbar. how can I get rid of it? :)

greetings,
marco

SCREENSHOT:
http://home.versanet.de/~marco204/test2.png

SOURCE:
public class Test2 extends MIDlet implements ActionListener {

private static final String RES = "/businessTheme.res";
private static final String THEME = "businessTheme";

private Resources r;

public void startApp() {
try {
Display.init( this );

r = Resources.open( RES );
UIManager.getInstance().setThemeProps( r.getTheme( THEME ) );

Form frm = new Form( "Test2" );
Container c = new Container( new BoxLayout( BoxLayout.Y_AXIS ) );

frm.setLayout( new BorderLayout() );
c.addComponent( new Button( "Button A" ) );
c.addComponent( new Button( "Button B" ) );
c.addComponent( new Button( "Button C" ) );

frm.addComponent( BorderLayout.CENTER, c );

class Spring extends Label {

public Spring() {
getStyle().setBgTransparency( 0 );
}

protected Dimension calcPreferredSize() {
return new Dimension( Display.getInstance().getDisplayWidth() / 3, Display.getInstance().getDisplayHeight() / 3 );
}
}

frm.addComponent( BorderLayout.EAST, new Spring() );
frm.addComponent( BorderLayout.WEST, new Spring() );

frm.addComponent( BorderLayout.NORTH, new Spring() );
frm.addComponent( BorderLayout.SOUTH, new Spring() );

frm.show();
}
catch( IOException e ) {
}
}

asterfr
Offline
Joined: 2008-07-17

Well... have you tried the setScrollable method ? It may help you I think ;)

Have a nice day.