2 Replies - 154 Views - Last Post: 31 July 2019 - 01:46 AM Rate Topic: -----

#1 dako   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 60
  • Joined: 01-July 19

What determines where the tab button takes my pointer?

Posted 30 July 2019 - 03:46 PM

I've made an eventhandler for the my login button to read when I press enter and what I am hoping to achieve is to enter my username (press tab) enter my password (press tab) and press enter....

However when I press tab it immediately goes from my username to the login button. Is this just the order of the code, maybe the bounds of the textfields versus the button? I will insert the code.

public LoginFrame() {
		setBackground(Color.WHITE);
		setIconImage(Toolkit.getDefaultToolkit().getImage(LoginFrame.class.getResource("/images/HyVee.png")));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 487, 149);
		contentPane = new JPanel();
		contentPane.setBackground(Color.WHITE);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		JLabel lblUser = new JLabel("Username");
		lblUser.setBounds(10, 20, 148, 25);
		contentPane.add(lblUser);
		
		JLabel lblPass = new JLabel("Password");
		lblPass.setBounds(10, 48, 148, 27);
		contentPane.add(lblPass);
		
		txtUsername = new JTextField();
		txtUsername.setBounds(171, 23, 96, 19);
		contentPane.add(txtUsername);
		txtUsername.setColumns(10);
		
		txtPass = new JPasswordField();
		txtPass.setBounds(171, 52, 96, 19);
		contentPane.add(txtPass);
		
		JButton btnLogin = new JButton("");
		btnLogin.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				boolean isValid = db.login(txtUsername.getText(),new String(txtPass.getPassword()));
				 if(e.getKeyCode() == KeyEvent.VK_ENTER) {
					 
					 if(isValid) {
						 dispose();
						LoginFrame.getWindows();
						 objTable.setVisible(true);
						 
					                  
					 
					             }
				   }
			}
		});
		btnLogin.setOpaque(false);
		btnLogin.setFocusPainted(false);
		btnLogin.setBorderPainted(false);
		btnLogin.setContentAreaFilled(false);
		btnLogin.setIcon(new ImageIcon(LoginFrame.class.getResource("/images/redlogin64.png")));
		btnLogin.setBackground(Color.WHITE);
		btnLogin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				boolean isValid = db.login(txtUsername.getText(),new String(txtPass.getPassword()));
				System.out.println(isValid);
					 if(isValid) {
						 dispose();
						LoginFrame.getWindows();
						 objTable.setVisible(true);
						 
					                  
					 
					             }
			}
					         });

				
				
			
		
		btnLogin.setBounds(341, 37, 104, 65);
		contentPane.add(btnLogin);
		


	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: What determines where the tab button takes my pointer?

#2 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 780
  • View blog
  • Posts: 5,858
  • Joined: 25-December 13

Re: What determines where the tab button takes my pointer?

Posted 30 July 2019 - 05:22 PM

Look at z-order for Swing components.
Wrong concept see following post.

This post has been edited by NormR: 31 July 2019 - 03:51 AM

Was This Post Helpful? 0
  • +
  • -

#3 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3639
  • View blog
  • Posts: 16,718
  • Joined: 20-September 08

Re: What determines where the tab button takes my pointer?

Posted 31 July 2019 - 01:46 AM

Have a look at

https://docs.oracle....misc/focus.html
and
https://blogs.sequoi...raversal-order/
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1