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!
Related
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));
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?
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.
Basically I want change the JLabel's Label text during on-click the button
'Generate PDF Record Book'
From the previous example says:
label.setText("new value");
when I do that, the label value doesn't change at all, please give me some directions, thanks
initialize();
JLabel lblNewLabel = new JLabel("513 k bytes");
lblNewLabel.setBounds(407, 713, 151, 14);
frmViperManufacturingRecord.getContentPane().add(lblNewLabel);
On button Generate PDF Record Book click
JButton btnGeneratePdfHeader = new JButton("Generate PDF Record Book");
btnGeneratePdfHeader.setMnemonic('G');
btnGeneratePdfHeader.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
final JLabel lblNewLabel = new JLabel("513 k bytes");
//java.io.File file = new java.io.File(strdfile);
//lblNewLabel.setSize(file.length());
//System.out.println(file.length());
String fileSize = file.length() + " k bytes";
System.out.println("I am here");
lblNewLabel.setText("new value");
}
});
You are creating a new JLabel when pressing the button and then set the text of that label to "new value"
final JLabel lblNewLabel = new JLabel("513 k bytes");
lblNewLabel.setText("new value");
rather than changing the text of the label on your UI. You will need to call setText("new value") on a reference to the label you've already added to the UI instead. For instance, that label would neeed to be a field in your UI class, eg final JLabel fileSizeLabel and you would set that labels text by calling
fileSizeLabel.setText("new value");
inside the buttons action listener.
All I want to do is have a JOptionPane inputDialog with a JTextArea instead of a JTextField.
I tried putting the JTextArea inside of the Message parameter like so
Object[] inputText = new Object[]{new JLabel("Enter Graph Information"),
newJTextArea("",20,10)};
graphInfo=(String)JOptionPane.showInputDialog(null,
inputText,
"Create Graph",
JOptionPane.PLAIN_MESSAGE,
null,
null,
"");
But it still has the text field at the bottom and I cannot get the text from the JTextArea.
Is there any way to either remove the original text field and get the text from the jtextarea or replace the text field with the text area completely? I'm trying to avoid having to make a custom dialog if possible and this "seems" like something that should be easy to do?
You're on the right lines; you just need to use showConfirmDialog instead of showMessageDialog, which allows you to pass any Component as your "message" and have it displayed within the JDialog. You can then capture the contents of the JTextArea if the user clicks OK; e.g.
int okCxl = JOptionPane.showConfirmDialog(SwingUtilities.getWindowAncestor(this),
textArea,
"Enter Data",
JOptionPane.OK_CANCEL_OPTION)
if (okCxl == JOptionPane.OK_OPTION) {
String text = textArea.getText();
// Process text.
}
If you want to show a JLabel in conjunction with your JTextArea you can create and pass in a JPanel containing both Components; e.g.
JTextArea textArea = ...
JPanel pnl = new JPanel(new BorderLayout());
pnl.add(new JLabel("Please enter some data:"), BorderLayout.NORTH);
pnl.add(textArea, BorderLayout.CENTER);
JOptionPane.show...