Note that there are some explanatory texts on larger screens.

plurals
  1. POJPasswordField focus issue
    text
    copied!<p>I wrote a very simple logon JPanel following the demo in <a href="http://download.oracle.com/javase/tutorial/uiswing/components/passwordfield.html" rel="nofollow">http://download.oracle.com/javase/tutorial/uiswing/components/passwordfield.html</a></p> <p>it has three components, </p> <ol> <li>JTextField to get username</li> <li>JPasswordField to get password</li> <li>JButton to logon</li> </ol> <p>then the JPanel is shown in the JFrame when the application starts. the issue is, I found if I click on password field first, i can enter password without problem. however if I enter username first then I can't enter anything in the password field. anyone knows what might be going wrong here? </p> <p>here is the the code I wrote for the logon panel, this code can be compiled and run, but the password can't be entered. is there something I missed?</p> <pre><code>import javax.swing.*; import javax.swing.border.*; import java.awt.*; import java.awt.event.*; import java.util.Arrays; public class Logon extends javax.swing.JPanel implements ActionListener { private JTextField fldName; private JPasswordField fldPasswd; private JButton btnLogon; public Logon() { JLabel labTitle = new JLabel("title"); labTitle.setText("EMT Monitor v.1.0"); // username fldName = new JTextField(10); JLabel labName = new JLabel("Username"); labName.setText("Username:"); labName.setLabelFor(fldPasswd); // passwd fldPasswd = new JPasswordField(10); fldPasswd.setActionCommand("Logon"); fldPasswd.addActionListener(this); fldPasswd.requestFocusInWindow(); JLabel labPasswd = new JLabel("Password"); labPasswd.setText("Password:"); labPasswd.setLabelFor(fldPasswd); // botten btnLogon = new JButton("Logon"); btnLogon.setActionCommand("Logon"); btnLogon.addActionListener(this); JPanel mainPanel = new JPanel(); btnLogon.setPreferredSize(new Dimension(200, 30)); mainPanel.setPreferredSize(new Dimension(340, 190)); mainPanel.add(labName); mainPanel.add(fldName); mainPanel.add(labPasswd); mainPanel.add(fldPasswd); mainPanel.add(btnLogon); JPanel outPanel = new JPanel(); outPanel.setPreferredSize(new Dimension(400, 300)); outPanel.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED)); outPanel.add(mainPanel); add(outPanel); setAlignmentX(Component.CENTER_ALIGNMENT); setAlignmentY(Component.CENTER_ALIGNMENT); setVisible(true); } public void actionPerformed(ActionEvent e) { String cmd = e.getActionCommand(); if (cmd.equals("Logon")) { //Process the password. String user = fldName.getText(); String passwd = new String(fldPasswd.getPassword()); System.out.println(user + " " + passwd); } } private static void createAndShowGUI() { //Create and set up the window. JFrame frame = new JFrame("Logon"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Create and set up the content pane. final Logon newContentPane = new Logon(); newContentPane.setOpaque(true); //content panes must be opaque frame.setContentPane(newContentPane); //Display the window. frame.pack(); frame.setVisible(true); } public static void main(String[] args) { //Schedule a job for the event dispatch thread: //creating and showing this application's GUI. SwingUtilities.invokeLater(new Runnable() { public void run() { //Turn off metal's use of bold fonts UIManager.put("swing.boldMetal", Boolean.FALSE); createAndShowGUI(); } }); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload