How can I set the size of JTextField? - java

I was trying to set size of JButton or JTextField or JPanel but somehow I didn't manage to do that.
I tried
JTextField FirstName = new JTextField("First Name");
FirstName.setSize(new Dimension(40, 20));
and
JTextField FirstName = new JTextField("Fisrt Name");
FirstName.setSize(40, 20);
but none of them worked. How can I achieve the desired effect?

Related

Resizing Components and Font with MigLayout

i'm pretty new to Layout Managers and i have absolutely no idea how to resize the Font-Size automatically with the MigLayout Manager. I have already managed to resize the components with the grow and fill constraint, but I don't seem to get the font-size to change with the size of the components. How do i do this?
Here my few code lines:
public class Projekte {
public Projekte()
{
main();
}
public static void main() {
JFrame projekte = new JFrame();
projekte.setBounds(100, 100,1080,1900);
projekte.setExtendedState(Frame.MAXIMIZED_BOTH);
projekte.setTitle("Testframe");
projekte.getContentPane().setBackground(new Color(255,255,255));
projekte.getContentPane().setLayout(new MigLayout("", "[][][][][][grow,fill][][][][]", "[][][][][][][][][][][grow,fill][][]"));
JLabel lblTest = new JLabel("Test");
projekte.getContentPane().add(lblTest, "cell 4 10,alignx trailing");
JTextField textField = new JTextField();
projekte.getContentPane().add(textField, "cell 5 10");
textField.setColumns(10);
JLabel lblTest_2 = new JLabel("Test_2");
projekte.getContentPane().add(lblTest_2, "cell 6 10,alignx trailing");
JTextField textField_2 = new JTextField();
projekte.getContentPane().add(textField_2, "cell 7 10");
textField_2.setColumns(10);
JLabel lblTest_3 = new JLabel("Test_3");
projekte.getContentPane().add(lblTest_3, "cell 4 11,alignx trailing");
JTextField textField_3 = new JTextField();
projekte.getContentPane().add(textField_3, "cell 5 11");
textField_3.setColumns(10);
}
}
I think it is quite easy, but I don't seem to find the solution, maybe you can help.
Font have attributes, but any of them relate to layout - you can manually scale font with some parameter using deriveFont(float size)
- creates a new Font object by replicating the current Font object and applying a new size to it.
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int screenWidth = screenSize.width;
float scale = screenWidth/1000;
label.getFont().deriveFont(scale);

Java: How do I change the font size of a JRadioButton?

How do I change the font size of a JRadioButton (in Java)?
I just want to make the text 24px instead of the default so that it matches the label.
Here's a link to a picture of my window...
(I don't have enough reputation to post the image directly...)
http://i.stack.imgur.com/z60kN.png
And here's a snippet of my code...
// question 1: make the labels / radio buttons / buttons / rows
JLabel q1 = new JLabel("Question 1: 2 + 4");
q1.setFont (q1.getFont ().deriveFont (24.0f));
final JRadioButton q1a1 = new JRadioButton("5");
final JRadioButton q1a2 = new JRadioButton("6");
final JRadioButton q1a3 = new JRadioButton("7");
final JRadioButton q1a4 = new JRadioButton("8");
JButton q1go = new JButton("Go");
JButton q1exit = new JButton("Exit");
JPanel question = new JPanel();
JPanel answers = new JPanel();
JPanel buttons = new JPanel();
question.setLayout(new BoxLayout(question, BoxLayout.X_AXIS));
answers.setLayout(new BoxLayout(answers, BoxLayout.Y_AXIS));
buttons.setLayout(new BoxLayout(buttons, BoxLayout.X_AXIS));
// add everything to the rows
question.add(q1);
answers.add(q1a1);
answers.add(q1a2);
answers.add(q1a3);
answers.add(q1a4);
buttons.add(q1go);
buttons.add(q1exit);
// add the rows
add(question);
add(answers);
add(answers);
add(answers);
add(answers);
add(buttons);
// group the radio buttons
ButtonGroup group = new ButtonGroup();
group.add(q1a1);
group.add(q1a2);
group.add(q1a3);
group.add(q1a4);
Thanks in advance!
-HewwoCraziness
You've already done it.
You want the label to have as same font size as Jlabel right. Then you can simply do that by
q1a1.setFont(q1.getFont()); //assign the label's font to radiobutton's font
or do the same way you've done it for label
q1a1.setFont(q1a1.getFont().deriveFont(24.0f));

Components get squished in JPanel and JFrame

I'm trying to create a GUI window for the user to enter some information for my program. But it seems like no matter how I change the sizes or the locations, all of my components are squished in far smaller than what I want. Could someone please point out what I am missing?
Here's what I tried:
JFrame inputFrame = new JFrame();
JPanel panel = new JPanel();
inputFrame.setTitle("Create Event");
inputFrame.setSize(500,400);
JTextField eventName = new JTextField("Untitled event");
JTextField eventStart = new JTextField();
JTextField eventEnd = new JTextField();
JButton save = new JButton("Save");
JLabel selectedDate = new JLabel(MyCalendarTester.currentMonth + 1 + "/" + selectedDay + "/" + MyCalendarTester.currentYear);
selectedDay = null;
panel.setSize(450,300);
eventName.setBounds(10, 10, 600, 50);
panel.add(eventName);
selectedDate.setBounds(10, 20, 50, 20);
panel.add(selectedDate);
panel.add(eventStart);
eventStart.setBounds(100, 20, 50, 20);
panel.add(eventEnd);
eventEnd.setBounds(175, 20, 50, 20);
panel.add(save);
save.setBounds(250, 20, 60, 30);
inputFrame.add(panel);
inputFrame.setVisible(true);
The default layout manager for a JPanel is the FlowLayout. The FlowLayout will display components at their preferred size, which is the way the component should be displayed.
You should not attempt to give the component a random size because you don't know what the best size for the component should be based on Font, OS etc.
When you create a JTextField you can use:
JTextField textField = new JTextField(10);
The value 10 will allow the text field to give itself a reasonable preferred size.
The JLabel and JButton size will be determined by the text of the component.

Java add text to textArea and textField

I am trying to make a JTextArea and a JTextField that has pre-set text in it already so when the user clicks in the box, they can edit it so something else.
Here's what I have for my text field:
private JTextField gridSizeField = new JTextField(6);
and my text area:
private JTextArea status = new JTextArea(20, 20);
private JScrollPane statusScroller = new JScrollPane(status);
You can use one of the overloaded constructors that accepts a String as an argument.
JTextField(String text, int columns)
JTextField gridSizeField = new JTextField("Your Text", 15);
Same for JTextArea
JTextArea(String text, int rows, int cols)
JTextArea status = new JTextArea("Your status", 20, 20);
You should have referred to the JavaDocs first
More on JTextArea and JTextField
In order to have text already set inside the JTextArea, it is a parameter of the JTextArea constructor. For example, to have the string "Text already in JTextArea", you just have to do
private JTextArea status = new JTextArea("Text already in JTextArea", 20, 20);
private JTextField field = new JTextField("Text already in JTextField", 6);
Hope it helps!

Adding a label for a JTextField

I have a JTextField and I want to add a label next to it, so that it looks like this...
+---------------+
TEAM 1: |Text field here|
+---------------+
This is the code where I am constructing the JTextFields...
jb = new JButton(">> FIGHT <<");
jt0 = new JTextField("", 25);
jt1 = new JTextField("", 25);
jt2 = new JTextField("<< BATTLE VICTOR >>", 35);
Could someone please tell me how to add the label.
Just put a JLabel next to your JTextField.
In addition to layout selection, don't forget that "you can improve your program's accessibility by using the setLabelFor() method," as discussed in How to Use Labels.
If you want to add any text to your interface, you should use JLabel. By placing a JLabel next to a JTextField, you can achieve the look you are asking for.
A JLabel is simply constructed like this...
JLabel myLabel = new JLabel("This is my message");
The way you add the JLabel to your interface depends on your JPanel/JFrame layout, but I tend to prefer BorderLayout, which you would use like this...
JLabel myLabel = new JLabel("Team 1");
JTextField myTextField = new JTextField("Team Awesome!");
JPanel panel = new JPanel(new BorderLayout());
panel.add(myLabel,BorderLayout.WEST);
panel.add(myTextField,BorderLayout.CENTER);
This will put a JLabel to the left (WEST) of the JTextField.
Refer to the following documentation for more information:
JLabel: http://docs.oracle.com/javase/6/docs/api/javax/swing/JLabel.html
BorderLayout: http://docs.oracle.com/javase/6/docs/api/java/awt/BorderLayout.html
You want to use a combination of a layout manager and JLabels
JLabel label = new JLabel("User name:");
JTextField field = new JTextField(12);
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
add(label, gbc);
gbc.grid++;
add(field, gbc);
Check out How to Use Labels for more examples

Categories