Codenameone - list component run slowly on real-phone - codenameone

Im a newbie codenameone.
I have a "Customer List - FORM". It has a List(com.​codename1.​ui.​List) with setRenderer my model(Customer):
public class CustRow extends Container implements ListCellRenderer {
private Label lblfocus = new Label("");
private Label lblname = new Label("");
private Label lblphone = new Label("");
private GuLabel lblnotes = new GuLabel("");
public CustRow() {
this.setUIID("LISTITEM");
this.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
Container mcselect = new Container(new FlowLayout());
CheckBox chkselect = new CheckBox();
chkselect.setUIID("LABEL");
mcselect.addComponent(chkselect);
this.addComponent(mcselect);
Container leftcol = new Container(new BorderLayout());
leftcol.addComponent(BorderLayout.WEST, lblname);
leftcol.addComponent(BorderLayout.EAST, lblphone);
this.addComponent(leftcol);
lblname.setUIID("LABEL");
lblphone.setUIID("LABEL");
lblnotes.setUIID("LBLNOTES");
this.addComponent(lblnotes);
}
public Component getListCellRendererComponent(List list, Customer value, int index, boolean isSelected) {
lblname.setText(value.getName());
lblphone.setText(value.getPhone());
lblnotes.setText(value.getNotes());
return this;
}
public Component getListFocusComponent(List list) {
return lblfocus;
}
}
I have 2 problem following:
http://i.stack.imgur.com/kuOhD.jpg -
http://i.stack.imgur.com/e8GpP.jpg
1. When I ran app on myPhone (android 4.2), next to scroll to choose item, it Slowly.
2. When I make one checkbox on ListItem checked, All checkbox is checked.
Help me! Please.

List is difficult I suggest avoiding the renderer paradigm if you are a beginner to Codename One and just using the GenericListCellRenderer instead or better yet just using MultiList.
Both effectively solve the issue of working with checkboxes & perform well.
I would guess your model is causing some of the performance issues you are seeing.

Related

Unwanted swipe left on swipeable container when swiping another

I have a number of swipeable containers added to a page. The swipeable containers have buttons left and right and the top component is a multibutton that takes the user to another page. When the user goes back to the page with the swipeable containers and swipes left (only left) on a different swipeable container then the first container also opens to the left. Images and sample code below:-
//Swipeable containers bugs
public void SwipeableContainerPage(Form mainForm){
Form formSC = new Form("Swipeable Container", new BorderLayout());
formSC.getToolbar().setBackCommand("", e -> mainForm.showBack());
Container northcnt = new Container(new BoxLayout(BoxLayout.Y_AXIS));
Container centercnt = new Container(new BoxLayout(BoxLayout.Y_AXIS));
Container southcnt = new Container(new BoxLayout(BoxLayout.Y_AXIS));
MultiButton mb_1 = new MultiButton("MB1");
Button b_l1 = new Button("LB1");
Button b_r1 = new Button("RB1");
MultiButton mb_2 = new MultiButton("MB2");
Button b_l2 = new Button("LB2");
Button b_r2 = new Button("RB2");
MultiButton mb_3 = new MultiButton("MB3");
Button b_l3 = new Button("LB3");
Button b_r3 = new Button("RB3");
SwipeableContainer sc_1 = new SwipeableContainer(b_l1, b_r1, mb_1);
SwipeableContainer sc_2 = new SwipeableContainer(b_l2, b_r2, mb_2);
SwipeableContainer sc_3 = new SwipeableContainer(b_l3, b_r3, mb_3);
mb_1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
GoToAndBackPage(formSC);
}
});
mb_2.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
GoToAndBackPage(formSC);
}
});
mb_3.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
GoToAndBackPage(formSC);
}
});
centercnt.add(sc_1);
centercnt.add(sc_2);
centercnt.add(sc_3);
formSC.add(BorderLayout.NORTH, northcnt).add(BorderLayout.CENTER, centercnt).add(BorderLayout.SOUTH, southcnt);
formSC.show();
}
//page for SC to go to
public void GoToAndBackPage(Form mainForm){
Form formGoTo = new Form("Go To Page", new BorderLayout());
formGoTo.getToolbar().setBackCommand("", e -> mainForm.showBack());
Container northcnt = new Container(new BoxLayout(BoxLayout.Y_AXIS));
Container centercnt = new Container(new BoxLayout(BoxLayout.Y_AXIS));
Container southcnt = new Container(new BoxLayout(BoxLayout.Y_AXIS));
Label l_welcome = new Label("Welcome to the Go To Page");
centercnt.add(l_welcome);
formGoTo.add(BorderLayout.NORTH, northcnt).add(BorderLayout.CENTER, centercnt).add(BorderLayout.SOUTH, southcnt);
formGoTo.show();
}
This sounds like a bug and ideally you should file an issue with a test case. But try this for a workaround.
Wrap the code that handles the navigation to a different form in a call serially. E.g. if the code looks like:
myMultiButton.addActionListener(e -> showOtherForm(...));
Then do this:
myMultiButton.addActionListener(e -> callSerially(showOtherForm(...)));
My theory is that the press brings the swipable component to a state that "could" be dragging. But because the navigation event happens to soon the release event isn't received by the button (we're on a different form now) and everything is lost.
So when you get back to the form you now have two "charged" buttons. By using a callSerially you effectively flush the event queue and make sure the release event is processed by everyone on the current Form before moving to the next Form.

How to add image in west and buttons in east side in container of codenameone

I would like to create a screen as shown in image.
I have tried the following code, it works fine in simulator but when on device it dont show images it just shows button.So, May I know whats the reason? or Am I doing anything wrong ?
public class PhotoExample extends com.codename1.ui.Form {
Container commandBar = null;
public PhotoAnnotatorScreen(String title) {
setTitle(title);
// give this form a border layout
setLayout(new BorderLayout());
getTitleArea().setPreferredH(0);
drawableArea = new AnnotatingArea(graffitiModel);
addComponent(BorderLayout.CENTER, drawableArea);
buildCommandBar();
getStyle().setBgColor(0x323232);
}
protected void buildCommandBar() {
Container c = new Container();
c.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
c.getStyle().setPadding(Component.TOP, 40);
Button palletteCommand = new Button();
Image im = Image.createImage("/sun.png");
palletteCommand.setIcon(im);
c.addComponent(palletteCommand);
Button cancelCommand = new Button();
Image im = Image.createImage("/Cancel-64-ltgrey.png");
cancelCommand.setIcon(im);
c.addComponent(cancelCommand);
Button okCommand = new Button();
Image im = Image.createImage("/Checked-64-ltgrey.png");
okCommand.setIcon(im);
c.addComponent(okCommand);
addComponent(BorderLayout.EAST, c);
commandBar = c;
}
}
In this case the drawableArea is nothing but the captured image or any image that need to be placed in container.
Please help me out on this. Thanks.

JAVAFX: Buttons with actions (open files)

I am fairly new to Javafx and I would like to be able to add "Actions" to my toolbar buttons for example "Tutorial" to open a PDF from the project root.
Im a bit stumped and I wonder, is there an easy to way add buttons with actions to this code?
#Override
public void start(Stage primaryStage) throws Exception {
Button btnNewGame = new Button("New Game");
Button btnConcede = new Button("Concede");
Button btnNetwork = new Button("Network");
Button btnTutorial = new Button("Tutorial");
ToolBar toolBar = new ToolBar();
toolBar.getItems().addAll( new Separator(), btnNewGame, btnConcede, btnNetwork, btnTutorial);
BorderPane pane = new BorderPane();
pane.setTop(toolBar);
pane.setCenter(createContent());
Scene scene = new Scene(pane, 640, 675);
primaryStage.setTitle("Dam spill - OBJ2000 Eksamen 2016");
primaryStage.setScene(scene);
primaryStage.show();
}
try this:
button.setOnAction(event -> {
System.out.println("button pressed");
} );

HTML and webbrowser

I tried to use html page in my code for that i found way from kichensink application, i am using same code and same page.html file applicatin working on simulator but not working on devices. Ondevices i got a blank screen. Below is my code. Please help me on this.
void ShowForm()
{
Form f = new Form("testweb");
Container cnt = new Container(new BorderLayout());
cnt = createDemo();
f.setLayout(new BorderLayout());
f.addComponent(BorderLayout.CENTER, cnt);
f.show();
}
public Container createDemo() {
Container cnt = new Container(new BorderLayout());
final WebBrowser wb = new WebBrowser();
if(wb.getInternal() instanceof BrowserComponent) {
Button btn = new Button("Add");
final TextArea content = new TextArea();
Container north = new Container(new BorderLayout());
north.addComponent(BorderLayout.CENTER, content);
north.addComponent(BorderLayout.EAST, btn);
cnt.addComponent(BorderLayout.NORTH, north);
content.setHint("Add to web document");
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
((BrowserComponent)wb.getInternal()).execute("fnc('" + content.getText() + "');");
}
});
((BrowserComponent)wb.getInternal()).setBrowserNavigationCallback(new BrowserNavigationCallback() {
public boolean shouldNavigate(String url) {
if(url.startsWith("http://sayhello")) {
// warning!!! This is not on the EDT and this method MUST return immediately!
Display.getInstance().callSerially(new Runnable() {
public void run() {
((BrowserComponent)wb.getInternal()).execute("fnc('this was written by Java code!');");
}
});
return false;
}
return true;
}
});
}
cnt.addComponent(BorderLayout.CENTER, wb);
wb.setURL("jar:///page.html");
return cnt;
}
Hi,I did few changes setlayout for container and add to another cotainer with scrollable true for container and scrollable false for form but now it's giving me error on devices and error is: "web page not available" page.html not found. Whereas page.html is already placed in src with .res file and application on simulator working fine.
Regards,
Jeny
You can't make a border layout scrollable, including nested scrollables and scrolling native+Codename One widgets in sync is probably not a good idea.
Which device are you having a problem on? There was an issue with browser component on Android for some use cases, its fixed now.

AWT repaint issues

I am trying to make some small additions to some old java code that does not support swing. I need to add a small dialog that contains a panel which has a checkbox and a couple text fields. When the user clicks on the checkbox I want to disable or enable the checkboxes. This part seems to work well but the text fields are not properly getting redrawn. When I click the checkbox the fields do not appear to become enabled but if I then click on the panel or the text field you see that they are enabled (the opposite is also true, when I un-check the checkbox the fields still look enabled until you try and click on them and they become ghosted and do not become selected). I use the setEnabled(boolean) to set the status of the fields. I have tried calling repaint and validate on both the fields and the panel after changing the status and this does not seem to work. I have also tried to have the fields request focus and this did not work. Anyone have any other ideas?
//The class that contains all of this is of type Window
//Declaration of the components
private Panel _inputPanel;
private TextField min , max;
//This method adds to two text fields
public void addMinMaxtextFields(String min, String max) {
TextField minField = new TextField(min);
TextField maxField = new TextField(max);
this.min = minField;
this.max = maxField;
this.min.setEnabled(false);
this.max.setEnabled(false);
_inputPanel.add(minField);
_inputPanel.add(maxField);
}
//listener for the checkbox
public void itemStateChanged(ItemEvent e) {
Component[] components = _inputPanel.getComponents();
min.setEnabled(!min.isEnabled());
min.setVisible(true);
min.validate();
min.repaint();
_inputPanel.validate();
_inputPanel.repaint();
this.pack();
this.setSize(this.getWidth(), this.getHeight());
this.validate();
this.repaint();
/* do nothing */
}
You will need to call update(Graphics g) on Panel after setEnabled(boolean) is called.
check :
http://download-llnw.oracle.com/javase/1.4.2/docs/api/java/awt/Container.html#update(java.awt.Graphics)
I tried following code (built from code you provided), Its working fine.
import java.awt.Checkbox;
import java.awt.Component;
import java.awt.Dialog;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
public class CheckUI extends Dialog implements ItemListener {
// The class that contains all of this is of type Window
// Declaration of the components
private Panel _inputPanel;
private TextField min, max;
private Checkbox cb;
public CheckUI(Frame owner, boolean modal) {
super(owner, modal);
_inputPanel = new Panel();
this.add(_inputPanel);
addMinMaxtextFields("min", "max");
}
// This method adds to two text fields
public void addMinMaxtextFields(String min, String max) {
cb = new Checkbox();
cb.addItemListener(this);
TextField minField = new TextField(min);
TextField maxField = new TextField(max);
this.min = minField;
this.max = maxField;
this.min.setEnabled(false);
this.max.setEnabled(false);
_inputPanel.add(minField);
_inputPanel.add(maxField);
_inputPanel.add(cb);
}
// listener for the checkbox
public void itemStateChanged(ItemEvent e) {
Component[] components = _inputPanel.getComponents();
min.setEnabled(!min.isEnabled());
min.setVisible(true);
min.validate();
min.repaint();
_inputPanel.validate();
_inputPanel.repaint();
this.pack();
this.setSize(this.getWidth(), this.getHeight());
this.validate();
this.repaint();
/* do nothing */
}
/**
* #param args
*/
public static void main(String[] args) {
Frame parent = new Frame();
parent.setVisible(true);
parent.setExtendedState(Frame.MAXIMIZED_BOTH);
parent.pack();
CheckUI ui = new CheckUI(parent, true);
ui.pack();
ui.setVisible(true);
}
}

Resources