I have a JDialog and inside it I have a JPanel that uses FlowLayout
now I've created 3 labels and text fields using the Netbeans GUI Builder, and I want to add 2 more text fields using code
I've adjusted the size of the panel so that when I add a new label and a textfield with a preferred size the new set of label - textfield will be under the previous set
somewhere in the JDialog I do something like this
JLabel cores = new JLabel("Cores");
cores.setPreferredSize(new Dimension(70,15));
first = new JTextField();
first.setPreferredSize(new Dimension(140,20));
JLabel power = new JLabel("Power");
power.setPreferredSize(new Dimension(70,15));
second = new JTextField();
second.setPreferredSize(new Dimension(140,20));
panel2.add(cores);panel2.add(first);panel2.add(power);panel2.add(second);
when I compile the program, the labels don't show up and neither do the textfields
when I go down and click I have the following result
http://img684.imageshack.us/img684/13/unledlpy.png
if I type something, the text field appears
http://img5.imageshack.us/img5/6796/unledhig.png
the labels don't appear though, I don't think I made any changes to the properties, any help would be appreciated thanks
Define the no of columns while creating object.
like this
JTextField jt=new JTextField(20);
Related
I want to create three JTextArea in my swing application.
Each JTextArea has a different size.
The first JTextArea should have 8 columns
The second one should only have 1 column
And the last one should have 50 columns.
My initial problem is that:
Whenever I type something, the JTextArea will keep on re-sizing its width.
This has been fixed by JScrollPane, setLineWrap(true), and setWrapStyleWord(true).
So here's my problem.
Whenever I add setLineWrap() to a JTextArea, the JTextArea will be resized.
My first and second JTextArea have been resized to 12 columns.
I searched and found some solution but they use MigLayout.
Is there any way to add word and line wrap in JTextArea without resizing it (and ofcourse, without the use of MigLayout)?
What's the easiest way to set the columns of JTextArea with word and line wrap?
What's the easiest way to set the columns of JTextArea with word and line wrap?
You create the JTextArea with code like:
JTextArea textArea = new JTextArea(5, 50);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
JScrollPane scrollPane = new JScrollPane( textArea );
JPanel panel = new JPanel();
panel.add(scrollPane);
frame.add(panel, BorderLayout.PAGE_START);
By default a JPanel uses a FlowLayout which respects the size of any component added to it. The BorderLayout.PAGE_START will repect the height of any component added to it.
Scrollbars will appear as required what text is added to the text area. So the key is to use a layout manager (or combination of layout managers) that meet your requirement.
If I apply,
text.setOptions(TextField.MULTILINE | TextField.REQUIRED);
to my text field, then I can enter more data, but the problem is the data is getting too small, then it becomes scrollable.
Can we get a scrollbar instead, before it gets too small?
I think you should use textarea instead of textfield as you want multiple lines. And then you need to put textArea to ScrollPane to get scrollbar.
JTextArea textArea = new JTextArea(10, 30);
ScrollPane scrollPane = new JScrollPane(textArea);
I created a form. Actually it is 10 JLabels with each JLabel having a text field next to it.
consider,
JLabel_called_Name JTextField_to_obtain_name
JLabel_called_Phone JTextField_to_obtain_phone_number
and so on..
I usually position this in a JPanel and display it in a frame. But my panel and frame have height smaller than the size required to hold 10 of these Labels and Textfields.
So I wish to add them to a JScrollPane.
But in every question I only obtained information of how to add Jlabels to a scroll pane using a Box,
or adding JLabels to a JList.
However I would like to represent it in the format I showed above. A Jlabel beside a JTextField.
How can one acheive this?
But in every question I only obtained information of how to add Jlabels to a scroll pane using a Box, or adding JLabels to a JList.
You can add any component to a JScrollPane:
JPanel = new JPanel();
panel.add( label1 );
panel.add( textField1 );
JScrollPane scrollPane = new JScrollPane( panel );
The trick is choosing the correct layout manager for you panel. Read the Swing tutorial on Layout Managers to help you decide how to design the panel. You can also nest panels to get your desired layout.
I would like to have a JButton (with a folder icon image) inside a JTextField, like over on the far right of the JTextField, so that when clicked, the button opens up a JFileChooser, and when a file is selected, the path to the file appears inside the JTextField.
I have made this code, but nothing shows up.
public class TextFieldChooser extends JTextField {
public ImageIcon folderIcon;
public JButton btnFolder;
public TextFieldChooser(int columns) {
super(columns);
btnFolder = new JButton();
folderIcon = new ImageIcon(getClass().getResource("/resources/folder_find.png"));
btnFolder.setIcon(folderIcon);
this.add(btnFolder);
}
}
You may find the Component Border helpfull. It allows you to display a button in the text field by using the Border API.
Building on what Shakedown suggested, I think you can get the desired effect relatively easily. What you do is have a JPanel that contains both the text area and, beside it, the button. Next, set the text field to not draw any borders and give the JPanel a bevel border. Now it will look like the button is inside the text area. It might take some fine tuning, but it should work.
You can't don't want to put a button in a text field. You need to break out your intent into several components - 3, in fact.
First you're going to need a parent container, or something that will contain both your text field and also the button; a JPanel should suffice.
Then you need your real components, and by real I mean the ones that actually do something. These are your JTextField and JButton - go ahead and add these to the JPanel. In order to add them and have them appear how you want (with the button in the corner), you're going to need to specify a layout for your JPanel. This layout will define where added components go (visually) inside the JPanel.
Now that you've added those things into your JPanel, you can work only with your JPanel instead of thinking in terms of the contained JTextField and JButton.
Because Pyrite has not posted his final solution, here is mine:
my_button = new JButton("x");
JFormattedTextField my_textfield = new JFormattedTextField("Nr.");
my_textfield.setBorder(javax.swing.BorderFactory.createEmptyBorder());
JPanel textfield_with_button = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
Border lowered_bevelborder = BorderFactory.createLoweredBevelBorder();
textfield_with_button.setBorder(lowered_bevelborder);
textfield_with_button.add(my_textfield);
textfield_with_button.add(my_button);
Well I probably think you got your answer, but for others who want to do this easily, cuz I think the other answers are a bit too complicated.
So, what you need to do is, when you create the JTextField you create the JButton as well. Well see the code for yourself:
JButton button = new JButton();
button.setBounds(50, 5, 50, 25);
button.setBackground(Color.black);
JTextField textField = new JTextField();
textField.setBounds(20, 60, 100, 35);
textField.setBackground(Color.white);
textField.add(button);
And its that easy, I used setBounds() on the button cuz I can place it anywhere I want, as for the textField you can use a frame/panel layout aswell, but this was for just demonstrating how it works.
The code is like this:
JTextField txt = new JTextField();
txt.setBorder(BorderFactory.createMatteBorder(2,2,2,2,Color.red));
However the text field is ignoring my call to setBorder.
No changes whatsoever.
I were to replace it with a JLabel (for instance)
JLabel txt = new JLabel();
txt.setBorder(BorderFactory.createMatteBorder(2,2,2,2,Color.red));
I would see the red border.
Can anybody tell me why? Or even better explain to me how to add a border in the JTextField?
Check out this explanation/recommendation from the Java API
In general, when you want to set a
border on a standard Swing component
other than JPanel or JLabel, we
recommend that you put the component
in a JPanel and set the border on the
JPanel.
So... you should nest your JTextField in a JPanel or JLabel, and put the border on the JPanel or JLabel. Voila!