Skip to main content

How to change list item BG color at runtime

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
1 reply [Last post]
abuhmead1987
Offline
Joined: 2010-04-21

Hello Guys,
I want to creat a list with four items as a question choices, when the user select an item the app checks if the selected item is correct or wrong and changes its BG color to green or red, and if it is correct a new question and choices will be displayed. and the list theme should reset to its default theme.
How could i do that?
Note: I created a new component extends TextArea and works as a button (ButtonArea), and i created container to hold the four ButtonArea and setted it to scrollabel Y, but changing these components bg color each time it mades the application to seem slow, Look at this timer task class which paints the component bg color 5 times as on off, likes who want to be millionaire game.

class ChangingBGTimerTask extends TimerTask
{

int selectedChoice,numOfBGPainted;
boolean isAnsweredWrong;
private boolean onOff=true;
private Image defaultBG,correctBG,wrongBG,focusBG;
public ChangingBGTimerTask(int selectedChoice,boolean isAnsweredWrong) {
this.selectedChoice=selectedChoice;
this.isAnsweredWrong=isAnsweredWrong;
try {
defaultBG=Image.createImage("/default_bg.png");
correctBG=Image.createImage("/correct_bg.png");
wrongBG=Image.createImage("/wrong_bg.png");
focusBG=Image.createImage("/focu_bg.png");
} catch (Exception e) {
}
}

public void run() {
repaint();
if (onOff) {
areaButtons[selectedChoice].getStyle().setBgImage(correctBG);
onOff = false;
} else {
areaButtons[selectedChoice].getStyle().setBgImage(focusBG);
onOff = true;
}

if (++numOfBGPainted == 5) {
areaButtons[selectedChoice].getSelectedStyle().setBgImage(focusBG);

// areaButtons[selectedChoice].setStyle(UIManager.getInstance().getComponentStyle("ChoiceButton"));
// areaButtons[selectedChoice].setFocus(true);

correctBGPaintingTimer.cancel();
// System.out.println(" isAnsweredWrong In TH"+isAnsweredWrong);
if (!isAnsweredWrong) {
diplayNewQ();
} else {

new MainMenuForm(mIDlet).show();
restVarables();
}
onOff=false;
selectedChoice=0;
isAnsweredWrong=false;
numOfBGPainted=0;
}
// Log.p("In TimerTask"+(log++)+"");
}
}

If I use correct approach, what is the cause of slowing my app?
Please Advice me.

Thanks a lot Advance!

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
vprise
Offline
Joined: 2003-11-07

Never use a timer with LWUIT, it uses its own thread and LWUIT only supports calls on the EDT.
If you need a timer just use:
[code]
parentForm.registerAnimated(new Animation() {
public boolean animate() {
// your code here
return false;
}
public void paint(Graphics g) {
// do nothing here
}
});