Results 1 to 10 of 10
Thread: How to avoid focus()..?
- 08-27-2009, 10:03 AM #1
Member
- Join Date
- Aug 2009
- Posts
- 14
- Rep Power
- 0
How to avoid focus()..?
Sorry for my poor English..
I'm trying to make an application which NetBeans using Swing and on my first jFrame i have a textField. My problem is that when i start the application this textfield gets directly focused on, and i don't want that, i want nothing to be focused on. Besides when i click somewhere else on the frame after where there is no object, the textField keeps beeing focused..
Does someone know how to avoid that?
- 08-27-2009, 10:42 AM #2
Set the requestFocus(false) on the textFieldComponent .U can do this for any component.
Example
JTextField field1 = new JTextField( 10 );
field1.requestFocus(false);
-Regards
RamyaRamya:cool:
- 08-27-2009, 10:56 AM #3
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Did you read the API specs for that method to see what it really does?
@OP You can stop a component from getting focus by setting
. When that button is clicked you can then set it back to true and request the focus for the field when you need it.Java Code:field.setFocusable(false)
- 08-27-2009, 10:57 AM #4
Member
- Join Date
- Aug 2009
- Posts
- 14
- Rep Power
- 0
Thanks Ramya for your reply.
I tried to use .requestFocus(false) on my textField but it doesnt work, it doesn't change anything..
And i also tried setRequestFocusEnable(false) but it also doesnt work, worse if i click on another button or textField, then i can't foncus on my 1st textField anymore..
Any other idea?
- 08-27-2009, 11:04 AM #5
Could you please send the code ?
Ramya:cool:
- 08-27-2009, 11:13 AM #6
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
- 08-27-2009, 11:16 AM #7
Member
- Join Date
- Aug 2009
- Posts
- 14
- Rep Power
- 0
I tried with the setFocusable(false) on my textFields and to make them focusable once we click on them but it means you have to click 2 times on them to be able to write.. that's a problem..
I join the code.
I also want that everytime someone click on a textField, a virtual keyboard appeared at the bottom of the Frame (it is for touch screens)(this part is already working, it's just to explain the code)
////////////////////////////////////////////////////////////////////////////////////
package project1;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import javax.swing.JButton;
import javax.swing.JTextField;
public class Login extends javax.swing.JFrame {
private JTextField textFieldOnFocus;
private String[] tab_string = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "'", "(", ")", "^", "q", "w", "e", "r", "t", "y", "u", "i", "o", "p", "@", "a", "s", "d", "f", "g", "h", "j", "k", "l", "-", "z", "x", "c", "v", "b", "n", "m", ",", ".", "!","?", "Maj", " ", "Back"};
//"ā", "á", "ǎ", "à", "è", "ě", "é", "ē", "ī", "í", "ǐ", "ì", "ò", "ǒ", "ó", "ō", "ū", "ú", "ǔ", "ù", "ü", "ǜ", "ǚ", "ǘ", "ǖ"
private JButton[] tab_button = new JButton[tab_string.length];
private javax.swing.JPanel test = new javax.swing.JPanel();
private int shift = 0;
public Login() {
initComponents();
test.setBackground(new java.awt.Color(183, 155, 123));
// Virtual Keyboard
for(int i = 0; i < tab_string.length; i++){
tab_button[i] = new JButton(tab_string[i]);
// On attribue les ActionListener
if(i!=46 && i!=48)
tab_button[i].addActionListener(new PushKey());
if(i==46)
tab_button[i].addActionListener(new Shift());
if(i==48)
tab_button[i].addActionListener(new Back());
// On place graphiquement les boutons sur le keyboardPanel
if(i<14)
tab_button[i].setBounds(95 + i*60, 6, 55, 45);
if(i>13 && i<25)
tab_button[i].setBounds(185+(i-14)*60, 57, 55, 45);
if(i>24 && i<35)
tab_button[i].setBounds(215+(i-25)*60, 108, 55, 45);
if(i>34 && i<46)
tab_button[i].setBounds(185+(i-35)*60, 159, 55, 45);
if(i==46)
tab_button[i].setBounds(95, 210, 120, 45);
if(i==47)
tab_button[i].setBounds(262, 210, 500, 45);
if(i==48)
tab_button[i].setBounds(810, 210, 120, 45);
keyboardPanel.add(tab_button[i]);
}
//////////////////////////////////////////////////////////////////////////////////////////
wrongLoginLabel.setVisible(false);
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
nicknameTextField = new javax.swing.JTextField();
nicknameLabel = new javax.swing.JLabel();
passwordLabel = new javax.swing.JLabel();
validateButton = new javax.swing.JButton();
wrongLoginLabel = new javax.swing.JLabel();
passwordField = new javax.swing.JPasswordField();
keyboardPanel = new javax.swing.JPanel();
jPanel1 = new javax.swing.JPanel();
setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);
setBackground(new java.awt.Color(255, 102, 255));
setResizable(false);
setUndecorated(true);
addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
formMouseClicked(evt);
}
});
getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
nicknameTextField.setFocusable(false);
nicknameTextField.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
TextFieldMouseClicked(evt);
}
});
nicknameTextField.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusGained(java.awt.event.FocusEvent evt) {
TextFieldFocusGained(evt);
}
});
getContentPane().add(nicknameTextField, new org.netbeans.lib.awtextra.AbsoluteConstraints(450, 320, 110, -1));
nicknameLabel.setHorizontalAlignment(javax.swing.S wingConstants.RIGHT);
nicknameLabel.setText("Nickname :");
getContentPane().add(nicknameLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(360, 320, 80, 20));
passwordLabel.setHorizontalAlignment(javax.swing.S wingConstants.RIGHT);
passwordLabel.setText("Password :");
getContentPane().add(passwordLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(360, 350, 80, 20));
validateButton.setText("Validate");
validateButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
validateButtonActionPerformed(evt);
}
});
getContentPane().add(validateButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(450, 380, 110, -1));
wrongLoginLabel.setForeground(new java.awt.Color(255, 51, 51));
wrongLoginLabel.setHorizontalAlignment(javax.swing .SwingConstants.CENTER);
wrongLoginLabel.setText("Wrong nickname or password");
getContentPane().add(wrongLoginLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(350, 440, 270, 20));
passwordField.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
TextFieldMouseClicked(evt);
}
});
passwordField.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusGained(java.awt.event.FocusEvent evt) {
TextFieldFocusGained(evt);
}
});
passwordField.setFocusable(false);
getContentPane().add(passwordField, new org.netbeans.lib.awtextra.AbsoluteConstraints(450, 350, 110, -1));
keyboardPanel.setBackground(new java.awt.Color(204, 255, 153));
keyboardPanel.setLayout(null);
getContentPane().add(keyboardPanel, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 500, 1024, 268));
keyboardPanel.setVisible(false);
jPanel1.setBackground(new java.awt.Color(204, 204, 255));
getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 1024, 768));
java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize ();
setBounds((screenSize.width-1024)/2, (screenSize.height-768)/2, 1024, 768);
}// </editor-fold>
private void validateButtonActionPerformed(java.awt.event.Actio nEvent evt) {
String nickname_typed;
//nickname_typed = Utils.Strtr_tab({"(",")"},"",nicknameTextField.get Text());
keyboardPanel.setVisible(false);
try{Class.forName("com.mysql.jdbc.Driver");}
catch (ClassNotFoundException e) {
System.err.println(e) ;
//System.exit (-1) ;
}
try {
Connection ma_connection = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/flashpoint",
"eric",
"mdp");
String ma_requete = "SELECT nickname FROM profiles WHERE nickname='"+nicknameTextField.getText()+"' AND password='"+passwordField.getText()+"'" ;
java.sql.Statement mon_statement = ma_connection.createStatement();
ResultSet mon_resultset = mon_statement.executeQuery(ma_requete);
if (mon_resultset.first()){
ma_connection.close();
Menu currentWindow = new Menu();
currentWindow.show();
dispose();
}else{
wrongLoginLabel.setVisible(true);
ma_connection.close();
};
} catch (java.sql.SQLException e) {
System.err.println(e);
//System.exit(-1);
}
}
private void TextFieldFocusGained(java.awt.event.FocusEvent evt) {
keyboardPanel.setVisible(true);
textFieldOnFocus = ((JTextField)evt.getSource());
}
private void formMouseClicked(java.awt.event.MouseEvent evt) {
if(evt.getY()<424)
keyboardPanel.setVisible(false);
}
private void TextFieldMouseClicked(java.awt.event.MouseEvent evt) {
((JTextField)evt.getSource()).setFocusable(true);
}
class PushKey implements ActionListener{
public void actionPerformed(ActionEvent e) {
String key = ((JButton)e.getSource()).getText();
if(shift==1)
key = key.toUpperCase();
String textFieldContent = textFieldOnFocus.getText();
String newContent = textFieldContent+key;
textFieldOnFocus.setText(newContent);
}
}
class Shift implements ActionListener{
public void actionPerformed(ActionEvent e) {
if(shift == 1)
shift = 0;
else
shift = 1;
}
}
class Back implements ActionListener{
public void actionPerformed(ActionEvent e) {
String textFieldContent = textFieldOnFocus.getText();
String newContent = textFieldContent.substring(0, textFieldContent.length()-1) ;
textFieldOnFocus.setText(newContent);
}
}
public String Strtr(String carA, String carB, String s1 ){
String s2 = "";
for(int i = 0; i < s1.length(); i++){
if(carA.equals(s1.charAt(i)))
s2 = s2+carB;
else
s2 = s2+s1.charAt(i);
}
return s2;
}
public String Strtr_tab(String[] tab_carA, String carB, String s1){
String s2 = "";
for(int k = 0 ; k<tab_carA.length ; k++)
{
for(int i = 0; i < s1.length(); i++){
if(tab_carA[k].equals(s1.charAt(i)))
s2 = s2+carB;
else
s2 = s2+s1.charAt(i);
}
}
return s2;
}
// Variables declaration - do not modify
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel keyboardPanel;
private javax.swing.JLabel nicknameLabel;
private javax.swing.JTextField nicknameTextField;
private javax.swing.JPasswordField passwordField;
private javax.swing.JLabel passwordLabel;
private javax.swing.JButton validateButton;
private javax.swing.JLabel wrongLoginLabel;
// End of variables declaration
}
////////////////////////////////////////////////////////////////////////////////////
- 08-27-2009, 11:25 AM #8
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
You are not describing your requirements accurately.
First you said you want them to be focused after a button is clicked now you say you want them to be focused they they are clicked. That is different.
Just add a MouseListener to it
Java Code:textfield.setFocusable(false); textfield.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { textfield.setFocusable(true); textfield.requestFocusInWindow(); } });
- 08-27-2009, 11:30 AM #9
Member
- Join Date
- Aug 2009
- Posts
- 14
- Rep Power
- 0
Sorry i admit i wasn't very clear..
You were right it is working now.
Thanks a lot for your help r0351..
- 08-27-2009, 11:32 AM #10
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Similar Threads
-
Avoid using systems DPI settings ?
By rolfius in forum AWT / SwingReplies: 5Last Post: 08-03-2009, 03:12 AM -
How to avoid response being commited in filter
By touchsky88 in forum Advanced JavaReplies: 1Last Post: 05-20-2009, 04:42 AM -
how to avoid input errors?
By Sinnergy in forum New To JavaReplies: 5Last Post: 02-02-2009, 11:25 PM -
avoid legitimate spamming
By hervey in forum Suggestions & FeedbackReplies: 1Last Post: 10-29-2008, 08:20 AM -
Avoid/Eliminate Duplicate Code
By kicker in forum New To JavaReplies: 6Last Post: 09-11-2008, 04:14 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks