• Post Reply Bookmark Topic Watch Topic
  • New Topic

JDialog: How to set focus on second JButton?  RSS feed

 
Tom Rodrigo
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I tried for quite a few hours to get the focus on my 'Cancel'-button in a JDialog.
getRootPane().setDefaultButton(jBtnCancel);
jBtnCancel.requestFocus();
transferFocus();
didn�t work. Focus stays on the first and only other button ('Ok') at the startup.
I would like to avoid using listeners as I am new to Java / Swing.
Any ideas?
TIA
Tom
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a simple example that works fine:
 
Tom Rodrigo
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul,
thanks for your example. I already tried this, but it doesn�t work. Maybe because of this:
I call up a JFrame in which one can click on a JButton. Doing this a dialog is displayed showing let�s say
1 button. By clicking this button the Ok / Cancel - Dialog is displayed and the focus is set on 'Ok'.
This dialog is specified in a different class (MsgDialog) than the frame and than the other 1-button-dialog.
I am using this class to create 'Ok' or 'Ok' / 'Cancel' - Dialogs at several other places in my applet.
Maybe the focus get�s lost on its way through the different classes, frames, dialogs etc.? But I have had
inserted all kinds of requestFocus() methods at all possible places without success...
Still any ideas?
Tom
_________________________________________________________________________
//the MsgDialog - class
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.Border;
public class MsgDialog extends myDialog { //myDialog sets up the frame size
boolean cancelSelected = false;
JPanel contentPane = new JPanel();
JPanel DummiePanel = new JPanel();
JPanel MainPanel = new JPanel();
JPanel ButtonPanel = new JPanel();
JButton jBtnOk = new JButton();
JButton jBtnCancel = new JButton();
JLabel jLblMessage = new JLabel();
JLabel jLblMessage2 = new JLabel();
BorderLayout borderLayout = new BorderLayout();
public MsgDialog(boolean drawCancelButton) {
try {
jbInit(drawCancelButton);
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit(boolean drawCancelButton) throws Exception {
this.setModal(true);
this.setSize(280, 110);
this.setResizable(false);
this.getContentPane().add(contentPane, null);
//MainPanel
contentPane.setLayout(borderLayout);
contentPane.add(DummiePanel, BorderLayout.NORTH);
contentPane.add(MainPanel, BorderLayout.CENTER);
contentPane.add(ButtonPanel, BorderLayout.SOUTH);
//SubPanels
MainPanel.add(jLblMessage, null);
MainPanel.add(jLblMessage2, null);
ButtonPanel.add(jBtnOk, null);
//Button 'OK'
jBtnOk.setText("OK");
jBtnOk.setPreferredSize(new Dimension(53, 25));
jBtnOk.setBorder(BorderFactory.createRaisedBevelBorder());
//'OK' - Event
jBtnOk.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == jBtnOk) {
dispose();
}
}
});
if (drawCancelButton) {
this.setDefaultCloseOperation(0);
ButtonPanel.add(jBtnCancel, null);
//Button 'Cancel'
jBtnCancel.setText("Cancel");
jBtnCancel.setPreferredSize(new Dimension(53, 25));
jBtnCancel.setBorder(BorderFactory.createRaisedBevelBorder());
//'Cancel' - Event
jBtnCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == jBtnCancel) {
cancelSelected = true;
dispose();
}
}
});
}
}
public boolean cancelSelected() {
return cancelSelected;
}
}
_________________________________________________________________________
//the 1-button-dialog:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class myInfo extends myDialog {
JPanel contentPane = new JPanel();
BorderLayout borderLayout = new BorderLayout();
JButton jBtn1 = new JButton();
public myInfo() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
this.setContentPane(contentPane);
this.setResizable(false);
this.setSize(new Dimension(570, 305));
contentPane.setLayout(borderLayout);
contentPane.add(jBtn1, BorderLayout.SOUTH);
jBtn1.setText("1");
jBtn1.setActionCommand("jBtn1");
jBtn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
MsgDialog msgDlg = new MsgDialog(true);
msgDlg.setTitle("myTitle");
msgDlg.setSize(280, 130);
msgDlg.jLblMessage.setText("First Line");
msgDlg.jLblMessage2.setText("Second Line");
msgDlg.centerFrame();
msgDlg.show();
}
});
}
}
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does your myDialog class look like?
 
Tom Rodrigo
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Paul,
here comes the 'myDialog' - class:
Tom
import java.awt.*;
import javax.swing.JDialog;
public class myDialog extends JDialog {
public myDialog() {
}
void centerFrame () {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = this.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
this.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
}
}
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Setting it as the default button works. Still playing with the focus thing.
 
Tom Rodrigo
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul,
I don�t even get the default button set up on cancel. Tried again for some time but neither default button nor focus on cancel works.
So how to set up the default button ?
I am on line for the next 4 hours; after that I am on VACATIONS (!) until january 7th. So don�t wonder if there will be no response the next days.
Merry Christmas to you & thanks for your help, if we don�t read from each other in the next couple of hours!
Tom
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just added getRootPane().setDefaultButton(jBtnCancel); before you set up the action listener. The focus is on the OK button still but if you hit enter the cancel button executes.
 
Tom Rodrigo
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So do I with no effect.
But I still need it together with the focus, so standing alone it�s not of that much importance.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!