Skip to main content

[JAVA2D] setSelected() method

1 reply [Last post]
Anonymous

The following issue im having is very small but extremely annoying and i cant figure why it wont work. I Create a frame within another frame and would like the smaller second frame to grab the focus (be selected) as soon as it is created. I use the method setSelected() in another one of my classes and it works perfectly, but here it does not...If anyone might be able to tell me what might be the problem, that would be great...

The code for my class is as follows: The problem is that openigFrame does not appear selected when created, it must be clicked upon to get the focus....

public class MainProgramFrame extends JFrame

{

private JDesktopPane desktop;

private JInternalFrame openingFrame;

private JRadioButton newButton;

private JRadioButton openButton;

int i = 1;

public static void main(String arg[]){

MainProgramFrame mpf = new MainProgramFrame();

mpf.show();

}

public MainProgramFrame()

{

super("GATACA");

this.setSize(900,650);

this.setLocation(50,30);

this.setBackground(Color.gray);

try {

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

} catch (Exception e) { }

init();

}

public void init()

{

desktop = new JDesktopPane();

desktop.setBackground(Color.gray);

setContentPane(desktop);

createGUI();

}

public void createGUI() //throws PropertyVetoException

{

AppMenuBar AppMenuBar = new AppMenuBar();

setJMenuBar((JMenuBar)AppMenuBar);

openingFrame = new JInternalFrame("Welcome to GATACA", true, true, true, true);

JPanel newPanel = new JPanel();

newButton = new JRadioButton("Create a new experiment:");

JPanel openPanel = new JPanel();

openButton = new JRadioButton("Open existing experiment:");

ButtonGroup gp = new ButtonGroup();

gp.add(newButton);

gp.add(openButton);

newPanel.add(newButton);

openPanel.add(openButton);

openingFrame.getContentPane().setLayout(new GridLayout(3,1));

openingFrame.getContentPane().add(newPanel);

openingFrame.getContentPane().add(openPanel);

desktop.add(openingFrame);

openingFrame.setSize(500,150);

openingFrame.setLocation(200,50);

openingFrame.setBackground(Color.lightGray);

openingFrame.setVisible(true);

openButton.setSelected(true);

try{

openingFrame.setSelected(true);

}catch (PropertyVetoException e) {}

JPanel buttonPanel = new JPanel();

JButton okButton = new JButton("Ok");

buttonPanel.add(okButton);

okButton.addActionListener(

new ActionListener() {

public void actionPerformed(ActionEvent e) {

if (openButton.isSelected())

{

createExperiment();

//openingFrame.hide();

openingFrame.setVisible(false);

repaint();

}

else if (newButton.isSelected())

{

}

}

}

);

JButton cancelButton = new JButton("Cancel");

cancelButton.setSize(1,1);

buttonPanel.add(cancelButton);

JSeparator sep = new JSeparator();

buttonPanel.add(sep);

openingFrame.getContentPane().add(buttonPanel);

}

public class AppMenuBar extends JMenuBar

{

Vector menus = new Vector();

public AppMenuBar(){ createMenus();}

public void createMenus(){

createFileMenu();

}

public JMenu createFileMenu()

{

JMenu fileMenu = new JMenu("Experiment");

JMenuItem open = new JMenuItem("Open...");

open.addActionListener(

new ActionListener(){

public void actionPerformed(ActionEvent e){

createExperiment();

}

}

);

fileMenu.add(open);

add(fileMenu);

return fileMenu;

}

}

public void createExperiment()

{

ImageManip2D4Two im = new ImageManip2D4Two();

im.setSize(780,400);

im.initializeIM(i);

desktop.add(im);

im.moveToFront();

im.setVisible(true);

try{

im.setSelected(true);

}catch (PropertyVetoException e) {}

i++;

}

}

*****************************************************************************

David Braun-Friedman

Programador de Telecomunicaciones

Departamento de Electr�nica y Comunicaciones

Email (CEIT): dbraun-friedman@ceit.es

Email (Fuera): dbra5506@postoffice.uri.edu

Tlfno: 943 212 800 Extensi�n: 289

M�vil: 628527739

Centro de Estudios e Investigaciones T�cnicas C.E.I.T.

Paseo Manuel de Lardizabal, 15
20018 San Sebastian

*****************************************************************************

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".
[att1.html]
[image001.jpg]

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Rosenstrauch, David

-----Original Message-----
From: Discussion list for Java 2D API [mailto:JAVA2D-INTEREST@JAVA.SUN.COM]On
Behalf Of Braun-Friedman, David
Sent: Wednesday, July 07, 2004 6:37 AM
To: JAVA2D-INTEREST@JAVA.SUN.COM
Subject: [JAVA2D] setSelected() method

The following issue im having is very small but extremely annoying and i cant
figure why it wont work. I Create a frame within another frame and would like
the smaller second frame to grab the focus (be selected) as soon as it is
created. I use the method setSelected() in another one of my classes and it
works perfectly, but here it does not...If anyone might be able to tell me
what might be the problem, that would be great...

The code for my class is as follows: The problem is that openigFrame does not
appear selected when created, it must be clicked upon to get the focus....

[Rosenstrauch, David]

The following should work. After you create openigFrame, do this:

openigFrame.addComponentListener(
new ComponentAdapter() {
public void componentShown(ComponentEvent e) {
openigFrame.requestFocus();
}
}
);

DR

==============================================================================
This message is for the sole use of the intended recipient. If you received
this message in error please delete it and notify us. If this message was
misdirected, CSFB does not waive any confidentiality or privilege. CSFB
retains and monitors electronic communications sent through its network.
Instructions transmitted over this system are not binding on CSFB until they
are confirmed by us. Message transmission is not guaranteed to be secure.
==============================================================================

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".
[att1.html]