JTextField text=newJTextField("Initial Value";
I got the string in the text field with getText
and found that I got the "Initial Value".However, if you remove the 値initial value 」 string in the text field, enter another string, and getText
again, the retrieved string remained でしたinitial value 」 (only preconfigured strings in the text field can be retrieved).
How can I get the string in the text field properly?Thank you.We are currently doing getText with this code only.
import java.awt.EventQueue;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
public class registration extensions JFrame {
private static final long serialVersionUID = 1L;
private JPanel contentPane;
JTextField producttext;
JTextField pricetext;
/**
* Launch the application.
*/
public static void main(String[]args) {
EventQueue.invokeLater(newRunnable(){
public void run() {
try{
registration frame = new registration();
frame.setVisible(true);
} catch(Exceptione){
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public registration(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds (100, 100, 450, 300);
contentPane=newJPanel();
contentPane.setBorder(new EmptyBorder(5,5,5,5));
contentPane.setLayout(null);
setContentPane(contentPane);
// commodity registration department
JLabel productlabel = new JLabel("name");
producttext=new JTextField("");
productlabel.setBounds(10,10,60,20);
contentPane.add(productlabel);
producttext.setBounds(80,13,100,20);
contentPane.add(producttext);
JLabel price = new JLabel ("price");
pricetext = new JTextField("");
price.setBounds(10,40,60,20);
contentPane.add(price);
pricetext.setBounds (80,43,100,20);
contentPane.add(pricetext);
JButton addbutton=new JButton("Register";
addbutton.setBounds (80, 70, 60, 30);
addbutton.addActionListener (new addProduct());
contentPane.add(addbutton);
}
public String [ ] getTextField() {
String [ ] str;
str = new String [2];
str[0] = producttext.getText();
str[1] = prietext.getText();
return str;
}
public void setTextField(){
producttext.setText("");
pricetext.setText("");
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JoptionPane;
public class addProduct implements ActionListener {
public void actionPerformed (ActionEvent event) {
String str [ ];
JFrame frame = new JFrame();
registration gettext = new registration();
str=gettext.getTextField();
if(str[0].equals("")&str[1].equals(""){
JoptionPane.showConfirmDialog(frame, "blank", "Error!", JoptionPane.DEFAULT_OPTION, JoptionPane.ERROR_MESSAGE);
} else if(str[0].equals(""){
JoptionPane.showConfirmDialog(frame, "name is blank", "Error!", JoptionPane.DEFAULT_OPTION, JoptionPane.ERROR_MESSAGE);
} else if(str[1].equals("")}
JoptionPane.showConfirmDialog(frame, "price is blank", "Error!", JoptionPane.DEFAULT_OPTION, JoptionPane.ERROR_MESSAGE);
} else {
JoptionPane.showConfirmDialog(frame, "Registration Complete", "Message", JoptionPane.DEFAULT_OPTION, JoptionPane.INFORMATION_MESSAGE);
gettext.setTextField();
}
}
}
In ActionListener
(addProduct
), you create a new JFrame
(registration
) and call getTextField()
to always see the initial value.
For example, why don't you make the addProduct
class an internal class of the registration
class and modify it to invoke the getTextField()
method of the registration
class you are currently viewing?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class registration extensions JFrame {
private static final long serialVersionUID = 1L;
private JPanel contentPane;
JTextField producttext;
JTextField pricetext;
public static void main(String[]args) {
EventQueue.invokeLater(newRunnable(){
@ Override
public void run() {
try{
registration frame = new registration();
frame.setVisible(true);
} catch(Exceptione){
e.printStackTrace();
}
}
});
}
public registration(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds (100, 100, 450, 300);
contentPane=newJPanel();
contentPane.setBorder(new EmptyBorder(5,5,5,5));
contentPane.setLayout(null);
setContentPane(contentPane);
// commodity registration department
JLabel productlabel = new JLabel("name");
producttext=new JTextField("");
productlabel.setBounds(10,10,60,20);
contentPane.add(productlabel);
producttext.setBounds(80,13,100,20);
contentPane.add(producttext);
JLabel price = new JLabel ("price");
pricetext = new JTextField("");
price.setBounds(10,40,60,20);
contentPane.add(price);
pricetext.setBounds (80,43,100,20);
contentPane.add(pricetext);
JButton addbutton=new JButton("Register";
addbutton.setBounds (80, 70, 60, 30);
addbutton.addActionListener (new addProduct());
contentPane.add(addbutton);
}
public String [ ] getTextField() {
String [ ] str = new String [2];
str[0] = producttext.getText();
str[1] = prietext.getText();
return str;
}
public void setTextField(){
producttext.setText("");
pricetext.setText("");
}
class addProduct implements ActionListener {
@ Override
public void actionPerformed (ActionEvent event) {
// String str [ ];
// JFrame frame = new JFrame();
// Creating a new registration
// registration gettext = new registration();
//str=gettext.getTextField();
Window frame = SwingUtilities.getWindowAnceptor(Component) event.getSource());
String [ ] str = getTextField();
if(str[0].equals("")&str[1].equals(""){
JoptionPane.showConfirmDialog(frame, "blank", "Error!", JoptionPane.DEFAULT_OPTION, JoptionPane.ERROR_MESSAGE);
} else if(str[0].equals(""){
JoptionPane.showConfirmDialog(frame, "name is blank", "Error!", JoptionPane.DEFAULT_OPTION, JoptionPane.ERROR_MESSAGE);
} else if(str[1].equals("")}
JoptionPane.showConfirmDialog(frame, "price is blank", "Error!", JoptionPane.DEFAULT_OPTION, JoptionPane.ERROR_MESSAGE);
} else{
JoptionPane.showConfirmDialog(frame, "Registration Complete", "Message", JoptionPane.DEFAULT_OPTION, JoptionPane.INFORMATION_MESSAGE);
// gettext.setTextField();
setTextField();
}
}
}
}
Alternatively, you may want to retrieve the parent window (registration
in this sample) from the event source (JButton
) such as the SwingUtilities.getWindowAncestor(...)
method and invoke gettext.getTextField();
.
public class addProduct implements ActionListener{
@ Override
public void actionPerformed (ActionEvent event) {
Window frame = SwingUtilities.getWindowAnceptor(Component) event.getSource());
if(frame instance of registration) {
registration gettext=(registration) frame;
String [ ] str = gettext.getTextField();
if(str[0].equals("")&str[1].equals(""){
// Omitted below...
© 2024 OneMinuteCode. All rights reserved.