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");
} );
Related
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.
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.
I am aware of existing error dialogs but I am trying to create my own dialog from scratch.
public class ExceptionDialog extends Dialog {
private Button okButton;
private SpanLabel errorMessage;
private Label title;
public ExceptionDialog(String titleText, String messageText, String buttonText) {
super();
this.setLayout(new BorderLayout());
okButton = new Button(buttonText);
errorMessage = new SpanLabel(messageText);
title = new Label(titleText);
this.add(BorderLayout.NORTH, title);
this.add(BorderLayout.CENTER_BEHAVIOR_CENTER, errorMessage);
this.add(BorderLayout.SOUTH, okButton);
this.setAutoDispose(false);
this.setDisposeWhenPointerOutOfBounds(true);
this.showStretched(BorderLayout.SOUTH, true);
this.setTransitionInAnimator(CommonTransitions.createSlide(CommonTransitions.SLIDE_VERTICAL, true, 1000));
this.setTransitionOutAnimator(CommonTransitions.createSlide(CommonTransitions.SLIDE_VERTICAL, false, 1000));
Dialog thisDialog = this;
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ev) {
System.out.println("BUTTON CLICKE");
thisDialog.dispose();
}
});
}
}
The problem: I noticed that on initialization, the dialog is showing without really calling ex.show(). Also, the button does not work, the only way to get the dialog to disappear is by clicking somewhere else on the screen.
Form hi = new Form("Hi World", BoxLayout.y());
hi.add(new Label("Hi World"));
hi.show();
ExceptionDialog ex = new ExceptionDialog("A", "B", "C");
Then I tried appending ex.show() to the code above. Now the dialog gets shown twice. The second time with a working button dismissing the dialog.
showStretched is show it will show the dialog and block until the dialog is disposed.
I'm trying to implement an Android style side menu and I'm having an issue implementing the rounded icon on top and labels below it before the sideCommands are added.
How do I implement this please?
You can use Toolbar API which allows you to add components to the Sidemenu.
Have a look at Flickr demo.
Instead of using tool.addCommandToSideMenu(Command) you should use tool.addComponentToSideMenu(yourComponent, CommandToPerform)
Example:
#Override
protected void beforeMain(Form f) {
//Store your commands before setting toolbar
List<Command> cmds = new ArrayList();
for (int i = 0; i < f.getCommandCount(); i++) {
cmds.add(f.getCommand(i));
}
Toolbar toolbar = new Toolbar();
f.setToolBar(toolbar);
Label lblTitle = new Label("My Form", "Title");
lblTitle.setEndsWith3Points(false);
toolbar.setTitleComponent(lblTitle);
// Use your stored commands after setting toolbar
for (Command cmd : cmds) {
toolbar.addCommandToSideMenu(cmd);
}
Container CustomContainer = ...
toolbar.addComponentToSideMenu(CustomContainer, new Command("") {
#Override
public void actionPerformed(ActionEvent evt) {
//What CustomContainer should do (if any)
}
});
f.revalidate();
}
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.