• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Hard to explain. Deals with frames and Action Listener

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Heres my problem: The getChoice method (OptionDialogFrame1 class) is called before i can select a radio button. How can i make it so i have time to choose a radio button before the value is returned? The below code compiles correctly, you can copy/paste it into a code writing program.

public class IronTomRed {

public static void main(String[] args) {

// Create application frame.
IronTomRedFrame frame = new IronTomRedFrame();

// Show frame.
frame.show();
}
}

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;

import javax.swing.*;
import javax.swing.border.*;
import java.io.*;

/**
* @(#)IronTomRed.java
*
* JFC Sample application
*
* @author
* @version 1.00 06/04/05
*/
public class IronTomRedFrame extends JFrame{
String choice = "x";
/**
* The constructor.
*/
public IronTomRedFrame() {

BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
JMenuBar menuBar = new JMenuBar();
JMenu menuFile = new JMenu();
JMenu menuHelp = new JMenu();
JMenuItem menuFileExit = new JMenuItem();
JMenuItem menuFileNew = new JMenuItem();
JMenuItem menuFileLoad = new JMenuItem();
JMenuItem menuHelpHelp = new JMenuItem();
JMenuItem menuHelpAbout = new JMenuItem();

//icon
Toolkit kit = Toolkit.getDefaultToolkit();
Image img = kit.getImage("G:\\IronTomRed\\src\\icon.gif");
setIconImage(img);


menuFile.setText("File");
menuHelp.setText("Help");
menuFileNew.setText("New Game");
menuFileLoad.setText("Load Game");
menuFileExit.setText("Exit");
menuHelpHelp.setText("Instructions");
menuHelpAbout.setText("About");

// Add action listener.for the menu button
menuFileExit.addActionListener
(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
IronTomRedFrame.this.windowClosed();
}
}
);
menuFileNew.addActionListener
(
new ActionListener() {
public void actionPerformed(ActionEvent e) {

String userName = JOptionPane.showInputDialog("Enter Ye Name, Mate!");
int island=0;

String islandLocation="x";
int villageLocation=0;
int harborLocation=0;
int islandLodgingOption=0;
int pubOption=0;
JOptionPane.showMessageDialog(null,"Story Frame 1","What to do with your scummy self...",JOptionPane.PLAIN_MESSAGE,new ImageIcon("G:\\IronTomRed\\src\\icon.gif"));
JOptionPane.showMessageDialog(null,"Story Frame 2","What to do with your scummy self...",JOptionPane.PLAIN_MESSAGE,new ImageIcon("G:\\IronTomRed\\src\\icon.gif"));
JOptionPane.showMessageDialog(null,"Story Frame 3","What to do with your scummy self...",JOptionPane.PLAIN_MESSAGE,new ImageIcon("G:\\IronTomRed\\src\\icon.gif"));
ButtonPanel islandOneOptions = new ButtonPanel("Island 1 Options!",new String[]{"Shipyard","Village"});
OptionDialogFrame1 islandOne = new OptionDialogFrame1("Welcome To Island One",islandOneOptions);
islandOne.show();
islandLocation = islandOne.getChoice();
if(islandLocation.equals("Village")){
ButtonPanel villageOptions = new ButtonPanel("Village Options!",new String[]{"Pub","Blacksmith","Stupidity"});
OptionDialogFrame1 villageOne = new OptionDialogFrame1("Welcome to the village!",villageOptions);
villageOne.show();
islandOne.hide();
}
if(islandLocation.equals("Shipyard")){
//ButtonPanel villageOptions = new ButtonPanel("Shipyard Options!",new String[]{"xxx","xxx","Stupidity"});
//OptionDialogFrame villageOne = new OptionDialogFrame("Welcome to the village!",villageOptions);
//villageOne.show();
//islandOne.hide();
}
System.out.println(islandLocation);
}


}
);
menuFileLoad.addActionListener
(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
//old game loaded
}
}
);
menuHelpHelp.addActionListener
(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null,"Instructions for yon game.","What to do with your scummy self...",JOptionPane.PLAIN_MESSAGE,new ImageIcon("G:\\IronTomRed\\src\\icon.gif"));
}
}
);
menuHelpAbout.addActionListener
(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null,"Written by Benson "+'\n'+"Cameron "+'\n'+"Ben "+'\n'+"and Kenneth, the First Jew-Pirate ever Invented","Pay homage to thine infinitely betters...",JOptionPane.PLAIN_MESSAGE,new ImageIcon("G:\\IronTomRed\\src\\icon.gif"));
}
}
);
menuFile.add(menuFileNew);
menuFile.add(menuFileLoad);
menuFile.add(menuFileExit);

menuHelp.add(menuHelpHelp);
menuHelp.add(menuHelpAbout);

menuBar.add(menuFile);
menuBar.add(menuHelp);

setTitle("Iron Tom Red's 7th Sojourn II");
setJMenuBar(menuBar);
setSize(new Dimension(400, 400));

// Add window listener.
this.addWindowListener
(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
IronTomRedFrame.this.windowClosed();
}
}
);
}

/**
* Shutdown procedure when run as an application.
*/
protected void windowClosed() {

// TODO: Check if it is safe to close the application

// Exit application.
System.exit(0);
}
}
class ButtonPanel extends JPanel{
/*
*helper method
*makes a new optionpanel
*/
public ButtonPanel(String title, String[] options){
setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),title));
setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
group = new ButtonGroup();
for(int i=0;i<options.length;i++){
JRadioButton b = new JRadioButton(options[i]);
b.setActionCommand(options[i]);
add(b);
group.add(b);
b.setSelected(i==0);
int length = options.length;
}
}
public String getSelection(){
return group.getSelection().getActionCommand();
}
private ButtonGroup group;
}
class OptionDialogFrame1 extends JFrame{
String choice = "s";
boolean pressed = false;
public OptionDialogFrame1(String title, final ButtonPanel buttons){

System.out.println(choice);
setTitle(title);
setSize(200,200);
JPanel optionPanel = new JPanel();
JButton ok = new JButton("OK");
optionPanel.add(buttons);
optionPanel.add(ok);
optionPanel.setVisible(true);
ButtonPanel villageOptions = new ButtonPanel("Village Options!",new String[]{"Pub","Blacksmith","Stupidity"});

if(choice.equals("Village"))
optionPanel.add(villageOptions);
Container contentPane = getContentPane();
contentPane.add(optionPanel, BorderLayout.CENTER);
contentPane.setVisible(true);
ok.addActionListener
(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
choice = buttons.getSelection();

}
}
);
}

public String getChoice(){

return choice;
}
}
[ May 08, 2006: Message edited by: Cameron Schultz ]
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> class OptionDialogFrame1 extends JFrame{

change this so it extends JDialog and set its modal state to true
Note: because of your actionListener, do not use super(..,true)

this is the order in the constructor

ok.addActionListener...
setModal(true);
contentPane.setVisible(true);
}//end of constructor
 
Straws are for suckers. Now suck on this tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic