When i type the text on textfield1 same text need to be display on textfield2 in java. i tried but i couldn't do it. if i remove the text on textfield1 same text should remove on textfield2.what i tried already so far i attached below.
this is code i tried
private void txtnameFocusGained(java.awt.event.FocusEvent evt) {
String name = txtname.getText();
txtname2.setText(name);
}
enter image description here
Related
I have a JPanel containing a JButton Like The Following Photo:
The problem is that if I typed a text like "Test Name" it is OK because the size of the button is fit!
But if I typed a text like "Test Name Example" or any other big text I need it to do like the following if it is possible!
Note: Code for the button
JButton btnfast = new JButton();
btnfast.setSize(120, 120);
btnfast.setMinimumSize(btnfast.getSize());
btnfast.setBackground(Color.white);
btnfast.setForeground(Color.black);
btnfast.setFont(Classes.Setting.fontDef);
btnfast.setText("Test Name Example");
btnfast.setAlignmentX(Component.CENTER_ALIGNMENT);
pd2.FastAddPanal.add(btnfast);
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.
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!
Below code only showing the textfield, but I want to include textfield and textarea. Need Help
form1 = new Form("Mobile");
tb2 = new TextField("To: ", "", 30, TextField.ANY);
TextBox tb3 = new TextBox("Message: ", "", 256, TextField.ANY);
form1.append(tb2);
// form1.append(tb3);
form1.addCommand(submitCommand);
display.setCurrent(tb3);
display.setCurrent(form1);
What you call textarea is an lcdui object TextBox; it can not be shown at the same screen as TextField.
If you're interested, refer to 'lcdui' tag info for more details on why is that (there are links to API reference, tutorials, popular libraries etc).
For the code snippet you posted, first thing that comes to mind would be to just replace TextBox to TextField, like
// ...initialization of Form and tb2
TextField tb3 = new TextField("Message: ", "", 256, TextField.ANY);
// above, TextBox has been replaced with TextField
form1.append(tb3); // show "Message" textfield above "Mobile"
form1.append(tb2);
form1.addCommand(submitCommand);
display.setCurrent(form1);
There is no such thing as TextArea in J2ME. You can show either Form with TextField[s] or TextBox, because TextBox is a Displayable. You can show only one Displayable at a time.
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...