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]
# 1

Did you try textField1.requestFocus()?

If you did you will have to post a Short, Self Contained, Compilable and Executable, Example Program ([url http://homepage1.nifty.com/algafield/sscce.html]SSCCE[/url]) that demonstrates the problem.

Because it's working fine for me.

And don't forget to use the code tags when posting code.

Rodney_McKaya at 2007-7-9 5:43:19 > top of Java-index,Desktop,Core GUI APIs...
# 2
You can only request focus on a component when the GUI is visible.
camickra at 2007-7-9 5:43:19 > top of Java-index,Desktop,Core GUI APIs...
# 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

ushradhaa at 2007-7-9 5:43:19 > top of Java-index,Desktop,Core GUI APIs...
# 4

> This is my compilable,executable dummy code

It doesn't compile

a) no import statements

b) missing ObjectSearchMainPanel class

The code was not formatted as requested.

> Please llet me know what can be done to get the focus in textfield as soon as window loads.

Use a WindowListener. Handle windowActivated, or WindowOpened event.

camickra at 2007-7-9 5:43:19 > top of Java-index,Desktop,Core GUI APIs...
# 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

ushradhaa at 2007-7-9 5:43:19 > top of Java-index,Desktop,Core GUI APIs...
# 6
The code is still not formatted. Learn to use the forum correctly if you want help.
camickra at 2007-7-9 5:43:19 > top of Java-index,Desktop,Core GUI APIs...
# 7
Thank you .Window listener solved the probem.Thank you again for your suggestion frame.addWindowListener(new WindowAdapter(){public void windowOpened(WindowEvent e){textField1.requestFocus();}});
ushradhaa at 2007-7-9 5:43:19 > top of Java-index,Desktop,Core GUI APIs...