Hi!
I’m trying to install a CButton in the title bar of a DefaultSingleCDockable. The purpose of the button is to put focus on a JTextField in the dockable.
On the first click after the applications starts, this doesn’t work: the button itself gets the focus, not the textfield. On a second click, the textfield gets the focus.
How can I reliable perform a focus request on first click to a CButton? I’m using dockingframes version 1.1.2p17b.
Here is code that shows the problem:
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import com.jcop.jta.ui.MainWindow;
import bibliothek.gui.dock.action.DockAction;
import bibliothek.gui.dock.common.CControl;
import bibliothek.gui.dock.common.CGrid;
import bibliothek.gui.dock.common.DefaultSingleCDockable;
import bibliothek.gui.dock.common.action.CAction;
import bibliothek.gui.dock.common.action.CButton;
import bibliothek.gui.dock.common.intern.action.CDecorateableAction;
import bibliothek.gui.dock.common.intern.action.CDecorateableActionListener;
public class TestDockButton {
static void setupGUI() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
CControl dockControl = new CControl( frame );
DefaultSingleCDockable frame1 = new DefaultSingleCDockable("frame1", "Frame1");
dockControl.addDockable(frame1);
DefaultSingleCDockable frame2 = new DefaultSingleCDockable("frame2", "Frame2");
Container content2 = frame2.getContentPane();
content2.setLayout(new FlowLayout());
final JTextField field1 = new JTextField(20);
content2.add(field1);
final JTextField field2 = new JTextField(20);
content2.add(field2);
CButton selectField1 = new CButton("Select field1", null);
selectField1.setShowTextOnButtons(true);
selectField1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
field1.requestFocusInWindow();
}
});
frame2.addAction(selectField1);
CButton selectField2 = new CButton("Select field2", null);
selectField2.setShowTextOnButtons(true);
selectField2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
field2.requestFocusInWindow();
}
});
frame2.addAction(selectField2);
dockControl.addDockable(frame2);
frame.getContentPane().add(dockControl.getContentArea(), BorderLayout.CENTER);
CGrid grid = new CGrid(dockControl);
grid.add(0, 0, 100, 100, frame1);
grid.add(0, 0, 100, 100, frame2);
dockControl.getContentArea().deploy(grid);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
setupGUI();
}
});
}
}
Screenshot immediately after applications startup:
After first click (note the small black dots around the button, denoting focus):
After second click: the text field finally gets the focus:
Thanks for your help !
Alex