![](/img/trans.png)
[英]How to set text for JTextField from another class using DocumentFIlter?
[英]For a JTextField, how do I set text from another class?
我環顧了其他線程,找不到有效的解決方案。
我的程序讀取一個文件,將每個單詞分成一行,然后存儲在數組中。 如果要搜索的是array [0],那么我想將數組輸出到其對應的文本字段中。
我試圖通過僅設置1個文本字段(ID文本字段)的文本來測試它,但是該文本字段沒有填充文本。 到目前為止,這是我的代碼:
GUI-StudentUI
public class StudentUI extends javax.swing.JFrame {
/**
* Creates new form StudentUI
*/
public StudentUI() {
initComponents();
saveBtn.setVisible(false);
}
final String FILENAME = "Students.txt";
private void initComponents() {
searchTxt = new javax.swing.JTextField();
idLbl = new javax.swing.JLabel();
titleLbl = new javax.swing.JLabel();
forenameLbl = new javax.swing.JLabel();
surnameLbl = new javax.swing.JLabel();
address1Lbl = new javax.swing.JLabel();
address2Lbl = new javax.swing.JLabel();
postcodeLbl = new javax.swing.JLabel();
numberLbl = new javax.swing.JLabel();
birthLbl = new javax.swing.JLabel();
idTxt = new javax.swing.JTextField();
forenameTxt = new javax.swing.JTextField();
surnameTxt = new javax.swing.JTextField();
address1Txt = new javax.swing.JTextField();
address2Txt = new javax.swing.JTextField();
postcodeTxt = new javax.swing.JTextField();
numberTxt = new javax.swing.JTextField();
birthTxt = new javax.swing.JTextField();
searchBtn = new javax.swing.JButton();
searchLbl = new javax.swing.JLabel();
titleCombo = new javax.swing.JComboBox<>();
addBtn = new javax.swing.JButton();
editBtn = new javax.swing.JButton();
deleteBtn = new javax.swing.JButton();
saveBtn = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
idLbl.setText("ID");
titleLbl.setText("Title");
forenameLbl.setText("Forename");
surnameLbl.setText("Surname");
address1Lbl.setText("Address 1");
address2Lbl.setText("Address 2");
postcodeLbl.setText(" Postcode");
numberLbl.setText("Phone Number");
birthLbl.setText("Date of Birth (DD/MM/YYYY)");
searchBtn.setText("Search");
searchBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
searchBtnActionPerformed(evt);
}
});
public void setTextField(JTextField jTF, String value) {
jTF.setText(value);
}
private void searchBtnActionPerformed(java.awt.event.ActionEvent evt) {
int count = Search.totalLines(FILENAME);
Search.linear(FILENAME, count, searchTxt.getText());
}
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new StudentUI().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton addBtn;
private javax.swing.JLabel address1Lbl;
private javax.swing.JTextField address1Txt;
private javax.swing.JLabel address2Lbl;
private javax.swing.JTextField address2Txt;
private javax.swing.JLabel birthLbl;
private javax.swing.JTextField birthTxt;
private javax.swing.JButton deleteBtn;
private javax.swing.JButton editBtn;
private javax.swing.JLabel forenameLbl;
private javax.swing.JTextField forenameTxt;
private javax.swing.JLabel idLbl;
private javax.swing.JTextField idTxt;
private javax.swing.JLabel numberLbl;
private javax.swing.JTextField numberTxt;
private javax.swing.JLabel postcodeLbl;
private javax.swing.JTextField postcodeTxt;
private javax.swing.JButton saveBtn;
private javax.swing.JButton searchBtn;
private javax.swing.JLabel searchLbl;
private javax.swing.JTextField searchTxt;
private javax.swing.JLabel surnameLbl;
private javax.swing.JTextField surnameTxt;
private javax.swing.JComboBox<String> titleCombo;
private javax.swing.JLabel titleLbl;
// End of variables declaration
}
搜索類別
public class Search {
public static int totalLines(String filename) {
int n = 0;
String currentLine;
try {
FileReader fr = new FileReader(filename);
BufferedReader br = new BufferedReader(fr);
while ((currentLine = br.readLine()) != null) {
n = n + 1;
}
} catch (Exception e) {
}
return n;
}
public static void linear(String filename, int lines, String searchItem) {
String currentLine;
try {
FileReader fr = new FileReader(filename);
BufferedReader br = new BufferedReader(fr);
while ((currentLine = br.readLine()) != null) {
String[] array = new String[lines];
array = currentLine.split(",");
if (array[0].equals(searchItem)) {
StudentUI student = new StudentUI();
student.setTextField(student.idTxt, array[0]);
}
}
} catch (Exception e) {
}
}
}
您在(A)遇到了一個大問題:
public static void linear(String filename, int lines, String searchItem) {
String currentLine;
try {
FileReader fr = new FileReader(filename);
BufferedReader br = new BufferedReader(fr);
while ((currentLine = br.readLine()) != null) {
String[] array = new String[lines];
array = currentLine.split(",");
if (array[0].equals(searchItem)) {
StudentUI student = new StudentUI(); // ****** (A) ******
student.setTextField(student.idTxt, array[0]);
}
}
} catch (Exception e) {
// ****** (B) ******
}
}
您正在創建一個new StudentUI()
對象-一個新的對象,該對象與已經顯示的StudentUI完全不同,因此設置其JTextField的狀態將對當前顯示的StudentUI對象無效。
一個錯誤的解決方案是將JTextField變量設置為靜態-不要這樣做,因為這樣做會把OOP嬰兒和洗澡水一起扔掉。
更好的解決方案是將對當前顯示的 StudentUI對象的引用傳遞給他的方法,以便您可以更改感興趣的對象的狀態。
(B)中的另一個問題-不要以這種方式忽略異常,因為這樣做,如果您的代碼崩潰了,您將不知道為什么。
因此,解決此問題的一種方法是為線性方法提供一個StudentUI參數:
public static void linear(StudentUI student, String filename, int lines, String searchItem) {
String currentLine;
try {
FileReader fr = new FileReader(filename);
BufferedReader br = new BufferedReader(fr);
while ((currentLine = br.readLine()) != null) {
String[] array = new String[lines];
array = currentLine.split(",");
if (array[0].equals(searchItem)) {
// StudentUI student = new StudentUI(); // ****** (A) ******
student.setTextField(student.idTxt, array[0]);
}
}
} catch (Exception e) {
e.printStacktrace(); // ****** (B) *******
}
}
然后調用它,通過this
入法作為第一個參數。
另外,請注意,我現在正在打印異常的堆棧跟蹤,以便我可以查看是否/何時引發異常,導致異常的原因和位置。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.