Results 1 to 10 of 10
  1. #1
    ehochedez is offline Member
    Join Date
    Aug 2009
    Posts
    14
    Rep Power
    0

    Question 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?

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    877
    Rep Power
    8

    Default

    Set the requestFocus(false) on the textFieldComponent .U can do this for any component.

    Example

    JTextField field1 = new JTextField( 10 );

    field1.requestFocus(false);

    -Regards
    Ramya
    Ramya:cool:

  3. #3
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    10

    Default

    Quote Originally Posted by RamyaSivakanth View Post
    Set the requestFocus(false) on the textFieldComponent .U can do this for any component.

    Example

    JTextField field1 = new JTextField( 10 );

    field1.requestFocus(false);

    -Regards
    Ramya
    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

    Java Code:
    field.setFocusable(false)
    . When that button is clicked you can then set it back to true and request the focus for the field when you need it.

  4. #4
    ehochedez is offline Member
    Join Date
    Aug 2009
    Posts
    14
    Rep Power
    0

    Default

    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?

  5. #5
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    877
    Rep Power
    8

    Default

    Could you please send the code ?
    Ramya:cool:

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    10

    Default

    Quote Originally Posted by ehochedez View Post
    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?
    Of course it doesn't work. It's wrong. Read my response above.
    Also read the API specs.

  7. #7
    ehochedez is offline Member
    Join Date
    Aug 2009
    Posts
    14
    Rep Power
    0

    Default

    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

    }
    ////////////////////////////////////////////////////////////////////////////////////

  8. #8
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    10

    Default

    Quote Originally Posted by ehochedez View Post
    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..
    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();
    
    			}
    		});

  9. #9
    ehochedez is offline Member
    Join Date
    Aug 2009
    Posts
    14
    Rep Power
    0

    Default

    Sorry i admit i wasn't very clear..
    You were right it is working now.
    Thanks a lot for your help r0351..

  10. #10
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    10

    Default

    Quote Originally Posted by ehochedez View Post
    ..
    Thanks a lot for your help r0351..
    It's r035198x, you're welcome.

Similar Threads

  1. Avoid using systems DPI settings ?
    By rolfius in forum AWT / Swing
    Replies: 5
    Last Post: 08-03-2009, 03:12 AM
  2. How to avoid response being commited in filter
    By touchsky88 in forum Advanced Java
    Replies: 1
    Last Post: 05-20-2009, 04:42 AM
  3. how to avoid input errors?
    By Sinnergy in forum New To Java
    Replies: 5
    Last Post: 02-03-2009, 12:25 AM
  4. avoid legitimate spamming
    By hervey in forum Suggestions & Feedback
    Replies: 1
    Last Post: 10-29-2008, 09:20 AM
  5. Avoid/Eliminate Duplicate Code
    By kicker in forum New To Java
    Replies: 6
    Last Post: 09-11-2008, 04:14 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •