I have two Screens Screen1 and Screen 2.
Screen1 contains a LabelField1 and a Button "ok".
Screen2 contains a LabelField2.
When the ok button is clicked the text in LabelField1 should be displayed in LabelField2(Screen1 should be popped and Screen2 should be opened).
Try this -
//This will close current screen.
UiApplication.getUiApplication().popScreen(this);
Then pass the value of label 1 to screen 2.
UiApplication.getUiApplication().pushScreen(new screen2(value_of_label1));
And in the screen 2 class, just display the string value.
public class screen2 extends MainScreen{
public screen2(String label1_value){
// TODO: create label field first
LabelField2.setText(label1_value);
}
}
Actually I got a different information
As I said before, two Screens(classes), Screen1 and Screen2.
Let the strings to be passed from Screen1 be name and id.
Let Screen2 contains label1 and label2 for displaying,
We can just use the following code,
label1.setText(Screen1.name);
add(label1);
label2=new LabelField(Screen1.id);
add(label2);
Now the values of name and Id from Screen1 will be displayed in both the labels in Screen2
Related
I'm building an app which which has built in with 2 different tabs. First tab is is "Home" which basically has a tableview with cells that configured from an api.(The api gets me country names for now)
Those cells also have a "Star" button which prints the data of the specific cell for now.
Second tab is "Saved" tab(SavedViewController), where I want to show the "starred" countries, using a tableview.
You can see the image below in order to get an idea for the app.
App simulation Image
The star button has a function in my CountriesTableViewCell. I'm using a saveButtonDelegate in order to let the SavedViewController know about an item is going to be saved. The code in CountriesTableViewCell for star button is as below.
#objc func buttonTapped() {
//If Button is selected fill the image. Else unfill it.
if !isSaveButtonSelected {
saveButton.setImage(UIImage(systemName: "star.fill"), for: .normal)
isSaveButtonSelected = true
saveButtonDelegate?.saveButtonClicked(with: countryData) //Checking if save button is clicked
}
}
countryData is the data that I get from the api, and this is the data I want to pass to SavedViewController.
struct CountryData: Codable {
let name : String
}
So on the SavedViewController, I'm handling the data using the SaveButtonProtocol conformance as below:
extension SavedViewController: SaveButtonProtocol {
func saveButtonClicked(with data: CountryData) {
countryDataArray.append(data)
print("saveButtonClicked")
print("countryData in savevc is \(countryDataArray)")
DispatchQueue.main.async {
self.countriesTableView.reloadData()
}
}
}
Whenever I click the star button on the first tab, this function is getting called on SavedViewController. So whenever I click to button, those print statements above work fine.
The problem is, whenever the star button is clicked, it should append the data of the current clicked cell to countryDataArray in SavedViewController. But the array is not populating as it should.
Let's say I pressed the first cell's star button, my print("countryData in savevc is (countryDataArray)") statement prints : ["Vatican City"], then I press the second cell's star button it only prints ["Ethiopia"] while it should print ["Vatican City", "Ethiopia"]
Why this issue is happening? My best guess is I'm delegating SavedViewController from the cell class so it behaves differently for every other cell. If that is the problem what should I do to solve it?
Many Thanks.
You should store your data in a shared (static array) object so you only have one source and add the saved indicator in your country struct so you do not rely on what is displayed in one view controller.
I use the menuBar component in my application and I would like to add a "mini" form in the menu.
In the "contact" menu, the user can fill a TextField with a phone number and click on a submit button to be called.
My issue is when I select the TextField, the menu closes automatically.
Do you know if it's possible to do that and how I can do ?
Thank you.
This an example of my menu
public class HeaderView extends MenuBar implements RouterLayout {
private final MenuItem tools;
private final MenuItem contacts;
public HeaderView() {
// logo
final Image img = new Image("img/logo.png", "image");
img.setHeight("44px");
this.addItem(img);
// TOOLS
tools = this.addItem("Tools");
tools.addComponentAsFirst(new Icon(VaadinIcon.TOOLS));
// CONTACTS
contacts = this.addItem("Contacts");
contacts.addComponentAsFirst(new Icon(VaadinIcon.PHONE));
// layout for the form contact
final FormLayout nameLayout = new FormLayout();
final TextField phoneField = new TextField();
phoneField.setLabel("Phone");
phoneField.setPlaceholder("Phone");
final Button sendButton = new Button("send");
// add textfield and button to the layout
nameLayout.add(phoneField, sendButton);
// add the form to the menubar
contacts.getSubMenu().addItem(nameLayout);
}
You can try changing contacts.getSubMenu().addItem(nameLayout) to contacts.getSubMenu().add(nameLayout) I'm not sure if you can self trigger closure of the submenu after submission. If that is required.
I have a grid with several columns. For three columns i used the column renderer. Each of the columns contains one button.
If i click one of those buttons, i want to replace the three buttons in that specific row with two other buttons. All the other rows should not be affected. Is this possible in a Vaadin grid?
Components in different columns don't know each other, as they are all defined in a separate scope (in the componentRenderer of their own column. You cannot define the Button outside of the componentRenderer as you found out in another question today). So the "obvious" solution won't work, where you add a clickListener on the Button to directly change the other buttons.
If you had one column with 3 Buttons inside then this would be much easier.
There is a way, but I see this more as a hack than as a solution. Because you need some extra implementation in the item class for this to work.
In the ComponentRenderer, you can add an if-statement where you look at some value of the item. In one case, you'll render button 1, in the other case you'll render the other button. And in the click listeners of the button you change that value in the item and refresh the dataprovider, so the componentRenderer is invoked again. It will now see the value on the item has changed, therefore displaying some other Button.
Here is some code to show what I mean:
// grid item class
public class Foo {
private boolean buttonPressed = false;
public Foo(){
}
public isButtonPressed(){
return buttonPressed;
}
public setButtonPressed(boolean buttonPressed){
this.buttonPressed = buttonPressed;
}
}
// adding of button columns
// do this 3 times for a test of your scenario.
grid.addComponentColumn(item -> {
if(!item.isButtonPressed()){
return new Button("Before Button was Pressed", click -> {
item.setButtonPressed(true);
grid.getDataProvider().refresh(item);
});
} else {
return new Button("Button was Pressed", click -> {
item.setButtonPressed(false);
grid.getDataProvider().refresh(item);
})
}
})
There are few ViewControllers in my app and all of them have menu button. When this button is pressed - menu ViewController is opened.
I want to mark menu button with red dot showing that some new content is available and user need to press menu button to see which one of menu item is marked with this dot.
As all my buttons are independent of each other - I thought that I need to solve it this way
Add red dot image on each menu button
Make this dot hidden by default
When each ViewController is opened - i should check - are there any new items available and switch isHidden property of this red dot image to false.
But maybe there is some more elegant way?
use NotificationCenter to notify the ui when new content available
in the menu viewcontroller class:
//put this in viewDidLoad
NotificationCenter.default.addObserver(self.selector : #selector(menuviewcontroller.refresh(_:)),name:NSNotification.Name(rawValue:"showRedBtn"),object : nill)
//create function refresh
func refresh(_ notification : Notification)
{
//make the red dot visible
}
create class that listen if any content added and call the delegate in case of add by this line of code
NotificationCenter.default.post(name : Notification.Name("showRedBtn"),object : nil , userInfo : nil)
hope it's will help you
My workFlow is like this ... I have taken Three HoriZontalManager one for holding theBackgroundImage of the button and two HoriZontalManager to hold the CustomButton and i am adding the main hfm which hold the button and the background image at top and then i have added the list in a VerticalfieldManager but i dont get the button Working the focus always remains on the list Field. Now when I click on the Button the focus always resides on the First row of the list Field so it always show me the picker which is actually i have implemented on the first row of listfield. How can i Solve it ..
Thanks in Advanced..
Please help.
I have solved This issue ... I had to check that if the focus is on the button then do my work else the list field will be invoked and it solved the issue. And it works for touchScreen simulator as well as trackpad devices.
protected boolean navigationClick(int status, int time) {
// if the row select do something with it
// for first row which is time
if(backCustomButton.isFocus()) {
UiApplication.getUiApplication().pushScreen(new saveScreen());
}else if (saveCustomButton.isFocus()) {
Dialog.inform("Save Button on focus");
}else
if (_list.getSelectedIndex() == 0){
// Do the ListSpecific things
}