How to access Boolean flag into remote Java Class - java

I'm working on a JavaFX application which will have several tab panes which I want to set to visible or hidden using check box which will send boolean flag to render or not to render the component.
Check box
final CheckMenuItem toolbarSubMenuNavigation = new CheckMenuItem("Navigation");
toolbarSubMenuNavigation.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent e)
{
// call here the getter setter and send boolean flag
System.out.println("subsystem1 #1 Enabled!");
}
});
Tab pane which will listen for the boolean property:
public boolean renderTab;
public boolean isRenderTab()
{
return renderTab;
}
public void setRenderTab(boolean renderTab)
{
this.renderTab = renderTab;
}
tabPane.setVisible(renderTab);
The check box and the tab pane are isolated into different Java Classes. I need to send the value of the flag every time when I check or uncheck the flag. Can you tell me how I can send the flag using getter and setter?
EDIT
I tested this code:
final CheckMenuItem toolbarSubMenuNavigation = new CheckMenuItem("Navigation");
toolbarSubMenuNavigation.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent e)
{
boolean dcd = toolbarSubMenuNavigation.isSelected();
DataTabs nn = new DataTabs();
nn.setRenderTab(dcd);
// call here the getter setter and send boolean flag
System.out.println("subsystem1 #1 Enabled!");
}
});
and
public boolean renderTab;
public boolean isRenderTab()
{
return renderTab;
}
public void setRenderTab(boolean renderTab)
{
this.renderTab = renderTab;
}
But it's not working when I switch the checkbox.

No.
Inorder to get that eithr you need to have a intance or you need to create new intance there.
If you create a new object there it will create a fresh intance,which doesnt helps you any more..
I guess the only way you have is to Make the renderTab as a static field and access there.

Related

How to enable a JButton on a condition?

I'm trying to create a JButton that enables when certain conditions are met. The program sets setEnabled(false) as initial value, but after an update, it should be setEnabled(true).
I tried several things. Here some code:
public class SwimAction extends AbstractAction {
private final PoolModel poolModel;
private final Swimmer swimmer;
public SwimAction(PoolModel poolModel, Swimmer swimmer) {
super("GO!");
this.poolModel = poolModel;
this.swimmer = swimmer;
// default
setEnabled(false);
}
I tried the following:
// Replaced the setEnabled(false) by setEnabled(checkGo())
public boolean checkGo(){
return(pool.isNotOccupied());
}
// Overwrite setEnabled
#Overwrite
public void setEnabled(boolean bool){
boolean oldBool = this.enabled;
if (oldBool != bool && pool.isNotOccupied()) {
this.enabled = bool;
this.firePropertyChange("enabled", oldBool, bool);
}
}
However, none of them worked. Anyone knows how to enable the button when a certain condition (pool.isNotOccupied()) is met?
Seems like you need to listener for a change in the state of the pool object's occupied property, and the best way to do this is to give it a listener of some sort. This could be as simple as a ChangeListener or perhaps better, a PropertyChangeListener. The details of the best solution would likely depend much on the structure of your program, of the rest of the code that we can't see, but, if PoolModel is what you're listening to, what if you gave it...
public class PoolModel {
public static final String OCCUPIED = "occupied";
private PropertyChangeSupport propChangeSupport = new PropertyChangeSupport(this);
private boolean occupied;
public void addPropertyChangeListener(PropertyChangeListener l) {
propChangeSupport.addPropertyChangeListener(l);
}
// also have a remove listener
public boolean isOccupied() {
return occupied;
}
public void setOccupied(boolean occupied) {
boolean oldValue = this.occupied;
boolean newValue = occupied;
this.occupied = occupied;
propChangeSupport.firePropertyChange(OCCUPIED, oldValue, newValue);
}
// ......
And then in the code that uses it:
poolModel.addPropertyChangeListener(pcEvent -> {
if (pcEvent.getPropertyName().equals(OCCUPIED)) {
setEnabled((boolean) pcEvent.getNewValue());
}
});

Add behavior to Wicket Tab

I have a wicket application on a page we have various forms for the same model split into separate tabs. What I need to do is whenever a tab is clicked check to see if a js variable tabDirty is set to true or false. If it is true I would launch a confirm prompt if okay then reset that form and move to the clicked tab. If cancel stay on that tab with keeping current changes.
I have this js for the warning nothing fancy
function warnOnChange(){
if(tabDirty){
decision = confirm('Leave?');
if(decision){
resetTab(); //sets tabDirty back to false
} else {
return false;
}
}
}
I have a super simple wicket behavior
public class WarnChangePromptOnClickBehavior extends Behavior {
#Override
public void bind(Component component) {
component.add(JQBehaviors.mouseClick(EditMerchant.WARN_ON_CHANGE));
}
}
and that behavior is added to the AjaxFallBackLink
AjaxTabbedPanel<CustomAjaxTab> tabbedPanel = new AjaxTabbedPanel<CustomAjaxTab>("tabbedPanel", tabList, new Model<>(0)) {
private static final long serialVersionUID = 1L;
#Override
protected WebMarkupContainer newLink(final String linkId, final int index) {
AjaxFallbackLink<Void> link = new AjaxFallbackLink<Void>(linkId) {
private static final long serialVersionUID = 1L;
#Override
public void onClick(final AjaxRequestTarget target) {
TabbedPanel<CustomAjaxTab> selectedTab = setSelectedTab(index);
CustomAjaxTab tab = tabList.get(index);
if (target != null) {
tab.getPanel(linkId);
target.add(selectedTab);
}
onAjaxUpdate(target);
}
};
link.add(new WarnChangePromptOnClickBehavior());
return link;
}
};
Current behavior with this is that if there is no change the tabs switch no prompt. If there is a change then I get the prompt. If okay tabDirty is reset and go to the next page clearing changes. Issue is that if I click cancel I still navigate to the next tab and lose changes. I know there is something in onClick I need to change but it is just not registering with me.
It is not that easy to intercept the JS event loop, especially when using Ajax requests.
Here is an approach that may do the job:
In warnOnChange() if dirty then call event.preventDefault() and event.stopImmediatePropagation(). This will tell the browser to not follow the link / make an Ajax call. Then show the confirmation dialog as you do now.
If the user presses Cancel then there is nothing more to do
If the use confirms then set dirty to false and do jQuery(event.target).triggerHandler(event.type), i.e. execute the same event (click) on the link. This time it won't be dirty and it will proceed with the Ajax call.
Not sure if this is the appropriate way to do this but I solved my issue like this:
Same old js just slightly modified to return what the user chose:
function warnOnChange(){
decision = true;
if(tabDirty){
decision = confirm('Leave?');
if(decision){
resetTab();
}
}
return decision;
}
Dumped the whole behavior code although I still think it could be used just not sure at the moment...
So to make this all work on the link I override the updateAjaxAttributesof the link with a precondition:
AjaxTabbedPanel<CustomAjaxTab> tabbedPanel = new AjaxTabbedPanel<CustomAjaxTab>("tabbedPanel", tabList, new Model<>(0)) {
private static final long serialVersionUID = 1L;
#Override
protected WebMarkupContainer newLink(final String linkId, final int index) {
AjaxFallbackLink<Void> link = new AjaxFallbackLink<Void>(linkId) {
private static final long serialVersionUID = 1L;
#Override
protected void updateAjaxAttributes( AjaxRequestAttributes attributes ) {
super.updateAjaxAttributes( attributes );
AjaxCallListener ajaxCallListener = new AjaxCallListener();
//very important to use the "return" if not then nothing happens with the response
ajaxCallListener.onPrecondition("return " + WARN_ON_CHANGE);
attributes.getAjaxCallListeners().add( ajaxCallListener );
}
#Override
public void onClick(final AjaxRequestTarget target) {
TabbedPanel<CustomAjaxTab> selectedTab = setSelectedTab(index);
CustomAjaxTab tab = tabList.get(index);
if (target != null) {
tab.getPanel(linkId);
target.add(selectedTab);
}
onAjaxUpdate(target);
}
};
link.add(new WarnChangePromptOnClickBehavior());
return link;
}
};

GWT Java - CellTable - ButtonCell - how to make it respond to a click

I have the following ButtonCell. How do I make it respond to a click please (e.g., addClickHandler)? I have tried a number of ways I have found yet none work. None of the Window.alert return a response.
ButtonCell selectButton = new ButtonCell();
Column <HikingMeals,String> update = new Column <HikingMeals,String>(selectButton){
#Override
public String getValue(HikingMeals selectButton)
{
return "Select";
}
public void execute(HikingMeals selectButton) {
// EDIT CODE
Window.alert("Pressed");
}
//#Override
public void update(int index, HikingMeals object, String value) {
// The user clicked on the button for the passed auction.
Window.alert("Pressed2");
}
};
table.addColumn(update, "Select");
You just need to set a FieldUpdater on the update column:
update.setFieldUpdater(new FieldUpdater<HikingMeals, String>() {
#Override
public void update(int index, HikingMeals object, String value) {
Window.alert("Pressed");
}
});

Boolean in Java condition

I have an issue, I have a method which is an action performed. If the checkbox is ticked then additional fields become available. If not ticked then they are greyed out. So basically what I want is to add to this method. I have a first condition and now need to add a second condition to it. I pasted the code snippet below, basically what I need is to put it into an if else, but I get some errors. Any advise is much appreciated.
public void actionPerformed(ActionEvent e) {
boolean sel = _useSSL.isSelected();
_port.setUseSSL(sel);
_keystore.setEnabled(sel);
_passphrase.setEnabled(sel);
L_KEYSTORE.setEnabled(sel);
L_PASSPHRASE.setEnabled(sel);
}
Above is the working method, now I need to add in if _truststore.isSelected(); then execute something else.
How can I add this second Boolean condition to the method?
I think you can do it by building a method per boolean and binding them to one "action performed" method like this:
public void actionPerformedForUseSSL(boolean useSSL) {
_port.setUseSSL(useSSL);
_keystore.setEnabled(useSSL);
_passphrase.setEnabled(useSSL);
L_KEYSTORE.setEnabled(useSSL);
L_PASSPHRASE.setEnabled(useSSL);
}
public void actionPerformedForTrustStore(boolean trustStore) {
_port.setTrustStore(trustStore);
_a.setEnabled(trustStore);
_b.setEnabled(trustStore);
_c.setEnabled(trustStore);
}
//Fire this when action performed
public void actionPerformed() {
boolean sel = _useSSL.isSelected();
boolean trust = _trustStore.isSelected();
actionPerformedForUseSSL(sel);
if(trust) {
actionPerformedForTrustStore(trust);
}
}
Add or remove or mix any fields with this structure easily.
You can use it just like you have used 'sel' in "actionPerfomed" method like this:
public void actionPerformed(ActionEvent e) {
boolean sel = _useSSL.isSelected();
_port.setUseSSL(sel);
_keystore.setEnabled(sel);
_passphrase.setEnabled(sel);
L_KEYSTORE.setEnabled(sel);
L_PASSPHRASE.setEnabled(sel);
boolean trus = _truststore.isSelected();
//Use trus for the other things
}
You can use the following piece of code:
public void actionPerformed(ActionEvent e) {
boolean sel = _useSSL.isSelected();
_port.setUseSSL(sel);
_keystore.setEnabled(sel);
_passphrase.setEnabled(sel);
L_KEYSTORE.setEnabled(sel);
L_PASSPHRASE.setEnabled(sel);
boolean selOther= _truststore.isSelected();
if(selOther){
//perform task if the _truststore is selected
}
}

How to set and get a private variable in javaFX?

I'm using the radioButton in javaFX to set a parameter.
public class SelectCOM extends Application {
private int comNum ;
public int getComNum() {
return comNum;
}
public void setComNum() {
launch();
}
#Override
public void start(Stage primaryStage) {
//......
//OK BUTTON
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
String str = tg.getSelectedToggle().toString();
int begin = str.indexOf("COM");
str = str.substring(begin+3, str.length()-1);
comNum = Integer.parseInt(str);
System.out.println(comNum);
primaryStage.close();
}
});
}
When I call setComNum, the variable comNum is changed to the number I want. But getComNum just return 0.
Here is my calling method:
SelectCOM selectCOM = new SelectCOM();
selectCOM.setComNum();//After clicking the OK BUTTON about 3s, a 0 printed.
int com = selectCOM.getComNum();
System.out.println(com);
The static launch() method in Application creates a new instance of your Application subclass, starts the JavaFX toolkit, and invokes start() on the instance it created. (The call to start() is made on the FX Application Thread.)
So you are setting the comNum value on the field in the instance created by the call to launch(), but you are calling getComNum() on the instance you created yourself (i.e. on a different object); hence you don't get the correct value.
Note also the launch() method, and consequently your setComNum() method, will not complete until the JavaFX Platform exits (by default this is when the user closes the last window).

Categories