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 javax.swing.*;
import javax.swing.event.*;

public class OptionsDialog extends JDialog implements ChangeListener, ActionListener
{
  public OptionsDialog()
  {
    JTabbedPane tabs;
    JPanel p1, p2;

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

    getContentPane().add(tabs);
    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" } );

    //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);

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

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

    return ans;
  }

  //method from ChangeListener
  public void stateChanged(ChangeEvent e
  {
    System.out.println"stateChanged: "+e);
    
  }

  //method from ActionListener
  public void actionPerformedActionEvent e )
  {
    System.out.println"actionPerformed: "+e);
  }  

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


site: Håvard Rast Blok
mail:
updated: 27 July 2007