Temporary grayed out text in textbox - java

I would like to show a text like "write your message here." that automatically disappears after the textbox gets the focus?
I was trying to do this without having to deal with listeners, with an initial text for example.

Try the example. I think that's what you need
http://tips4java.wordpress.com/2009/11/29/text-prompt/

SwingX contains a class "PromptSupport" that does exactly what you need.
It's very easy to handle:
JTextField tf = new JTextField(5);
PromptSupport.setPrompt("A Prompt", tf);
Take a look here:
http://weblogs.java.net/blog/kschaefe/archive/2010/07/15/swingx-using-promptsupport
You can download the jar-file here:
http://java.net/downloads/swingx/releases/

Related

Need to add text to JFrame

So I'm trying to create this really basic calculator that can calculate volume and area. I already have formulas and everything worked out, I just need to get the actual window and text to work. My code just to create the window is here:
JFrame myCalc = new JFrame("Area/Volume Calculator");
JTextField input;
myCalc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myCalc.setSize(400, 500);
myCalc.setVisible(true);
JLabel welcome = new JLabel("");
welcome.setText("Please choose which shape's area/volume you would like to calculate.");
All I need to know is why the JLabel "welcome" isn't actually showing up in the window. The actual window does appear, with the correct title, but there is no text. Keep in mind that I am a beginner at Java and am really only beginning to use Swing.
Thank you!
P.S. I also have no idea how to use the text editor. I understand that the code is badly formatted and that it appears to be missing a class and main method, but it wouldn't let me copy/paste that in.
You will need to add welcome to myCalc.
Try this:
JLabel welcome = new JLabel("Please choose which shape's area/volume you would like to calculate.");
myCalc.add(welcome);
myCalc.pack();
myCalc.setVisible(true);
1. Put this to the end of the your code. Now you will just have to style components in JFrame.
myCalc.setSize(400, 500); //or just myCalc.pack();
myCalc.getContentPane().add(input);
myCalc.getContentPane().add(welcome);
myCalc.setVisible(true);
2. Here you can find some examples, which will help you get stared with java swing. Here is the link:https://docs.oracle.com/javase/tutorial/uiswing/examples/components/

Switching the text on a JButton

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"

jTextfield display problem in java

I have made a frame in which i have put two jTextfield boxes where the user can see the path of the loaded file. Problem is that if the path is too long , the textfield expands to accomodate the full path which again leads to display problems. I would like to keep the textfield's length constant and instead , display the full path of file as a tooltip instead.
How can this be done?
Code for layout manager of jinternal Frame:
javax.swing.GroupLayout jInternalFrame1Layout = new javax.swing.GroupLayout(jInternalFrame1.getContentPane());
jInternalFrame1.getContentPane().setLayout(jInternalFrame1Layout);
I never use an IDE so I don't know how the GroupLayout works.
But when using the other layout managers I always use:
JTextField textField = new JTextField(10); // or whatever size your want
This will give the text field a preferred size and the layout manager can use that information when laying out the component.
Code the GUI by hand instead. You will avoid problems like this and it will be much easier to make changes to your code.
you need to choose a layout manager to manage the proportions of your JComponents.
Try to put your textfiels on a JPanel so you can select a layout useful for you
Later you can use JTextField. setToolTip("full path") to set a tool tip
I solved my problem:
Anybody having the same problem can set the Property Columns using Netbeans. The default is 0, so the textfield cannot accomodate the full text. Use some value like 3 to achieve it.

Using <html><u> tags to make button text underlined, caused button to take up entire JToolBar

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.

Java Swing, textarea as input/output?

I want to load up a window using the java swing, something like this
However I want to be able to use it for text input also not just to dump data into it..
any idea how to do it?
JTextAreas are editable by default, so input is trivial. Just put one into a test UI and see for yourself.
From a design perspective, using the same JTextArea for both input and output strikes me as unwise. The only example I can think of is a shell interface, and that imposes stronger limits on how input and output works than a JTextArea will provide out of the box.
I'm not sure if I got the point of your question, but you can get the text the user has typed in using the getText() method:
JTextArea ta = new JTextArea();
String input = ta.getText();
Check out this Sun toturial:
http://java.sun.com/docs/books/tutorial/uiswing/components/textarea.html
In particular, scroll down to the "TextAreaDemo" example.

Categories

Resources