×

ComponentDemo

ComponentDemo demonstrates how to use LWUIT components to create buttons, checkboxes and radio buttons. It also shows how to apply colours to foreground and background of components.

Design

The MIDlet starts with a list of components to select — Button, Checkbox and Radio Button.

  • Button: This section shows how to apply different colours to foreground and background of components.

  • Checkbox: This section shows the different variants of CheckBox.

  • Radio Button: This section shows the different variants of RadioButton.

The MIDlet can be used in both portrait and landscape modes, and is tested on Nokia Asha SDK 1.0.

Implementation

Initialising LWUIT Display:

Display.init(this); // initialise LWUIT Display

Initialising different variants of CheckBox:

checkboxGroup = new ComponentGroup();

CheckBox firstCB = new CheckBox("First CheckBox");
checkboxGroup.addComponent(firstCB);

CheckBox secondCB = new CheckBox("Second CheckBox");
checkboxGroup.addComponent(secondCB);

// Creating Disabled CheckBox
CheckBox disabledCB = new CheckBox("Disabled CheckBox");
disabledCB.setEnabled(false);
checkboxGroup.addComponent(disabledCB);

// Creating checked-disabled CheckBox
CheckBox disabledCB1 = new CheckBox("Checked Disabled CheckBox");
disabledCB1.setSelected(true);
disabledCB1.setEnabled(false);
checkboxGroup.addComponent(disabledCB1);

Initialising different variants of RadioButton:

radioGroup = new ComponentGroup();

firstRadio = new RadioButton("First RadioButton");
radioGroup.addComponent(firstRadio);

secondRadio = new RadioButton("Second RadioButton");
radioGroup.addComponent(secondRadio);

// Creating disabled RadioButton
disabledRadioButton = new RadioButton("Disabled RadioButton");
disabledRadioButton.setEnabled(false);
radioGroup.addComponent(disabledRadioButton);

// Creating checked-disabled RadioButton
chkdisabledRadioButton = new RadioButton("Checked Disabled RadioButton");
chkdisabledRadioButton.setSelected(true);
chkdisabledRadioButton.setEnabled(false);
radioGroup.addComponent(chkdisabledRadioButton);

Initialising ColorPalette:

for (int i = 0; i < buttons.length; i++) {
    if (buttons[i] == null) {
        buttons[i] = new Button(" ");               
    }
    // Create style to button
    Style style = new Style();
    style.setBgTransparency(255);
    style.setBgColor(colors[i]);
    style.setMargin(1, 1, 1, 1);
    style.setPadding(0, 0, 0, 0);
    //Add border to button
    style.setBorder(Border.createBevelRaised(0xdddddd, 0xAAAAAA, 0x111111,0x020202));
    // Set styles to button
    Style pressedStyle = new Style();
    pressedStyle.setBgTransparency(255);
    pressedStyle.setBgColor(colors[i]);
    pressedStyle.setMargin(1, 1, 1, 1);
    pressedStyle.setPadding(0, 0, 0, 0);
    pressedStyle.setBorder(Border.createBevelRaised(0x020202, 0x111111, 0xAAAAAA, 0xdddddd));
    // add actionlistener for button
    buttons[i].addActionListener(this);
    buttons[i].setUnselectedStyle(style);
    buttons[i].setSelectedStyle(style);
    buttons[i].setPressedStyle(pressedStyle);
    buttons[i].setDisabledStyle(style);
    paletterHolder.addComponent(buttons[i]);
}

Applying foreground and background colours to components:

if (source instanceof Button) {
    if (!source.equals(setBgColor) && !source.equals(setFgColor)) {
        Button button = (Button) evt.getSource();

        selectedbgColor = button.getUnselectedStyle().getBgColor();
        if (setBgColor.isSelected()) {
            // Setting background color of form title
            label = form.getTitleComponent();
            Style style = label.getUnselectedStyle();
            style.setBgTransparency(255);
            style.setBgColor(selectedbgColor, true);
            label.setUnselectedStyle(style);
            label.setSelectedStyle(style);
            label.setDisabledStyle(style);
            label.repaint();
        }

        if (setFgColor.isSelected()) {
            // Setting foreground color of form title
            label = form.getTitleComponent();
            Style style = label.getStyle();
            style.setFgColor(selectedbgColor);
            label.setUnselectedStyle(style);
            label.setSelectedStyle(style);
            label.setDisabledStyle(style);
            label.repaint();
        }
    }
}

Last updated 24 June 2013

Back to top

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×