Cant get Focus on textfield
Hi
I have a class extending JFrame say "MainFrame" which consist of JSplitPane say "splitPaneA"
splitPaneA has top and bottom component.
TopComponent is another JsplitPane say "splitPaneA-B"
splitPaneA-B consist of a Jpanel say "B-rightPanel "as RightComponent and another JPanel as left.
splitPaneA has bottomcoponent which is Jpanel class say "BottomPanel " extendingJPanel
splitPaneA .setABottomComponent(new BottomPAnel());
BottomPanel has JtoolBar say toolBar
ToolBar has a JtextFiled say textField1 which needs to be focused as soon as bottomPanel loads.
I tried using following in BottomPanel class
this.setEnabled(true);
this.setFocusable(true);
this.setVisible(true);
this.requestFocusInWindow();
textField1.requestFocusInWindow()
But textFiled never gets focused.I also tried requestFocus method.
In class MainFrame
FocusManager.getCurrentManager().getFocusOwner()); always returns null
(FocusManager.getCurrentManager().getFocusedWindow()); returns null
this.hasFocus()); returns false
Any help is greatly appreciated.
[1158 byte] By [
ushradhaa] at [2007-11-26 18:10:51]

# 3
Thanks for your responses.
This is my compilable,executable dummy code.I have taken out lines which are not relevant to focus issue as well as the import.Please llet me know what can be done to get the focus in textfield as soon as window loads.
public class DummyObjectSearchPanel extends JPanel implements ActionListener
{
JTextField textField1;
JButton searchButton;
ImageIcon searchIconIcon;
JToolBar jToolBar1;
private static Icon closeIcon = new ImageIcon(ObjectSearchMainPanel.class.getResource("/resources/images/close_small.gif"));
private JButton closeButton;
JFrame frame;
private String newline = "\n";
public DummyObjectSearchPanel()
{
this.setLayout(new BorderLayout());
this.setBorder(new ShadowBorder());
textField1 = new JTextField();
this.setEnabled(true);
this.setFocusable(true);
this.requestFocusInWindow();
this.setVisible(true);
textField1.requestFocus();
System.out.println("has focus " + textField1.hasFocus());
jToolBar1 = new JToolBar();
frame = new JFrame();
frame.setUndecorated(true);
jbInit();
}
void jbInit()
{
searchButton = new JButton();
searchButton.setText("SearchButton");
closeButton = new JButton(closeIcon);
ActionListener al = new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
textField1.requestFocusInWindow();
}
};
textField1.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent e)
{
searchButton_actionPerformed(e);
}
});
jToolBar1.setBorder(null);
jToolBar1.addSeparator();
jToolBar1.add(closeButton);
closeButton.setOpaque(false);
closeButton.setMargin(new Insets(4, 4, 4, 4));
Dimension closeBtnDimension = new Dimension(20, 20);
closeButton.setPreferredSize(closeBtnDimension);
closeButton.setMinimumSize(closeBtnDimension);
closeButton.setSize(closeBtnDimension);
closeButton.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
//closeButton action taken out to make code short
}
});
jToolBar1.addSeparator();
jToolBar1.addSeparator();
jToolBar1.add(textField1);
jToolBar1.addSeparator();
jToolBar1.add(searchButton);
jToolBar1.addSeparator();
jToolBar1.addSeparator();
this.add(jToolBar1);
searchButton.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent e)
{
searchButton_actionPerformed(e);
}
});
textField1.requestFocusInWindow();
}
void searchButton_actionPerformed(ActionEvent e)
{
//searchbutton logic removed to simplify the code
}
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("New Search");
Dimension d=new Dimension(600,100);
frame.setSize(d);
frame.setMinimumSize(d);
frame.setPreferredSize(d);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
//Create and set up the content pane.
JComponent newContentPane = new DummyObjectSearchPanel();
newContentPane.setOpaque(true); //content panes must be opaque
frame.getContentPane().add(new DummyObjectSearchPanel(),
BorderLayout.CENTER);
//Display the window.
//frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
UIManager.put("swing.boldMetal", Boolean.FALSE);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
}
}
Thanks you so much
# 5
Please copy paste following import.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.*;
import javax.swing.BorderFactory;
import javax.swing.DefaultFocusManager;
import javax.swing.FocusManager;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.SpringLayout;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
2.Please remove following line
private static Icon closeIcon = new ImageIcon(ObjectSearchMainPanel.class.getResource("/resources/images/close_small.gif"));
code will compile fine.Thanks for your suggestion on window opened event
.Iam going totry implement this now