Reputation: 13
I have two questions and please bear in mind that i am a somewhat newbie with java 1. I have a class that creates a GUI using a JFrame. the JFrame has 2 panels that i added using a JSplitPane
the problem is i can manage to set focus on the desired JPanel, but i want to set the focus on a specific JTextField within this Panel
This is the code that creates the Frame
//Create JFrame
JFrame frame = new JFrame("Testing Frames");
frame.setLocation(100,100);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p1=new JPanel();
p1.add(new First_Panel());
p1.setBackground(Color.RED);
JPanel p2=new JPanel();
p2.add(new Second_Panel());
p2.setBackground(Color.BLUE);
//Create Split JPanel
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, p1, p2 );
splitPane.setOneTouchExpandable(true);
splitPane.setDividerSize(20);
//Pack Frame and show
frame.add(splitPane);
frame.setMinimumSize(new Dimension(1500,600));
frame.pack();
p1.requestFocus();
frame.setVisible(true);
2. Another Issue i have with focusing, within the first panel i have a JTextField that is being started with a partial current day. /MM/yyyy when i tab through the JTextfields and reach this specific field it starts to write after the yyyy, how can i set it that when i tab to this JTextField it will start at the proper point.
Thanks in advance :D
Edit: i add the code for one of the JPanel class.
i removed most of the code and only left the parts that relate to the question.
Focus should be on driver_name JTextField, and the Date JTextFiled is being started with /MM/yyyy and relates to the 2nd question.
public class First_Panel extends JPanel {
//Load Frame Components
JTextField driver_name=new JTextField(10);
JTextField date=new JTextField(10);
public First_Panel() throws ClassNotFoundException, SQLException {
super(new MigLayout("", "[grow][grow][grow][center][grow][grow][grow]", "[][][]"));
//Date Format
DateFormat dateFormat = new SimpleDateFormat("/MM/yyyy");
//Set Date Automatically to Date Field
date.setFont(font);
Calendar cal = Calendar.getInstance();
date.setText(dateFormat.format(cal.getTime()));
//Frame Layout Properties
//Third Column
add(driver_name, "cell 0 2,growx");
driver_name.setColumns(10);
add(date, "cell 2 2,growx");
date.setColumns(10);
send.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
dn=driver_name.getText();
SimpleDateFormat originalFormat = new SimpleDateFormat("dd/MM/yyyy");
SimpleDateFormat targetFormat = new SimpleDateFormat("yyyy/MM/dd");
try {
odate = originalFormat.parse(date.getText());
ndate=(targetFormat.format(odate));
} catch (ParseException ex) {
}
Upvotes: 0
Views: 80
Reputation: 324118
but i want to set the focus on a specific JTextField within this Panel
Well the code you posted doesn't even contain a JTextField so it is going to be hard to set focus on a component that doesn't exist. When you ask a question post a proper SSCCE that demonstrates the problem.
Be default a JPanel is not focusable, so you should add components to the panel that can gain focus.
You are using the wrong method, read the API for the following method:
p1.requestFocus();
The proper method to use is requestFocusInWindow()
. However focus can only be given to a component on a visible frame so the order of the code should be:
frame.setVisible(true);
textField.requestFocusInWindow();
a JTextField that is being started with a partial current day. /MM/yyyy
Again you talk about this mysterious text field that does not appear in the code you posted. My guess is you are not using layout managers properly and you have set the size of the text field incorrectly so the entire text doesn't display properly.
If you need more help then post a proper SSCCE
. A SSCCE should be included with all questions you ask, since the problem will always be with the code you write and if we can't see the code we can't always guess what you might be doing.
Upvotes: 3