Need to add text to JFrame - java

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/

Related

set hidden text into JTextfield in java

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);

suggestions on designing a JFrame

I am designing a Jframe using netbeans. I do have few questions.
Can we create a label for a field in a desired location(For eg.,we have a field named height, I need to display a label below it indicating height is in cm) conditionally?
Can we disable a field based on a condition?(by disable I mean it shouldn't be displayed in my frame)
Can someone suggest me whether we can achieve them through some examples.
Tried this, after some helpful suggestions
private void englishRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
JLabel userlabel;
if (englishRadioButton.isSelected())
{
userlabel = new JLabel("Inches");
userlabel.setBounds(311, 59, 64, 36);
//userlabel.setLocation(307,55);
//userlabel.setSize(70,40);
userlabel.setForeground(Color.green);
userlabel.setText("Inches");
userlabel.setVisible(true);
System.out.println(englishRadioButton.getBounds());
inchesTextField.setVisible(true);
}
}
The textfield is visible only when I click the English radio button,at the same time I need to get a label but it's not displayed with the above code. Can I know where I am going wrong?
Please see the attached screenshots
When English button is clicked, I need a label beneath the second textfield as inches, I am disabling the text field when Metric is displayed. I am able to achieve the later one but not the former one
Thanks!!
Yes, relative placement of components is easily achieved with use of layout managers.
Yes, all components have a setEnabled(...) and a setVisible(...) method either of which can be called at any time during a program's run. The former helps you activate/inactivate components and the latter helps make them visible/invisible. If you want to swap complete "views", use a CardLayout.
Regarding:
Can someone suggest me whether we can achieve them through some examples.
Please, you first as I strongly believe that the onus of effort here should be yours, the questioner's, since you're the one asking the questions, and the one with the most to learn by coding as much as possible. Let's see your attempts and we can help you with them. Otherwise the best examples are to be found at the Swing Tutorials.
For links, please look here: Swing Tag Info.
Edit
You ask:
I tried the above posted code,conditionally disabling the text field works well but getting a label doesn't work. Can you please suggest on that?
I don't see you adding your JLabel to any component. If you are going to create a component on an event, you must add it to a component whose ancestor hierarchy eventually reaches a visible top-level component such as a JFrame. Then after adding a component to a container (say a JFrame), you must call revalidate() on the container to have its layout managers re-layout its components, and then repaint() to repaint any "dirty" pixels.
I again will re-iterate that you're far better off not using null layout and absolute positioning, but rather using layout managers and relative positioning. If you want a label with and without visible text, it's often best to add an empty JLabel to the GUI on GUI creation, and just set its text when needed, as long as the label is located somewhere that allows its text to shrink and expand.
Also, as to your current problem, you might wish to show a picture of what you're trying to achieve, and what you're getting. Or if you can't post a picture here yet, post a link to an image or images you've created, and then we'll post it for you.

Temporary grayed out text in textbox

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/

JLabel should not be able to receive focus.. but it does?

I'm working on a Swing application that uses the default Swing methods for handling focus. Focus isn't working as I'd expect.
In one case, I have a JTextField that I call .requestFocusInWindow() When the window is displayed a JLabel has focus instead
The Java 6 docs for JLabel say "As a result, it cannot get the keyboard focus." http://docs.oracle.com/javase/6/docs/api/javax/swing/JLabel.html
However, I have a sample application that shows a JLabel receiving focus and KeyboardFocusManager.getFocusOwner() returns that component. (http://github.com/akinsgre/swingStarter)
The code the the class is https://raw.github.com/akinsgre/swingStarter/master/src/main/java/test/HelloWorldSwing.java
Can anyone help me understand or explain what I'm missing?
I think you need to associate the label with the text field. So try using the setLabelFor method and see if that helps.

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.

Categories

Resources