I'm new in LWUIT. I have created a theme for my MIDlet and applied it. The background of the Form, the Label and font change as defined in the theme but the Buttons on the Form do not change like I have defined them in my theme. What could be the problem? Here is a snippet of my code.
import com.sun.lwuit.Button;
import com.sun.lwuit.Display;
import com.sun.lwuit.Form;
import com.sun.lwuit.plaf.UIManager;
import com.sun.lwuit.util.Resources;
import java.io.IOException;
import javax.microedition.midlet.MIDlet;
public class mainMidlet extends MIDlet {
public void startApp()
{
Display.init(this);
Resources r;
try {
r = Resources.open(getClass().getResourceAsStream("res/TUNGtheme.res"));
UIManager.getInstance().setThemeProps(r.getTheme("Theme 2"));
} catch (IOException ex) {
//do something
}
Form f = new Form("App using LWUIT!");
f.show();
Button tuskysButton = new Button("Tuskys");
f.addComponent(tuskysButton);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}
I suppose that the UIID that you defined for the Buttons has the right name. I mean in the ResourceEditor you have to apply the style for elements called Button. If that is right, did you set selected and unselected style? If I can see a pair of screens from your Resource Editor, I could help you more.
I'm guessing the Button is focused and you only defined the unselected style and not the selected/pressed styles.
Related
I'm currently trying to learn JavaFx, and I'm now stuck on a problem. By using a scanner I want to update my label on stage consecutively.
I have tried to use platform.runLater, but this only shows one update. It doesnt update the label every time I write something new in my console.
This is what I have been using:
Platform.runLater(new Runnable() {
#Override
public void run() {
label.setText(sc.nextLine());
}
});
The nextLine() method in Scanner is a blocking call: you should never block the FX Application Thread. You need to create a background thread to read from the scanner, and then update the label on the FX Application Thread:
import java.util.Scanner;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class UpdateLabelFromScanner extends Application {
#Override
public void start(Stage primaryStage) {
Label label = new Label();
Thread scannerReadThread = new Thread(() -> {
try (Scanner scanner = new Scanner(System.in)) {
while (scanner.hasNextLine()) {
String line = scanner.nextLine() ;
Platform.runLater(() -> label.setText(line));
}
} catch (Exception exc) {
exc.printStackTrace();
}
});
scannerReadThread.setDaemon(true);
scannerReadThread.start();
StackPane root = new StackPane(label);
primaryStage.setScene(new Scene(root, 180, 120));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
There are a few ways you can do this. One way to is to read the text into an observable property, and when that property changes, you update your label. One of the objects you can use to do this is called SimpleStringProperty.
Declare it like this:
private StringProperty someText = new SimpleStringProperty();
In a constructor or some initialization function, add a new ChangeListener to the property:
someText.addListener((observable, oldValue, newValue) -> {
Platform.runLater(() -> {
label.setText(newValue);
});
});
When you read input from your scanner, change the value of your observable and the listener you added will be invoked, thus changing the text of your label:
someText.set(sc.nextLine());
When i right click the FXML document and click open with scene builder the scene builder opens on my taskbar but if i click it, it will not show anything can anyone please help me?
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
try {
Parent root = FXMLLoader.load(getClass().getResource(
"/application/MyView.fxml"));
Scene scene = new Scene(root);
scene.getStylesheets().add(
getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
setUserAgentStylesheet(STYLESHEET_CASPIAN);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
This is my main class
I found out that it is a bug and you have to open Scene Builder from your desktop and:
File>Preferences>Reset To BuiltIn Defaults
File>Open recent>clear menu
I installed scene builder from a different maker (gluon)
Works wonderfully.
Associate the FXML files in your SO to that they are open by this application instead of the "normal" one and you can forget about this problem.
I did a try with effect of the framework, but it has some weird behaviour when I blur a textfield into a Parent, the textfield is positioned at a different place, please take a look :
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TextField;
import javafx.scene.effect.GaussianBlur;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class BlurTest extends Application {
CTextView subPane = new CTextView(100,100);
#Override
public void start(Stage stage) throws Exception {
VBox myBox = new VBox();
CheckBox chkBlur = new CheckBox("Show");
chkBlur.selectedProperty().addListener(new ChangeListener<Boolean>(){
#Override
public void changed(ObservableValue<? extends Boolean> v,
Boolean oldValue, Boolean newValue) {
if(oldValue)
subPane.getTxt().setEffect(new GaussianBlur());
else
subPane.getTxt().setEffect(null);
}
});
myBox.getChildren().addAll(new TextField("Not blur"), subPane, new TextField("Not blur"), chkBlur);
myBox.setPrefSize(250, 500);
Scene scene = new Scene(myBox);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
And my custom textview :
import javafx.scene.Parent;
import javafx.scene.control.TextField;
public class CTextView extends Parent {
private TextField txt;
public CTextView(double w, double h) {
super();
this.txt = new TextField("Default");
this.txt.setLayoutX(20);
this.txt.setLayoutY(20);
this.getChildren().add(this.txt);
}
public TextField getTxt() {
return txt;
}
}
I don't understand why the textfield is repositioned in the Parent after blur effect.. :/
Thanks for your help
> Why is the textfield repositioned?
The GaussianBlur's default radius value is 10. When this effect applied to the node, that node's local bounds expands extra these blurring radii, but the node's width and height remains the same. The Parent does not apply CSS style and does not layout its children, however as seen in your example, it takes into account the local bounds and repositioned the node.
> Why do the textfield's setLayoutX and setLayoutY not worked?
The Parent does consider the local bounds of its child but it does not layout them according the child's layout values. Use a Region (or its subclasses) which takes care its children layout values.
public class CTextView extends Region {
private TextField txt;
public CTextView(double w, double h) {
super();
this.txt = new TextField("Default");
this.txt.setLayoutX(20);
this.txt.setLayoutY(20);
this.getChildren().add(this.txt);
}
public TextField getTxt() {
return txt;
}
}
actually i'm looking for something very similar to this thread:
How to hide the controls of HTMLEditor?
so basically i try to add a custom button to the javafx html editor but with the difference that it's implemented through FXML.
So my question is:
Is there a "work-around" to add custom buttons to the html-editor when it's implemented through FXML?
Sample solution is :
htmlEditor.setVisible(false);
Platform.runLater(new Runnable() {
#Override
public void run() {
Node[] nodes = htmlEditor.lookupAll(".tool-bar").toArray(new Node[0]);
for (Node node : nodes) {
node.setVisible(false);
node.setManaged(false);
}
htmlEditor.setVisible(true);
}
});
I have modified the #jewelsea answer for javaFX9.
I have also added some customization to move toolbars. The main idea is to get all the components by css selector, then modify or hide them. Read the class HTMLEditorSkin to get the CSS classes names, like ".html-editor-align-center" for the align button.
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.regex.Pattern;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.MenuButton;
import javafx.scene.control.MenuItem;
import javafx.scene.control.RadioMenuItem;
import javafx.scene.control.ToolBar;
import javafx.scene.effect.DropShadow;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.scene.web.HTMLEditor;
import javafx.stage.Stage;
public class HTMLEditorCustomizationSample2 extends Application {
// limits the fonts a user can select from in the html editor.
private static final ObservableList<String> limitedFonts = FXCollections.observableArrayList("Arial",
"Times New Roman", "Courier New", "Comic Sans MS");
private HTMLEditor htmlEditor;
public static void main(String[] args) {
launch(args);
}
#SuppressWarnings("unchecked")
#Override
public void start(Stage stage) {
htmlEditor = new HTMLEditor();
stage.setScene(new Scene(htmlEditor));
stage.show();
customizeEditor(htmlEditor);
}
private void customizeEditor(HTMLEditor htmlEditor) {
// hide controls we don't need.
Node seperator = htmlEditor.lookup(".separator");
seperator.setVisible(false);
seperator.setManaged(false);
hideByClass(htmlEditor, ".separator");
hideByClass(htmlEditor, ".html-editor-cut", ".html-editor-copy", ".html-editor-paste", ".html-editor-strike",
".html-editor-hr");
hideByClass(htmlEditor, ".html-editor-align-left"
, ".html-editor-align-center"
, ".html-editor-align-right"
, ".html-editor-align-justify", ".html-editor-outdent"
, ".html-editor-indent", ".html-editor-bullets"
, ".html-editor-numbers");
// Move the toolbars
Node top= htmlEditor.lookup(".top-toolbar");
GridPane.setConstraints(top,1,0,1,1);
Node bottom= htmlEditor.lookup(".bottom-toolbar");
GridPane.setConstraints(bottom,0,0,1,1);
Node web= htmlEditor.lookup("WebView");
GridPane.setConstraints(web,0,1,2,1);
// modify font selections.
int i = 0;
Set<Node> fonts = htmlEditor.lookupAll(".font-menu-button");
Iterator<Node> fontsIterator = fonts.iterator();
fontsIterator.next();
ComboBox<String> formatComboBox = (ComboBox<String>) fontsIterator.next();
formatComboBox.itemsProperty().addListener((obs, old, value) -> {
if (value.size() != limitedFonts.size()) {// should loop on array for equality
Platform.runLater(() -> {
value.clear();
// stop.set(true);
value.addAll(limitedFonts);
formatComboBox.setValue(limitedFonts.get(0));
});
}
});
// add a custom button to the top toolbar.
Node node = htmlEditor.lookup(".top-toolbar");
if (node instanceof ToolBar) {
ToolBar bar = (ToolBar) node;
ImageView graphic = new ImageView(
new Image("http://bluebuddies.com/gallery/title/jpg/Smurf_Fun_100x100.jpg", 16 , 16, true, true));
graphic.setEffect(new DropShadow());
Button smurfButton = new Button("", graphic);
bar.getItems().add(smurfButton);
smurfButton.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent arg0) {
htmlEditor.setHtmlText("<font face='Comic Sans MS' color='blue'>Smurfs are having fun :-)</font>");
}
});
}
}
private void hideByClass(HTMLEditor htmlEditor, String... selectors) {
for (String selector : selectors) {
Set<Node> nodes = htmlEditor.lookupAll(selector);
for (Node node : nodes) {
node.setVisible(false);
node.setManaged(false);
}
}
}
#Override
public void stop() throws Exception {
super.stop();
System.out.println(htmlEditor.getHtmlText());
}
}
Here is some sample code which customizes the HTMLEditor and adds a custom button to it. The sample code does not use fxml but really it's very similar if fxml is used. You could define the HTMLEditor in fxml and inject it into your Controller using the standard #FXML annotation. Once you have a reference to the editor, customize it in Java code using an appropriate variation of the sample code. For the added button, just create it in Java rather than fxml and it will be simpler.
import com.sun.lwuit.Button;
import com.sun.lwuit.Command;
import com.sun.lwuit.Display;
import com.sun.lwuit.Label;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.plaf.UIManager;
import com.sun.lwuit.util.Resources;
import java.io.IOException;
public class Ruwwa extends javax.microedition.midlet.MIDlet implements ActionListener{
Form f;
Button mybutton1;
Button mybutton2;
Command exit;
Command ok;
public void startApp() {
Display.init(this);
f = new Form();
try {
Resources r = Resources.open("/mairuwa.res");
UIManager.getInstance().setThemeProps(r.getTheme("Mairuwa Theme"));
} catch (IOException ioe) {
ioe.printStackTrace();
}
mybutton1=new Button("Report A Problem");
mybutton2=new Button("Request Info");
f.setLayout(new BorderLayout());
f.addComponent(BorderLayout.CENTER, new Label("The Mairuwa Portal"));
ok = new Command("OK");
exit = new Command("Exit");
f.addCommand(ok);
f.addCommand(exit);
f.addCommandListener(this);
f.show();
}
public void pauseApp() {}
public void destroyApp(boolean unconditional) {}
public void actionPerformed(ActionEvent ae) {
notifyDestroyed();
}
}
I would like to add another label under the "The Mairuwa Portal" and also place two buttons("Report A Problem","Request Information") beneath this as well. An illustration of what i am describing is
label: The Mairuwa Portal
then another label beneath it: I want to:
Then two buttons beneath this Button:Report Problem Button: Request Information
I have been able to add OK and EXIT button to the project,but this above buttons i talked about should as I described.
These buttons will carry functionality. I hope this can be done in LWUIT.
It just simple. Use the BoxLayout.Y_AXIS for Form and add the labels to the form. Create the Container with BoxLayout.Y_AXIS (or x_AXIS, Its your needs) and add the buttons to this Container and set the margin for the Container. See the sample code for how to do,
Form form = new Form("form");
form.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
Label label1 = new Label("Label 1");
Label label2 = new Label("Label 2");
form.addComponent(label1);
form.addComponent(label2);
Container c = new Container(new BoxLayout(BoxLayout.X_AXIS));
int center = Display.getInstance().getDisplayWidth()/2;
c.getStyle().setMargin(0, 0, center , 0);
Button b1 = new Button("button 1");
Button b2 = new Button("button 2");
c.addComponent(b1);
c.addComponent(b2);
form.addComponent(c);
form.show();