Open JFileChooser on doubleclick of JTable - mouseevent

I have another question.
I want to open a JFileChooser window when I double click on a JTable.
My code so far :
productTable.addMouseListener(new MouseAdapter(){
public void dblclick(MouseEvent click){
if (click.getClickCount() == 2){
fileChooser.setVisible(true);
}
}
});
I have fileChooser declared as a new JFileChooser box, to clear up any confusion. I kind of understand ActionListeners but my understanding is really only limited to regular buttons. I read through mouse listeners, and the code above is my understanding.
How do I add the class dblclick on double click of the JTable?
Also, if there is an easier way to approach the problem, I would greatly appreciate some pointers.
Thanks in advance!

So I actually figured it out on my own... As I figured, it was a pretty simple solution.
I changed the above code to :
productTable.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent click){
productTableMouseClicked(click);
}
});
and then I added the handler at the bottom with my other action handlers :
private void productTableMouseClicked(MouseEvent click){
if (click.getClickCount() == 2){
fileChooser.showOpenDialog(fileChooser);
}
}
So that's that, I guess.

Related

AndroidTV RecyclerView: Highlight entry when using DPAD

This subject is driving my nuts. I've read virtually a hundred posts about it, but none of them reflects my situation. I'm using a plain simple RecyclerView in an app running on AndroidTV. To enable navigation, I've set
android:focusable="true"
Now, I can use DPAD to scroll inside the RecyclerView, nicely. My goal is to animate the highlighting of the currently focused item in the list. However, I can't seem to find any event which indicates a focus change.
I would very much appreciate a hint, how my code could be informed about a focus changed inside the list, programaticaly, and how to figure out which list items have gained/lost focus.
Thanks!
Finally, it turned out that the solution is fairly easy. I don't know, why it was hidden in front of my eyes. All I had to do was to view.setOnFocusChangeListener() inside onBindViewHolder like this:
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
...
view.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean b) {
highlightEntry(view, b);
}
});
...
}

How to modify a form in a background thread

This might be a simple question but I can't figure it out.
I have a form called in my main function:
void Main() {
Mem = new MemoryManager();
Console::WriteLine("Thread Started");
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
FinalSolution::ControlPanel form;
Thread^ cLoop = gcnew Thread(gcnew ThreadStart(loop));
cLoop->Start();
Application::Run(%form);
}
All I want to do is, if someone presses a key in general (not just when the program is in focus), it changes the background to a different color.
I have tried a few things but nothing has worked so far. Here is the loop and I have indicated where I want it to happen.
void loop() {
while (true) {
if (GetAsyncKeyState(key)) {
//Here
form.button->BackColor = System::Drawing::Color::ForestGreen;
}
}
}
Of course the issue is that this function doesn't know what form is, but I don't know how to tell it.
Ended up just putting the loop directly in the form header and that solved the problem.

Disable copy/paste on Xamarin forms input field i.e. Entry

I am working on disabling copy/paste option menus on xamarin forms Entry, I am able to disable copy option using IsPassword=true attribute but this attribute also converts the normal input field to password field, which is not a requirement.
<Entry IsPassword="true" Placeholder="Password" TextColor="Green" BackgroundColor="#2c3e50" />
Thanks in advance.
This has to do with how Forms functions. Using iOS as the example here, the CanPerform override referred to in the other answer's Bugzilla issue is using the UIMenuController as the withSender and not the UITextField itself that might otherwise be expected. This is because the EntryRenderer class is a ViewRenderer<TView, TNativeView> type and subsequently is using whatever TNativeView (in this case, the UITextView) has in its CanPerform. Because nothing is going to be overridden by default, one still sees all of the cut/copy/paste options in the UIMenuController.
As a result, there would be a couple options. You could first make the modification where if you don't want copy/paste but are fine with getting rid of everything else, you can use UIMenuController.SharedMenuController.SetMenuVisible(false, false) in a custom renderer inheriting from EntryRenderer. If you look around on SO, there are similar questions where this is a possible route.
Alternatively, you can create a "true" custom renderer inheriting from ViewRenderer<TView, TNativeView> as ViewRenderer<Entry, YourNoCopyPasteUITextFieldClassName>. The class inheriting from UITextField can then override CanPerform as something like follows:
public override bool CanPerform(Selector action, NSObject withSender)
{
if(action.Name == "paste:" || action.Name == "copy:" || action.Name == "cut:")
return false;
return base.CanPerform(action, withSender);
}
This will require more effort because the custom renderer will not have the same behavior as the EntryRenderer, but as Xamarin.Forms is now open source, you could look to it for some ideas as to how the EntryRenderer functions normally. Something similar would likely have to be done for Android.
Edit: For Android, you can probably use this SO answer as a starting point: How to disable copy/paste from/to EditText
Another custom renderer, this time inheriting from ViewRenderer<Entry, EditText>, and create a class inside of it like this (in the most basic form):
class Callback : Java.Lang.Object, ActionMode.ICallback
{
public bool OnActionItemClicked(ActionMode mode, IMenuItem item)
{
return false;
}
public bool OnCreateActionMode(ActionMode mode, IMenu menu)
{
return false;
}
public void OnDestroyActionMode(ActionMode mode)
{
}
public bool OnPrepareActionMode(ActionMode mode, IMenu menu)
{
return false;
}
}
Then, in your OnElementChanged method, you can set the native control and the CustomSelectionActionModeCallback value:
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
if (Control != null)
{
Control.CustomSelectionActionModeCallback = new Callback();
}
}
Doing something like the following appears to disable all of the copy/paste/cut functionality on the custom entry as far as the toolbar goes. However, you can still long click to show the paste button, to which I've poked around a bit hadn't found an answer yet beyond setting LongClickable to false. If I do find anything else in that regard, I'd make sure to update this.

Animation not starting until UI updates or touch event

I have a strange problem with an AlphaAnimation. It is supposed to run repeatedly when an AsyncTask handler is called.
However, the first time the handler is called in the Activity, the animation won't start unless I touch the screen or if the UI is updated (by pressing the phone's menu button for example).
The strange part is that once the animation has run at least once, it will start without problem if the handler is called again.
Here's what the code looks like:
// AsyncTask handler
public void onNetworkEvent()
{
this.runOnUiThread(new Runnable() {
#Override
public void run()
{
flashScreen(Animation.INFINITE);
}
});
}
// Called method
private void flashScreen(int repeatCount)
{
final View flashView = this.findViewById(R.id.mainMenuFlashView);
AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
alphaAnimation.setRepeatCount(repeatCount);
alphaAnimation.setRepeatMode(Animation.RESTART);
alphaAnimation.setDuration(300);
alphaAnimation.setInterpolator(new DecelerateInterpolator());
alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation)
{
flashView.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationEnd(Animation animation)
{
flashView.setVisibility(View.GONE);
}
#Override
public void onAnimationRepeat(Animation animation) { }
});
flashView.startAnimation(alphaAnimation);
}
I have noticed that runOnUIThread isn't necessary (same results occur if I don't use it), but I prefer keeping it as I'm not on the UI thread.
Any ideas on what could cause this?
A little more research showed that my problem was the same a this question:
Layout animation not working on first run
The flashView's visibility was set to GONE by default (causing the Animation not to start immediately as the View had never been rendered), so I just need to set it to INVISIBLE before calling flashView.startAnimation()
If setting the View to VISIBLE won't work, as was in my case, it helped for me to call requestLayout() before starting the Animation, like so:
Animation an = new Animation() {
...
view.requestLayout();
view.startAnimation(an);
In my case, my View was 0dip high which prevented onAnimationStart from being called, this helped me around that problem.
This worked for me:
view.setVisibility(View.VISIBLE);
view.startAnimation(animation);
I had to set the view to VISIBLE (not INVISIBLE, neither GONE), causing the view renderization needed to animate it.
That's not an easy one. Till you got a real answer : The animation start is triggered by onNetworkEvent. As we don't know the rest of the code, you should look there, try to change onNetworkEvent by an other event that you can easily identify, just to debug if the rest of the code is ok or if it's just the trigger that is responsible for it.
May be it will help someone, because previous answers not helped me.
My animation was changing height of view (from 0 to it's real height and back) on click - expand and collapse animations.
Nothing worked until i added listener and set visibility to GONE, when animation ends:
collapseAnim.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
view.setVisibility(View.GONE);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
And when expand just set it to VISIBLE before animation:
view.setVisibility(View.VISIBLE);
view.startAnimation(expandAnim);

How to execute a binding for a metro control

I want to write the contents of a per occasion active TextBox back to the bound property of the ViewModel when the user presses the key combination for save (Ctrl-S).
My Problem with it is, that I'm not able to trigger the execution of the binding so that the bound Text-Property reflects the contents of the TextBox.
-There seems to be no GetBinding-method. Therefore I can not get the Binding and execute it manualy.
-There is no Validate-method such as in WinForms which executes the Binding
-Giving focus to another control from within KeyDown seems not to work, the binding does not execute
How can I achieve this?
Take a look at Aaron's discussion about this in his WiredPrarie blog post : http://www.wiredprairie.us/blog/index.php/archives/1701
I think I understand your question better now. One way around this would be to use a sub-classed textbox with a new property like this from here:
public class BindableTextBox : TextBox
{
public string BindableText
{
get { return (string)GetValue(BindableTextProperty); }
set { SetValue(BindableTextProperty, value); }
}
// Using a DependencyProperty as the backing store for BindableText. This enables animation, styling, binding, etc...
public static readonly DependencyProperty BindableTextProperty =
DependencyProperty.Register("BindableText", typeof(string), typeof(BindableTextBox), new PropertyMetadata("", OnBindableTextChanged));
private static void OnBindableTextChanged(DependencyObject sender, DependencyPropertyChangedEventArgs eventArgs)
{
((BindableTextBox)sender).OnBindableTextChanged((string)eventArgs.OldValue, (string)eventArgs.NewValue);
}
public BindableTextBox()
{
TextChanged += BindableTextBox_TextChanged;
}
private void OnBindableTextChanged(string oldValue, string newValue)
{
Text = newValue ? ? string.Empty; // null is not allowed as value!
}
private void BindableTextBox_TextChanged(object sender, TextChangedEventArgs e)
{
BindableText = Text;
}
}
Then bind to the BindableText property.
Solution for command-instances
Here a solution I have found which is relatively leightweight, but also a bit "hackish":
btn.Focus(Windows.UI.Xaml.FocusState.Programmatic);
Dispatcher.ProcessEvent(CoreProcessEventsOption.ProcessAllIfPresent);
btn.Command.Execute(null);
First I give the focus to another control (In my case the button which has the bound command). Then I give the system time to execute the bindings and in the end I raise the command which is bound to the button.
Solution without bound commands
Give the Focus to another control and call the Dispatcher.ProcessEvent(...).
anotherControl.Focus(Windows.UI.Xaml.FocusState.Programmatic);
Dispatcher.ProcessEvent(CoreProcessEventsOption.ProcessAllIfPresent);
// Do your action here, the bound Text-property (or every other bound property) is now ready, binding has been executed
Please see also the solution of BStateham.
It's another way to solve the problem