Display of Java Component is delayed [duplicate] - java

This question already has answers here:
Will Runnables block the UI thread?
(4 answers)
How to update swing GUI from inside a long method?
(3 answers)
Closed 3 years ago.
I'm facing a strange behavior from the Java Runtime(VM) when I'm trying to display a label or progress bar on the screen when something happens.
The scenario is like this:
the user will press a button to do a certain action
this action is a kind of process on the DB which could take some seconds
I'm displaying a message in a Jlabel field which is already in the form but set to be invisible when the form opens.
Then when the user presses a button I'm setting this label to be visible and do some processing and then set it back again invisible as it was, which is very simple logic.
problem is the Jlable is displayed after the processing is done and not before.
The same problem with happens also a progress bar.
Any explanation why is this happening?
here a sample of the code
Mylable.setVisible(false); defaullt status when the form opens
Event Occurend (user clicked a button)
Mylable.setVisible(true);
....... do some process here
Mylable.setVisible(false);

Related

how to set a listener for the selected text in TextView in java? [duplicate]

This question already has answers here:
How to set up a listener on the selected text in TextView
(3 answers)
Closed 1 year ago.
i want a Help just which way i follow to get a listener for the selected text in TextView,
what i want is when the user select a specified text, a button of options will appear.
i hope you help me
Have you tried ActionListener? It will be called when user interacts or performs any action on that particular component. You can get the selected content by component.getSelectedText() or similar method. Do not write separate listeners for mouse and keyboard operations as it might interfere with one another and cause problems.

OutputStream JTextArea Thread.Sleep Causes Freezing [duplicate]

This question already has answers here:
java thread.sleep puts swing ui to sleep too
(3 answers)
Swing - Thread.sleep() stop JTextField.setText() working [duplicate]
(3 answers)
Closed 4 years ago.
I'm working on a game project where I'm creating a GUI using JSwing. It looks something like this:
A lonely 'F'
I'm trying to create a 'typewriter' effect, and so have a Thread.sleep(100) in my custom PrintUtils method (let's call it printToGui). I've routed System.out to the TextArea in the center of the GUI, and the first time I run the program the 'typewriter' effect works great and the startup text I send through printToGui shows up as it should. However, whenever I click a button (which triggers more text getting sent through printToGui), the app freezes, and won't unfreeze until the length of time it would have taken to print with Thread.sleep() finishes, spitting out all the text at once.
What I've figured out is that the first time I start the app, Thread.sleep() is happening on the "main" thread, while every time I click a button it's happening on the AWT-EventQueue-0 thread. I assume this is the problem, but maybe it's not; how do I get future output to the GUI to happen on the "main" thread and not the new one? Thanks!

Is there a Java method to determine if a JButton is clicked? [duplicate]

This question already has answers here:
How can I check that JButton is pressed? If the isEnable() is not work?
(6 answers)
Closed 7 years ago.
I am creating a basic calculator GUI and I have created JHoverButtons and a text field that only allows integer inputs. Now what I am trying to do is have it so that if for example, the hover button that says "1" on it is clicked by the user, then the text field would display "1" just as the user has done so by clicking the button.
So I have created if-statements to do as I had detailed above and this is what I had written for that:
if (hoverOne.isSelected()){
integerInput.setText("1");
}
Turns out the isSelected method is for toggle buttons with which my hover buttons are not so when I run the file, it doesn't do what I wanted. I named the text field in the GUI as integerInput and the hover button with "1" on it as hoverOne.
How can I get it so that when a user clicks the button, the text field displays the number the user entered?
You can try to use isPressed() method to check if the JButton is pressed:
if(jButton1.getModel().isPressed())
{
//code
}

How to write a code for default close button in JFrame? [duplicate]

This question already has answers here:
setDefaultCloseOperation to show a JFrame instead
(3 answers)
Closed 8 years ago.
I want to write a code to default close button in JFrame. I just want to ask whether "to save the content or not?" using another JDialog or JOptionPane when click on close button.
Where do I have to write the code? As default close button is not in design of JFrame, and no such event found for JFrame window too, how can I add event listener to that button?
Check out Closing an Application for a couple of solutions:
using a WindowListener and overriding the windowClosing(...) event to display your option pane.
using a simple API so you only need to provide a message or write an ActionListener for more complicated processing.

What is the differences between focused and pressed on android Button? [duplicate]

This question already has answers here:
android: what is the difference between focused, enabled, pressed, and selected states?
(2 answers)
Closed 7 years ago.
I found a tutorial to create android custom button :
http://developer.android.com/resources/tutorials/views/hello-formstuff.html#CustomButton
What is the differences between pressed and focused
And please tell me a focused condition on this button. because I couldn't find any condition which can make this button turned orange.
This question has already been answered here. Please thoroughly search stackoverflow, before posting a question: android: what is the difference between focused, enabled, pressed, and selected states?
I am basically pasting the answer here for your convenience.
"Enabled -> User Interaction possible.
Disabled -> User interaction not possible.
if you hover the mouse over a widget, it is focussed
If you make a press-down (half click) on that widget, it is pressed
If you press-down and press-up while the mouse is at the same position, it is selected"
You can focus buttons without clicking them. This was a bug in a application i developed recently. I set them to be focusable: then the first click on them was just focusing the user on the button and the second was actually triggering the onClick event. This is not so good user experience, but I imagine there might be cases where this is useful.

Categories

Resources