This question already has an answer here:
Output not getting redirecting to proper Jtextarea
(1 answer)
Closed 6 years ago.
I'm trying to make a Heads or Tails game in Java at the moment and I'm struggling with getting the output text into my text area.
What I have is at the moment is:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
if (Math.random() < 0.5)
System.out.println("Heads");
else
System.out.println("Tails");
jTextArea1.update(jTextArea1.getGraphics());
}
I'm trying to make sure when the button is pressed it displays but to no avail. It's been bugging me for a bit.
System.out doesn't go to any text area. It's the standard out (although you could redirect it, but let's not get into that here). Use jTextArea1.setText("Heads"); to update the textarea contents. For appending you'll need to get the existing text first.
Related
This question already has answers here:
How to use PauseTransition method in JavaFX?
(2 answers)
JavaFX periodic background task
(6 answers)
Closed 3 months ago.
I am a beginner Java programmer and have a question and really appreciate if you can help me. I use Netbeans and in default JavaFX fxml Application, there is a button that prints "Hello world" when it is pressed.
#FXML
private void handleButtonAction(ActionEvent event) {
System.out.println("You clicked me!");
label.setText("Hello World!");
}
Is there a way to change the color of button (using button.setStyle() ), exactly 5 seconds after it is first pressed? Meantime, I want the button remains functional during that 5 seconds and prints "Hello world" when it is pressed.
I tried use sleep() function but then the button would not be functional during that period. Is there is an alternative way to code this?
This question already has answers here:
Java - placeholder on textfield
(3 answers)
java swing JTextField set PlaceHolder [duplicate]
(4 answers)
Closed 7 months ago.
What I'm trying to do.
I'm basically trying to add a search bar to my program, made out of a JTextArea.
What I would like to do, is add an "example" into the search bar (greyed out text, which disapears when the JTextArea is activated). I've attempted this by changing the text colour, and adding a mouseListener to setText("");. However, this means that every time I click on the bar, it empties, which isn't something I want. (Say I typed in a really long string and had to retype it because it was 1 character wrong).
JTextField searchBar = new JTextField("Search...");
searchBar.setForeground("Color.GRAY");
searchBar.addMouseListener(new MouseListener() {
#Override
public void mouseClicked(MouseEvent e) {
searchBar.setForeground(Color.BLACK);
searchBar.setText("");
}
}
What I'd like to know
How can I make it so that, each time the frame is repainted, the search bar will display search... in grey, until clicked, after which it remains persistent.
I hope it's clear why I'm trying to do, any help is appreciated.
Thanks.
This question already has answers here:
How to get X and Y index of element inside GridLayout?
(7 answers)
JButtons inside JPanels with a GridLayout JFrame
(3 answers)
JButton array ActionListener
(4 answers)
Closed 11 months ago.
This post was edited and submitted for review 11 months ago and failed to reopen the post:
Original close reason(s) were not resolved
How would I go about creating lots of JButtons and JLables without having to declare a long list of them , I'm not sure how to do it if I made a for loop which said e.g. "for (int I = 0; I < 30; I++) { JButton button = new JButton(); }
Although this would create many buttons I want to be able to individually edit each button later on so how would I do this loop of buttons but them all having different names.
Any help is very much appreciated :)
I would like to have a customized InputDialog so that following code could work. I am creating an old fashion application. The InputDialog will be created once users click a button on a view. I am wondering if we could create a customized input dialog to have the feature that my title says. Just let me know if anything is unclear.
String s = MyCustomInputDialog("my question");
// show the input dialog that will be either closed in 5 seconds or closed by the user's action
System.out.println(s) // print null or print user's input
Check answers over in Closing A JOptionPane Programmatically , which has a really interesting hack to loop through all Windows to find the JOptionPane and kill it.
This question already has an answer here:
How to clear the JTextField by clicking JButton [duplicate]
(1 answer)
Closed 6 years ago.
I am writing a program and I want the JTextfield to be blank after the input has been given.
For example; user inputs a number, he presses the add button and the textfield should be blank again and should not contain the number. I tried using null but it doesnt work. If you need the code tell me, I will edit it.
you can just do
textfieldname.setText("");