aspose file tools*
The moose likes Java in General and the fly likes Passing values back from inner classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Passing values back from inner classes" Watch "Passing values back from inner classes" New topic
Author

Passing values back from inner classes

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi there:
I have a program using actionlistener to create a non-modal dialog (pop-up window).
the pop-up window display a Jcombobox to select some variables, but I can not pass the value back
to the parent window
here is an extract of the code:
String s_alr1 = null;
.
.
.
// pmc button
gbc.gridx = 2;
pmcButton = new JButton("PMC");
topPanel.add(pmcButton,gbc);
pmcButton.addActionListener( new ActionListener()
{
public void actionPerformed( ActionEvent e )
{
showpmc();
}
} );

.
.
.
.
.

/** Method which builds and launches a non-modal dialog */
public void showpmc ()
{
JPanel panel0 = new JPanel(new GridLayout(7,1));
JPanel panel1 = new JPanel(new FlowLayout());
JPanel panel2 = new JPanel(new FlowLayout());
JPanel panel3 = new JPanel(new FlowLayout());
JPanel panel4 = new JPanel(new FlowLayout());
JPanel panel5 = new JPanel(new FlowLayout());
JPanel panel6 = new JPanel(new FlowLayout());
JPanel panel7 = new JPanel(new FlowLayout());
dialog = new JDialog();
String[] listmdc = { "MDC","MDC1","MDC2","MDC3"};
mdc1 = new JComboBox(listmdc);
mdc1.setEditable(false);
mdc1.setSelectedIndex(0);
panel1.add(mdc1);
mdc2 = new JComboBox(listmdc);
mdc2.setEditable(false);
mdc2.setSelectedIndex(0);
panel2.add(mdc2);
.
.
.
.
.
okButton = new JButton("Ok");
cancelButton = new JButton("Cancel");
panel7.add(okButton);
panel7.add(cancelButton);
okButton.addActionListener( new ActionListener()
{
public void actionPerformed( ActionEvent e )
{
addmdc();

// this shows a null value
textArea.append("!!!back from addalr" + s_alr1 + "\n");
dialog.setVisible(false);
dialog.dispose();
}
} );

cancelButton.addActionListener( new ActionListener()
{
public void actionPerformed( ActionEvent e )
{
dialog.setVisible(false);
dialog.dispose();
}
} );
panel0.add(panel1);
panel0.add(panel2);
panel0.add(panel3);
panel0.add(panel4);
panel0.add(panel5);
panel0.add(panel6);
panel0.add(panel7);
dialog.setContentPane(panel0);
dialog.setLocation(300,300);
dialog.setSize(300,100);
dialog.pack();
dialog.setVisible(true);
}

public void addmdc( )
{
// this comment display OK
textArea.append("!!! in addmdc\n");
alr1.addItemListener( new ItemListener()
{
public void itemStateChanged( ItemEvent e )
{
if( e.getStateChange() == ItemEvent.SELECTED )
{
s_alr1 = (String)e.getItem()+",";

// this comment never shows......
textArea.append("!!! alr1 selected" + s_alr1 + "\n");


}
}
}
);
.
.
.
.
}
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi,
I am not exactly sure that I can help you because I don't understand from your code where things are. From you topic label I am guessing that you have created an inner class that is the popup window. Since non-static inner classes can access outer member variables just use one to get the information. Have your inner class 'close/ok/actionPerformed' routine call some parent class method to then use the member variable.
If your popup window is not an inner class then you need to use some AWTEvent extension so that your parent window could listen to the child window and then perform something being given the event structure with user information (chosen items from child window).
Regards,
Manfred.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Passing values back from inner classes