JavaFX Toggle Group Change - events

i have a lot of Toggle Buttons! They are all in one toggle group, now i want to add a change listener! If there is one event one a toggle button in this group i want to have a event on the other buttons too!
How can i check that?
I made something like this, but it doesn't work very well:
triangleGroup.selectedToggleProperty().addListener(new ChangeListener<Toggle>()
{
#Override
public void changed(ObservableValue<? extends Toggle> arg0, Toggle arg1, Toggle arg2)
{
if (btnTriangle.getRotate() != iRotateCoord1)
{
closeTriangle();
}
}
});
So the triangleGroup represents my ToggleGroup! There i add a ChangeListener.... If there ia a Event, the current btnTriangle ( ToggleButton ) will close if it has not the rotate value like iRotate...
How can I get the Event when ever there is a Event on this Button Group or when ever there is a change...
Or is there a possibility to get all of the buttons that are in the Group?

Related

Disable SpreadsheetView cell selection via mouse/key events, but retain ability to scroll the view

I'm writing an Excel import wizard which loads a user's spreadsheet into a ControlsFX SpreadsheetView. The user can then identify the relevant columns (i.e., first and last name, etc.) to be imported into the app via a set of combo boxes. Each combo box contains a list of the available columns. When the user makes a choice in a combo box, the respective column is added to the selection model's range of selections.
Column selection should only be done via the combo boxes, and user clicks on cells in the spreadsheet view should be ignored (i.e, not change the selection model contents).
However, I cannot find a way to disable mouse clicks on cells or column headers. The solution to a related question about how to disable clicks on TableView doesn't provide a satisfactory answer: setMouseTransparent(true) essentially disables the entire view.
In my case, the user must be able to scroll the view in order to find relevant columns, since not all users of the app have structured their spreadsheet in the same column order and the column may not be visible without scrolling horizontally.
So to re-iterate: I need to consume mouse clicks on cells and column headers to prevent changing the column selections made using the combo boxes. How do I do this? (FYI: consuming mouse events on the spreadsheet view [setOnMousePressed|Released|Clicked(mEvent -> mEvent.consume()] has no effect.)
Actually, when you use the setOnMousePressed methods, you are adding an EventHandler.
EventHandler are happening in the later chain of bubbling event.
What you want is to add an EventFilter that will catch the event before the SpreadsheetView has it.
For example, I've made this :
spreadSheetView.addEventFilter(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
event.consume();
}
});
spreadSheetView.addEventFilter(MouseEvent.MOUSE_PRESSED, new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
event.consume();
}
});
spreadSheetView.addEventFilter(MouseEvent.MOUSE_RELEASED, new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
event.consume();
}
});
And it's working. Although I really don't know the side effect of such a manipulation..

onPrepareOptionsMenu acts weird if API is called from inside

I have a need to enable and disable menu items based on the API call response and it has to be called everytime the menu shows up.
I need asynctask because I have to show progressbar
#Override
public boolean onPrepareOptionsMenu (Menu menu) {
handleMenuItems(menu)
}
private void handleMenuItems(menu)
{
new AsyncTask<Void, Void, Void>(){
#Override
protected Void doInBackground(Void... params) {
//API call
return null;
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (progressDialog != null)
progressDialog.hide();
//necessary menu items are enabled and disabled
super.onPrepareOptionsMenu(menu);
}
#Override
protected void onPreExecute() {
if (progressDialog != null){
progressDialog.setMessage("Checking");
progressDialog.show();
}
super.onPreExecute();
}
}.execute();
}
Whenever I touch the options menu the onPrepareOptionsMenu gets called and the options menu doesn't appear then when I press it again the onPrepareOptionsMenu doesn't get called and the options menu appears.
I want the api to get called every time and menu to be shown whenever I touch for options menu.
You shouldn't show and hide progress dialog in OnPrepareOptionsMenu
Comment out the progress dialog codings and rest of the codes are ok and works as you expected
Note:
You have called super.OnPrepareOptionsMenu in OnPostExecute(UI
Thread)
As the progress bar is not needed don't remove the AsyncTask
otherwise app my crash
I don't think this code does what you expect it to do. When onPrepareOptionsMenu is called the first time, it only schedules the AsyncTask to run at a future time and not right away. Next it executes the super.onPrepareOptionsMenu line which should show whatever the option menu is set to be at that time. I think (have to check on that) the UI will then wait for the user input as it is showing some sort of a menu (maybe not what you had expected). When the menu is touched for the second time, I think it will dismiss the menu and that might be the time that the AsyncTask is executed which results in the first menu that you had expected in the first place. The second instance of the AsyncTask will not run until the first one finishes (unless executeOnExecutor is called instead of execute, but that is not the case here nor I believe it to help the expected outcome.)
You might want to have a "dummy" menu prepared (maybe something that shows "waiting...") and start a background thread where it builds up the right menu and then when it is ready update and display a new menu (programmatically).
I personally would advise in finding a way to build and show the menu on the UI thread (maybe prep the menu as the changes are made, ahead of time, and just show it when needed.
Kaamel

How to scroll JavaFX TextArea after setText

I am using JavaFX and I want to scroll a textarea to a certain position from the top after I have set the text of the textarea first.
I have tried to first set a listener on my textarea and inside it I use setText:
textArea.textProperty().addListener(new ChangeListener<String>() {
#Override
public void changed(final ObservableValue<? extends String> observable, final String oldValue, final String newValue) {
textArea.setScrollTop(<Here I use the scroll position that I want>);
}
});
Then I try to set the text of the textarea by setText. But this does not scroll my textarea. I should add that I am loading in book long strings, but they load rather quickly. I have also tried to use a delay of 1 second inside the listener after I set the text and then scroll the textarea, and this turns out to work for me which might suggest that I use the wrong listener. I really want to be able to use a listener for this and not a delay.
All help is welcome!
What will happen if you use this code ?
textarea.addEventHandler(KeyEvent.KEY_TYPED, new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent t) {
//textArea.setScrollTop(<Here I use the scroll position that I want>);
}
}
You should put the code of the listener in a separate Thread to make the sleep and then call the textArea.setScrollTop using Platform.runLater, in this way the sleep doesn't block the Platform Thread. If you don't the Platform Thread is blocked by the sleep and any pending operation on the TextArea will be blocked as well, if any of the pending operation is preventing the scrolling it will not finish and your call will be voided.

Make a difference between a tab event and a flick event on listbox

I have a listbox which contains a list of images; I don't know how to differentiate betwwen a Flick event and a Tap event, to make a zoom on the chosen image?
There is a Tap event on all elements (in Mango). Tap event don't raised when user scrolls a list.
Also, you can place an image inside a retemplated Button (leave only content holder). Then you get for free Click event and Tilt Effect as well
There is additional support for detecting touch in the XNA library. Trying adding the Microsoft.Xna.Framework.Input.Touch reference to your project
Include the following using statement:
using Microsoft.Xna.Framework.Input.Touch;
Subscribe to the required events in your constructor as follows:
TouchPanel.EnabledGestures = GestureType.Tap | GestureType.Flick;
On your list box create an event for Manipulation Completed as follows:
ManipulationCompleted="ListBoxDays_ManipulationCompleted"
You could add code to the that event method to track the type of events that have been completed with the following code:
private void ListBoxDays_ManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
{
while (TouchPanel.IsGestureAvailable)
{
GestureSample gesture = TouchPanel.ReadGesture();
if (gesture.GestureType == GestureType.Tap)
{
//Do something
}
if (gesture.GestureType == GestureType.Flick)
{
//Do something else
}
}
}
Hope this Helps

Group of buttons?

I have a question - is there any way to define a few buttons as a 1 group, and make 1 animation that will start for this group, instead of making an separate animation start for each of them?
Put them all in a ViewGroup and animate that. All children will be part of the animation. A ViewGroup is the base class for any layout class, that means you can use a LinearLayout, RelativeLayout or anything else that you find suitable.
You can try to set to all buttons same onTouchListener, like:
button1.setOnTouchListener(this);
button2.setOnTouchListener(this);
button3.setOnTouchListener(this);
Of course your app needs to implement onTouchListener, and then you can write your onTouchListener:
public void onClick(View v) {
// write your code what you want your buttons to do
// You can also write some different codes for buttons by using id
if(v.getId() == button1.getId())
{
//do something when button1 clicked...
}
}

Resources