我正在使用NetBeans IDE來設計一個帶有java和swing的圖形用戶界面。 GUI的目的是獲取用戶輸入,然後生成一些文本文件,用作Fortran編寫的程序的輸入。如何在Java中調用InputVerifier?
問題是Fortran程序無法處理文件名或路徑中的空格。所以我需要檢查用戶的文件名在GUI中沒有任何空格。
我已經實現了一個InputVerifier ContainsNoSpaces
就是這麼做的。但是,只有當用戶關注jTextField時纔會調用它。問題在於用戶不太可能關注jTextField,而是使用激活JFileChooser的jButton輸入文件名。
我想要做的是在動作監聽器jButton1ActionPerfromed
內放置一些類似jTextField1.verifyInput()
的內容,以便我可以向用戶顯示錯誤對話框。我怎樣才能做到這一點?
下面是一個最小的(不)工作示例:
import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.text.JTextComponent;
public class InputVerifierMwe extends javax.swing.JFrame {
public InputVerifierMwe() {
initComponents();
}
private void initComponents() {
jButton1 = new javax.swing.JButton();
jTextField1 = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("Choose file ...");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jTextField1.setInputVerifier(new ContainsNoSpaces());
jLabel1.setText("File name:");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jButton1)
.addGap(18, 18, 18)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser c = new JFileChooser();
int rVal = c.showDialog(InputVerifierMwe.this, "Choose file");
if (rVal == JFileChooser.APPROVE_OPTION) {
jTextField1.setText(c.getSelectedFile().getName());
/*
*
* This is where I need to verify the input.
*
*/
}
}
class ContainsNoSpaces extends InputVerifier {
public boolean verify(JComponent input) {
final JTextComponent source = (JTextComponent) input;
String s = source.getText();
boolean valid = s.indexOf(" ") == -1;
if (valid)
return true;
else {
JOptionPane.showMessageDialog(source, "Spaces are not allowed.",
"Input error", JOptionPane.ERROR_MESSAGE);
return false;
}
}
}
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(InputVerifierMwe.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(InputVerifierMwe.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(InputVerifierMwe.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(InputVerifierMwe.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new InputVerifierMwe().setVisible(true);
}
});
}
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JTextField jTextField1;
}
謝謝!我將使用你的第一個建議'requestFocusInWindow()'。這樣我可以重新使用相同的InputVerifier類擴展來處理其他幾個文本字段。 –