incah
asked on
How do I set the focus to a component ?
I want to give the focus to a textfield. I tried using requestFocus method but didn't work. I am using JDK 1.1 and Swing 1.0.2.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I have recompiled and run your code. The focus is OK when I start the app. When the database
does not exist, for example, the message appears and the focus is lost for the textfield. I
suppose that this is what annoy you. The following change fixes the "problem".
Before:
} catch(SQLException sqle) {
l2.setText("Could not open: "+tf1.getText());
return;
}
After:
} catch(SQLException sqle) {
l2.setText("Could not open: "+tf1.getText());
tf1.requestFocus();
return;
}
The caret position will be at the end of the text entered in the query. If you want to have it at
the beginning:
} catch(SQLException sqle) {
l2.setText("Could not open: "+tf1.getText());
tf1.requestFocus();
tf1.setCaretPosition(0);
return;
}
does not exist, for example, the message appears and the focus is lost for the textfield. I
suppose that this is what annoy you. The following change fixes the "problem".
Before:
} catch(SQLException sqle) {
l2.setText("Could not open: "+tf1.getText());
return;
}
After:
} catch(SQLException sqle) {
l2.setText("Could not open: "+tf1.getText());
tf1.requestFocus();
return;
}
The caret position will be at the end of the text entered in the query. If you want to have it at
the beginning:
} catch(SQLException sqle) {
l2.setText("Could not open: "+tf1.getText());
tf1.requestFocus();
tf1.setCaretPosition(0);
return;
}
I have recompiled and run your code. The focus is OK when I start the app. When the database
does not exist, for example, the message appears and the focus is lost for the textfield. I
suppose that this is what annoy you. The following change fixes the "problem".
Before:
} catch(SQLException sqle) {
l2.setText("Could not open: "+tf1.getText());
return;
}
After:
} catch(SQLException sqle) {
l2.setText("Could not open: "+tf1.getText());
tf1.requestFocus();
return;
}
The caret position will be at the end of the text entered in the query. If you want to have it at
the beginning:
} catch(SQLException sqle) {
l2.setText("Could not open: "+tf1.getText());
tf1.requestFocus();
tf1.setCaretPosition(0);
return;
}
does not exist, for example, the message appears and the focus is lost for the textfield. I
suppose that this is what annoy you. The following change fixes the "problem".
Before:
} catch(SQLException sqle) {
l2.setText("Could not open: "+tf1.getText());
return;
}
After:
} catch(SQLException sqle) {
l2.setText("Could not open: "+tf1.getText());
tf1.requestFocus();
return;
}
The caret position will be at the end of the text entered in the query. If you want to have it at
the beginning:
} catch(SQLException sqle) {
l2.setText("Could not open: "+tf1.getText());
tf1.requestFocus();
tf1.setCaretPosition(0);
return;
}
ASKER
When I run this program the focus is not OK. It is not in the textfield as it should be.
Here is the full code I have tested. Try it on your system. The focus remains in textfield on
my system (Swing 1.0.2 & JDK 1.1.5 on Win NT 4.0).
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import com.sun.java.swing.*;
import com.sun.java.swing.event.* ;
public class SwingApp extends JFrame implements ActionListener,CaretListen er {
JPanel p1,p2;
JLabel l1,l2;
JButton b1,b2;
JTextField tf1,tf2;
// FileJComboBox fcb1;
// Connection con;
public SwingApp() {
Container contentPane = getContentPane();
p1=new JPanel();
p2=new JPanel(new GridLayout(0,1));
p1.add(l1=new JLabel("Database Name",SwingConstants.LEFT) );
p1.add(tf1=new JTextField(50));
p1.add(b1=new JButton("Open"));
p1.add(b2=new JButton("Close"));
contentPane.add("Center",p 1);
p2.add(l2=new JLabel(" ",SwingConstants.CENTER));
contentPane.add("South",p2 );
b1.setEnabled(false);
b2.setEnabled(false);
tf1.requestFocus();
tf1.addCaretListener(this) ;
tf1.addActionListener(this );
b1.addActionListener(this) ;
b2.addActionListener(this) ;
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
});
}
public void actionPerformed(ActionEven t e) {
java.sql.Connection con= null;
if (e.getSource()==b1 || e.getSource()==tf1) {
try {
con=DriverManager.getConne ction("jdb c:odbc:"+t f1.getText ());
l2.setText("SUCCESS!");
tf1.setEnabled(false);
b1.setEnabled(false);
b2.setEnabled(true);
} catch(SQLException sqle) {
l2.setText("Could not open: "+tf1.getText());
tf1.requestFocus();
tf1.setCaretPosition(0);
return;
}
} else if (e.getSource()==b2) {
try {
con.close();
} catch(SQLException sqle) {
}
con=null;
tf1.setEnabled(true);
b1.setEnabled(true);
b2.setEnabled(false);
l2.setText("");
}
}
public void caretUpdate(CaretEvent e) {
if (tf1.getText().equals(""))
b1.setEnabled(false);
else
b1.setEnabled(true);
}
public static void main(String[] args) {
SwingApp window = new SwingApp();
try {
Class.forName("sun.jdbc.od bc.JdbcOdb cDriver");
DriverManager.setLogStream (null);
} catch(ClassNotFoundExcepti on cnfe) {
System.err.println("Class not found");
return;
}
window.setTitle("Java Application");
window.pack();
window.setVisible(true);
}
}
my system (Swing 1.0.2 & JDK 1.1.5 on Win NT 4.0).
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import com.sun.java.swing.*;
import com.sun.java.swing.event.*
public class SwingApp extends JFrame implements ActionListener,CaretListen
JPanel p1,p2;
JLabel l1,l2;
JButton b1,b2;
JTextField tf1,tf2;
// FileJComboBox fcb1;
// Connection con;
public SwingApp() {
Container contentPane = getContentPane();
p1=new JPanel();
p2=new JPanel(new GridLayout(0,1));
p1.add(l1=new JLabel("Database Name",SwingConstants.LEFT)
p1.add(tf1=new JTextField(50));
p1.add(b1=new JButton("Open"));
p1.add(b2=new JButton("Close"));
contentPane.add("Center",p
p2.add(l2=new JLabel(" ",SwingConstants.CENTER));
contentPane.add("South",p2
b1.setEnabled(false);
b2.setEnabled(false);
tf1.requestFocus();
tf1.addCaretListener(this)
tf1.addActionListener(this
b1.addActionListener(this)
b2.addActionListener(this)
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
});
}
public void actionPerformed(ActionEven
java.sql.Connection con= null;
if (e.getSource()==b1 || e.getSource()==tf1) {
try {
con=DriverManager.getConne
l2.setText("SUCCESS!");
tf1.setEnabled(false);
b1.setEnabled(false);
b2.setEnabled(true);
} catch(SQLException sqle) {
l2.setText("Could not open: "+tf1.getText());
tf1.requestFocus();
tf1.setCaretPosition(0);
return;
}
} else if (e.getSource()==b2) {
try {
con.close();
} catch(SQLException sqle) {
}
con=null;
tf1.setEnabled(true);
b1.setEnabled(true);
b2.setEnabled(false);
l2.setText("");
}
}
public void caretUpdate(CaretEvent e) {
if (tf1.getText().equals(""))
b1.setEnabled(false);
else
b1.setEnabled(true);
}
public static void main(String[] args) {
SwingApp window = new SwingApp();
try {
Class.forName("sun.jdbc.od
DriverManager.setLogStream
} catch(ClassNotFoundExcepti
System.err.println("Class not found");
return;
}
window.setTitle("Java Application");
window.pack();
window.setVisible(true);
}
}
ASKER
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import com.sun.java.swing.*;
import com.sun.java.swing.event.*
public class SwingApp extends JFrame implements ActionListener,CaretListen
JPanel p1,p2;
JLabel l1,l2;
JButton b1,b2;
JTextField tf1,tf2;
FileJComboBox fcb1;
Connection con;
public SwingApp() {
Container contentPane = getContentPane();
p1=new JPanel();
p2=new JPanel(new GridLayout(0,1));
p1.add(l1=new JLabel("Database Name",SwingConstants.LEFT)
p1.add(tf1=new JTextField(50));
p1.add(b1=new JButton("Open"));
p1.add(b2=new JButton("Close"));
contentPane.add("Center",p
p2.add(l2=new JLabel(" ",SwingConstants.CENTER));
contentPane.add("South",p2
b1.setEnabled(false);
b2.setEnabled(false);
tf1.requestFocus();
tf1.addCaretListener(this)
tf1.addActionListener(this
b1.addActionListener(this)
b2.addActionListener(this)
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
});
}
public void actionPerformed(ActionEven
if (e.getSource()==b1 || e.getSource()==tf1) {
try {
con=DriverManager.getConne
l2.setText("SUCCESS!");
tf1.setEnabled(false);
b1.setEnabled(false);
b2.setEnabled(true);
} catch(SQLException sqle) {
l2.setText("Could not open: "+tf1.getText());
return;
}
} else if (e.getSource()==b2) {
try {
con.close();
} catch(SQLException sqle) {
}
con=null;
tf1.setEnabled(true);
b1.setEnabled(true);
b2.setEnabled(false);
l2.setText("");
}
}
public void caretUpdate(CaretEvent e) {
if (tf1.getText().equals(""))
b1.setEnabled(false);
else
b1.setEnabled(true);
}
public static void main(String[] args) {
SwingApp window = new SwingApp();
try {
Class.forName("sun.jdbc.od
DriverManager.setLogStream
} catch(ClassNotFoundExcepti
System.err.println("Class not found");
return;
}
window.setTitle("Java Application");
window.pack();
window.setVisible(true);
}
}