This one may be easy for you. But I'm stuck and can't figure out an algorithm for doing that. I want to show a JTextField and change the text on the JButton to "Hide" if it's "Search". If
the text on the JButton is "Search" a JTextBox should appear and vice versa, if the text is "Hide" make the JTextField invisivle and change the text on JButton to "Search"
This is how I have done it:
private void switchBtnText(){
searchTxtField.setVisible(true);
btnSearch.setText("Hide");
if(btnSearch.getText().equals("Hide")){
btnSearch.setText("Search");
searchTxtField.setVisible(false);
}
}
If I comment the if section it works to show the JTextField. My problem is to go back to the default settings which is a JButton with "Search" as a text and with an invisible JTextField.
The method is then called in an ActionEvent. I've done this before, in C#, so I know I'm close.
Thank you in advance. The fastest and best answer will get upvoted and accepted.
This should work although I've not tested it.
//btn action
private void toggleVisible(){
String btnVal = btnSearch.getText();
if(btnVal.equals("Search")){
searchTxtField.setVisible(true); // or however you are showing search field
btnSearch.setText("Hide");
}else{
searchTxtField.setVisible(false);
btnSearch.setText("Search");
}
}
Take a look at your execution sequence....
setText to "Hide"
if text equals "Hide", change text to "Show"
Try changing the logic so you check the text first, then make decisions about what should be done...
If text equals "Hide", change text to "Show"
Else, change text to "Hide"
Related
I'm trying to create one search toolbar that appears inside the letters hidden stick. when I press the hidden letters that when the bar disappears and there's no hiding the letters that appear. I've tried to capture events keyPressed entering the search bar, but I do not really feel comfortable with that way. if anyone has a better way, I hope everyone helps do.cam Thanks!
photo illustration
Check out the Text Prompt.
You can control when the prompt disappears. You can also control the color and style of prompt.
The Text Prompt uses a DocumentListener and a FocusListener to determine when the prompt should be displayed.
You can use the default settings with a single line of code:
JTextField textField = new JTextField(10);
TextPrompt tp7 = new TextPrompt("First Name", textField);
Hi I have a problem about getting the text from the buttons in a JPanel. My program will have a JPanel and there are 4 buttons inside it. Each button will have a random integer shown as a text. I want my program to be able to get the key that is pressed from the keyboard, and check if that key is matched with any of the buttons' text (Something like the calculator).
If the key is matched with any buttons in the JPanel, it will print out that key and make that button disabled.
My code is something like:
private void formKeyPressed(java.awt.event.KeyEvent evt) {
Component[] comp = numpanel.getComponents();
for (int i = 0;i<comp.length;i++) {
if (comp[i] instanceof JButton) {
//check if it matches with any button's text
}
}
}
and I get an error when I try to write comp[i].getText() in order to check the key and the button's text. In my understanding, it says that comp[i] is a Component, which doesn't have the method getText(), am I understand it correctly ?
How can i fix it or are they any alternative ways to do this?
it says that comp[i] is a Component, which doesn't have the method getText(), am I understand it correctly ?
Yes.
How can i fix it or are they any alternative ways to do this?
If you know that comp[i] is a JButton, such as within the if statement you have where you checked that it is with instanceof, then you can cast it as a JButton, and use the getText() method.
.... = ((JButton)comp[i]).getText();
I have a JText area in my program contain a text, and I want to add more text to it, when a button is pressed but without deleting the text that it already has on it. for example
JTextArea Text = new JTextArea("including; ");
When a button is pressed, I have to add another text but without deleting the one it has already, like so;
Text.setText("including button1");
and when another button is pressed it should look like this
Text.setText("including; button1, button2");
What is the most effective way of doing this?
Use the append feature, like this
Text.append("button1,");
place this in the button / s and just write the name of the button
I have written two methods that take and return a string. One converts from English to Pig Latin and vice versa. I have two JTextFields (one English and the other Piglatin- for user input). Likewise, two JButtons: "To English" and "To Pig Latin". I am confused how to use action listeners. It looks like it is possible to just have one ActionListener execute everything you want "on-click", however I am confused about how it knows which button you pushed. I instinctively want to make two ActionListeners for each button, whereby when the button is clicked the appropriate code will be executed.
Could someone please show and/or explain to me how this all works?
Summary of how I understand everything so far:
Type English words into JTextField
Click "To Pig Latin" button
ActionListener somehow knows the "To Pig Latin" button is clicked and executes the toPigLatin() method with the JTextField text as a parameter. (Assuming JTextField input is automatically considered a String type, really have not got that far yet).
The String returned from that method is output to the Pig Latin JTextField for user to see.
Thanks everyone!
I found what I was looking for. Sorry I posted it on the wrong Forum and thanks for migrating it over here where it belongs...I meant to post it here! :) For anyone trying to figure out how to make it so each button executes a certain block of code when clicked use the following: [ActionEvent].getActionCommand().equals("[Name of Button]")
Here's my final solution if anyone is interested:
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("To Pig Latin")){
String english = English.getText();
PigLatin.setText(englishToPigLatin(english));
}
if (e.getActionCommand().equals("To English")){
String piglatin = PigLatin.getText();
English.setText(pigLatinToEnglish(piglatin));
}
String piggyCount = Integer.toString(pigCount);
countP.setText("Number of Successful Word Translations: English: "+piggyCount+" ");
String englishyCount = Integer.toString(englishCount);
countE.setText(String.format("Pig-Latin: "+englishyCount));
}
Thanks again everyone who tried to help me out!
There's a good tutorial on ActionListeners here. The easiest way to use the same listener for two buttons is to call e.getActionCommand() on the ActionEvent passed to the actionPerformed function. That will return a string with the label of the button, which you can then use in an if statement to perform the appropriate action.
I need to have a button whose text is underlined and the only way I could find to do this in Java was to use and tags, but when I do this, it causes the button to take up as much room as is left in the JToolBar even though the text is short and it should only take up a small amount of space. Here is how I create the Button.
String buttonText = new String("<html><u>Lesson Plans</u></html>");
JButton lessonButton = new JButton(buttonText);
toolBar.add(lessonButton);
If I remove the tags then it takes up the right amount of space but if I have them in there is takes up the entire toolBar. Anyone know what's going on?
You might be able to fix the problem by using:
button.setMaximumSize( button.getPreferredSize() );
Otherwise you should be able to just change the font to use an underlined font. Darryl's Visual Font Designer shows how to add attributes to a font.
You can overwrite the paintComponent method of your JButton, and write on it with any style and font.
You forgot the closing "" and wrote "" instead... This may be the reason for your problems.