Switching theme by button Codename One - java

I'm having issues when I change my theme, I have a default theme and a theme called Blue, I change to Blue by pressing a button then it's working good untill I get back to my main Menu then it changes back to my default theme by overriding my Blue theme. I want to avoid it.
This is my initVars:
protected void initVars(Resources res){
Toolbar.setOnTopSideMenu(false);
}
This is my button to do the change:
#Override
protected void onMain_Button4Action(Component c, ActionEvent event) {
UIManager.initNamedTheme("/theme", "Blue");
Display.getInstance().getCurrent().refreshTheme();
}
And this is my button function to back to my main Menu:
back.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ev)
{
new StateMachine("/theme");
}
});
Should I set something in initVars or in my ActionListener to avoid overriding?

I would suggest staring over with a new project. You are using the old GUI builder which is deprecated.
Assuming both themes are in the main res file you don't need to do that. You just need a reference to the resource file which you can get in the old GUI builder using fetchResourceFile().
Hashtable themeData = theme.getTheme("Theme Name");
UIManager.getInstance().setThemeProps(themeData);
Display.getInstance().getCurrent().refreshTheme();

Related

Problems when creating containers in CodenameOne

For my main App I would need a custom container for navigation purpose, see here (Stackoverflow).
As the solution posted did not work for me (I tried with simple System.out.println), I began a new Project to understand, how the container embedding works, but it does not work the way I expected.
So the new App is the Hi World application with the orange color.
I created a new blank container in the GUI designer and added 3 Buttons.
I created a new blank container in the GUI designer and added 3 Buttons.
I added an actionListener to them within the container
I added the container to the form
My StateMachine looked like this:
#Override
protected void onButtonCont_ButtonAction(Component c, ActionEvent event) {
System.out.println("button1 clicked");
}
#Override
protected void onButtonCont_Button1Action(Component c, ActionEvent event) {
System.out.println("button2 clicked");
}
#Override
protected void onButtonCont_Button2Action(Component c, ActionEvent event) {
System.out.println("button3 clicked");
}
(The container I created was named ButtonCont..) Nothing else modified in StateMachine or elsewhere!
Now I started the app and clicked the buttons - but nothing happened.
So I
Opened the GUI Builder
Selected MainForm
selected the three Buttons one after another
added ActionListeners to each one
Now my StateMachine looks like this:
#Override
protected void onMain_Button2Action(Component c, ActionEvent event) {
System.out.println("button3 -now- clicked");
}
#Override
protected void onMain_Button1Action(Component c, ActionEvent event) {
System.out.println("button2 -now- clicked");
}
#Override
protected void onMain_ButtonAction(Component c, ActionEvent event) {
System.out.println("button1 -now- clicked");
}
(in addition to the previous onButtonCont- methods)
Starting the app and clicking the buttons results in this output:
button1 -now- clicked
button3 -now- clicked
button2 -now- clicked
What am I doing wrong?
I found the answer myself.
Instead of adding the container to the Form under the section "user defined", you simply need to add an embedded container and select "embedded | [null]" to your own container

JavaFX subclassed Button - How to make Label update work?

I want to have several JavaFX Buttons that update one Label in my Application with text. For testing purposes it's just Button Text.
What I did at first worked fine and looked like this:
String Text = "...";
public void kons() {
System.out.println("Works...");
System.out.println(Text);
Tekst.setText(Text);
Button G4 = new Button("Spadantes");
G4.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
Text = G4.getText();
kons();
}
});
Then I decided to stylize my buttons with CSS and because I wanted to have several groups of buttons stylized in different way I subclassed JavaFX Button class in this way:
public class Buttons extends Button {
public Buttons(String text) {
super(text);
getStylesheets().clear();
getStylesheets().add("./Buttons.css");
Which still worked. But now I want my event handler to be moved to Button subclass (to avoid copy-pasting exactly same code into each and every button of mine). What I did looks like this:
public class Buttons extends Button {
public Buttons(String text) {
super(text);
getStylesheets().clear();
getStylesheets().add("./Buttons.css");
setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
Main.Text = getText();
Main.kons();
}
});
}
}
Main is my extend Application class
Tekst is my label.
And sadly it throws me exception about calling non-stathic method and variable from static context. From what I understand instances are static and definitions are non-static. I tried to change everything "in the way" to static but it gives me red wall of errors after clicking button (nothing in compilation process). I also tried to call instance of my Application somehow but I have no idea how (from what I understand extend Application class intantiates itself on it's own while starting program so there's no "name" by which I can call it's Label.
What I'm looking for is "quick and dirty solution" to be able to use subclassed buttons (or other sliders, text-fields, etc.) that can call a method that updates something "on screen".
[EDIT] I'm using newest Java there is of course. In case it matters.
Instead of subclassing, why not just write a utility method that creates the buttons for you? I would also not recommend making the text variable an instance variable: just reference the Label directly.
public class SomeClass {
private Label tekst ;
// ...
private Button createButton(String buttonText) {
Button button = new Button(buttonText);
button.getStylesheets().add("Buttons.css") ;
button.setOnAction(e -> tekst.setText(buttonText));
return button ;
}
}
Then, from within the same class, when you need one of those buttons you just do
Button button = createButton("Text");
If you really want to subclass (which just seems unnecessary to me), you need to pass a reference to the label to the subclass:
public class LabelUpdatingButton extends Button {
public LabelUpdatingButton(String text, Label labelToUpdate) {
super(text);
getStylesheets().add("Buttons.css");
setOnAction(e -> labelToUpdate.setText(getText()) );
}
}
Then from your class that assembles the UI you can do
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
Label tekst = new Label();
Button someButton = new LabelUpdatingButton("Button text", tekst);
// etc...
}
}
But again, creating a subclass that does nothing other than define a constructor that calls public API methods is redundant, imo.
Also, it's a bit unusual to create an entire stylesheet just for your buttons. Typically you would set a style class on the Button:
button.getStyleClass().add("my-button-class");
and then in the stylesheet you add to the Scene do
.my-button-class {
/* styles for this type of button */
}

How to change SWT Button background color or make it transparent

I have a transparent image on a Button (no text), which is placed on a Composite. Since the Composite is white (created with FormToolkit#createComposite(parent, SWT.NONE)), I'd like the Button background to be the same color. How do I do it?
The Label does the trick, but doesn't have the shadows like Button does when I'm clicking on it..
The background color of a Button is determined by the OS. In fact, the documentation for Control.setBackground() states that:
Note: This operation is a hint and may be overridden by the platform. For example, on Windows the background of a Button cannot be changed.
That said, one possible way to circumvent this is to override the paint event as shown here: Changing org.eclipse.swt.widgets background color in Windows. When I tried this out the results were a bit wonky.
The safest and most consistent approach would be to use a label like in your second image, but have different images to display on various mouse events to emulate how a button behaves.
Those images can emulate the shadow just by adding whatever shape of shadow you want to the image itself. That shadow can also change for each image to give the impression that the button is being pressed or not.
For example, I'm thinking something along the lines of:
public class MyButton {
private final Label buttonLabel;
public MyButton(final Composite parent, final Theme theme) {
buttonLabel = new Label(parent, SWT.NONE);
buttonLabel.setImage(theme.getUpImage());
buttonLabel.addMouseListener(new MouseAdapter() {
#Override
public void mouseDown(final MouseEvent mouseEvent) {
buttonLabel.setImage(theme.getButtonPressedImage());
}
#Override
public void mouseUp(final MouseEvent mouseEvent) {
buttonLabel.setImage(theme.getButtonUpImage());
}
});
buttonLabel.addMouseTrackListener(new MouseTrackAdapter() {
#Override
public void mouseEnter(final MouseEvent mouseEvent) {
buttonLabel.setImage(theme.getButtonHoverImage());
}
#Override
public void mouseExit(final MouseEvent mouseEvent) {
buttonLabel.setImage(theme.getButtonUpImage());
}
});
}
}
Where the Theme just has all of the images already conveniently loaded.
You'll also need to make sure that the parent Composite has the background mode set to force its background color:
parent.setBackgroundMode(SWT.INHERIT_FORCE);
Obviously the drawback to this approach is that you have to handle the mouse click logic yourself (ie. mouseDown isn't really clicked until the mouse is released, so you'll have to handle the state of the button in each listener method).

Changing a variable in another class without a static variable?

in the program that I am writing at the moment, I have 2 JFrames (each in a different class, each has a different purpose, however you could consider the widget frame to be a slave of some sort), one is a main window, and the other is a 'widget' that pops up upon hitting a button in the main window.
I only want one copy of the widget open at one time. I am currently doing this through boolean variables under an actionPerformed action listener. Below is the action listener for the main window.
public void actionPerformed(ActionEvent e) {
if(getOpenWidget() == false){
System.out.println(getOpenWidget()); //test line
widget.initialize(); // please note that the instance "widget" is declared just after "public class MainWindow{" :)
widget.frame.setVisible(true);
setOpenWidget(true);
System.out.println(getOpenWidget() ); // test line
}else{
System.out.println(getOpenWidget());
JOptionPane.showMessageDialog(frame, "There is already an instance of the Booking Widget open.");
}
}
Now the booking widget is open, on the booking widget there is a cancel button. Below here is the action listener for the widget's 'cancel' button.
btnCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
MainWindow ui = new MainWindow();
frame.dispose();
ui.setOpenWidget(false);
}
}
Now, upon hitting my button in the main window again, in theory, the openWidget bool should be false, and allow me to open another window, however in the cancel button action listener, my variable isnt changed. So, am I going about my problem in the right way without making openWidget a static variable?(I should be using getters and setters right?)
What am I doing wrong and what don't I understand about instantiating a new instance of the main window every time I click that button?
Also, my getters and setters are stock standard as follows.
void setOpenWidget(boolean val){
this.openWidget = val;
}
boolean getOpenWidget(){
return this.openWidget;
}
Just pass the reference of MainWindow to the Widget class so that it can update its flag on cancel button.
You are calling setOpenWidget(false) on some other new instance you have created using this line MainWindow ui = new MainWindow();
You should call setOpenWidget(false) using same instance from which you have initialised widget. Pass the reference of MainWindow to widget while creating widget and invoke setOpenWidget(false) using that reference
When you are creating the object of widget within MainWindow you can call it like this:
widget = new Widget(this);
And change the Widget Window Constructor as follows:
MainWindow ui;
public Widget(MainWindow mw)
{
this.ui = mw;
//...initialize btnCancel...
btnCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
frame.dispose();
ui.setOpenWidget(false);
}
});
//..do all other stuffs here...
}

Vaadin ColorPicker IllegalArgumentException

I have downloaded the Vaadin Colorpicker addon to try it out , there is a small problem if i klick the colorPicker "Button" twice i get an IllegalArgumentException :
Exception
java.lang.IllegalArgumentException: Window was already added to application - it can not be added to another window also.
at com.vaadin.ui.Window.addWindow(Window.java:1447)
at com.vaadin.addon.colorpicker.ColorPicker.changeVariables(Unknown Source)
at com.vaadin.terminal.gwt.server.AbstractCommunicationManager.handleVariableBurst(AbstractCommunicationManager.java:1299)
at com.vaadin.terminal.gwt.server.AbstractCommunicationManager.handleVariables(AbstractCommunicationManager.java:1219)
at com.vaadin.terminal.gwt.server.AbstractCommunicationManager.doHandleUidlRequest(AbstractCommunicationManager.java:735)
Another question :
I want the colorPicker window to popup if I click on an item in a Menu something like if i click on "set Color" i get the colorPicker window. its quite hard to place the button on my GUI :P
EDIT :
Im adding the ColorPicker like this :
colorPicker = new ColorPicker();
colorPicker.setButtonCaption("Set Color");
colorPicker.setRGBVisibility(false);
colorPicker.setHSVVisibility(false);
colorPicker.setHistoryVisibility(false);
colorPicker.addListener(this);
window.addComponent(colorPicker);
I think you should try this code in your application:
public class MyApplication extends Application {
#Override
public void init() {
Window mainWindow = new Window("Your Application");
// Create a color picker
ColorPicker cp = new ColorPicker("ColorPicker", Color.RED);
// Add a color change listener to the color picker
cp.addListener(new ColorPicker.ColorChangeListener() {
#Override
public void colorChanged(ColorChangeEvent event) {
MyApplication.this.getMainWindow()
.showNotification("Color changed!");
}
});
mainWindow.addComponent(cp);
setMainWindow(mainWindow);
}
}
If it doesn't work, then there is a defect in ColorPicker (and you could report a defect here: http://dev.vaadin.com/).
If the code above works, the problem is in your code then (in this case, share with us more of your code - you can even share whole class for).

Categories

Resources