[an error occurred while processing this directive]
Domain for sale!
Start Search Contents Index Links About
OptionsDialog.java
/*
 * Author: Havard Rast Blok
 * E-mail: 
 * Web   : www.rememberjava.com
 */

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;

public class OptionsDialog extends JDialog implements ChangeListener, ActionListener
{
  private Properties props;

  public OptionsDialog()
  {
    Container c;
    JTabbedPane tabs;
    JPanel p1, p2;
    JButton ok;

    props = new Properties();

    //set up basic GUI
    p1 = makeGameOptions();
    p2 = makeFileOptions();
    tabs = new JTabbedPane();
    tabs.addTab("Game", p1 );
    tabs.addTab("File", p2 );
    
    ok = new JButton("OK");
    ok.setName"ok" );
    ok.addActionListenerthis );

    c = getContentPane();
    c.setLayoutnew BorderLayout() );
    c.add(tabs, BorderLayout.CENTER);
    c.add(ok, BorderLayout.SOUTH);

    pack();
  }

  private JPanel makeGameOptions()
  {
    JPanel ans;
    JLabel l1, l2;
    JSpinner spinner;
    JComboBox cb;

    //create components...
    ans = new JPanelnew GridLayout2) );
    l1 = new JLabel"Speed" );
    l2 = new JLabel"Rules" );
    spinner = new JSpinner();
    cb = new JComboBoxnew String[]{ "Play by the rules""No rules" } );

    //set names
    spinner.setName"speed" );
    cb.setName"rules" );

    //add change listeners
    spinner.addChangeListenerthis );
    cb.addActionListenerthis );

    //.. and add them
    ans.add(l1);
    ans.add(spinner);
    ans.add(l2);
    ans.add(cb);

    return ans;
  }

  private JPanel makeFileOptions()
  {
    JPanel ans;
    JCheckBox check;
    JLabel l1;
    JSlider slider;

    //create components...
    ans = new JPanelnew GridLayout3) );
    check = new JCheckBox"Autosave files" );
    l1 = new JLabel"File size limit" );
    slider = new JSlider0);

    //set names
    check.setName"autosave" );
    slider.setName"fileLimit" );

    //add change listeners
    check.addChangeListenerthis );
    slider.addChangeListenerthis );

    //and add
    ans.addcheck );
    ans.addl1 );
    ans.addslider );

    return ans;
  }

  private void getEventValueEventObject e )
  {
    Object src;
    String value, name;
    
    src = e.getSource();
    name = ((Component)src).getName();
    value="";

    //Methods to retrieve values from the various classes:
    // AbstractButton - isSelected()
    // JSlider - getValue() 
    // JSpinner - getValue() (Object) a Number for the NumberModel
    // JComboBox - getSelectedIndex()

    ifsrc instanceof JButton && name.equals("ok") )
    {
      //special case - user press ok button
      //print properties and exit
      try
      {
  props.storeSystem.out, "Settings from OptionsDilog" );
      }
      catchException ex )
      {
  System.out.println"getEventValue - Error printing properties: "+ex );
      }
      
      System.exit(0);
    }
    else ifsrc instanceof AbstractButton )
    {
      value = ""+((AbstractButton)src).isSelected();
    }
    else ifsrc instanceof JSlider )
    {
      value = ""+((JSlider)src).getValue();
    }
    else ifsrc instanceof JSpinner )
    {
      value = ""+((JSpinner)src).getValue();
    }
    else ifsrc instanceof JComboBox )
    {
      value = ""+((JComboBox)src).getSelectedIndex();
    }
    
    System.out.println"name: "+name+", value="+value );
    props.setPropertyname, value );
  }

  //method from ChangeListener
  public void stateChanged(ChangeEvent e
  {
    getEventValue(e);
  }

  //method from ActionListener
  public void actionPerformedActionEvent e )
  {
    getEventValue(e);
  }  

  public static void mainString args[])
  {
    //show the dialog
    JDialog d = new OptionsDialog();
    d.show();
  }
}


site: Håvard Rast Blok
mail:
updated: 16 July 2010