Results 1 to 6 of 6
  1. #1
    SVENeRGy1 is offline Member
    Join Date
    Mar 2013
    Posts
    3
    Rep Power
    0

    Default Problem with actionlisteners en changelisteners

    Hello,

    I allready posted the question in a dutch forum but I got not a single answer, maybe this forum (wich has more users) wil be the answer of my question.

    I making something that uses the MVC pattern.

    first of al the model:

    this is 1 class (and 1 class for an exception)

    Java Code:
    /**
     * 
     */
    package testprogram.model;
    
    
    /**
     * @author ikke
     *
     */
    public class TestModel {
    	
    	private int amountOfPeople;
    	private int maxAge;
    	private String uit;
    	
    	public TestModel(int m, int l) throws TooSmallException {  
    		if (m < 1) {
    			throw new TooSmallException("It must be at least 1.");
    		}
            
    		if (l <= 0)
    		{
    			throw new TooSmallException("Age must be greater then 0.");
    		}
            
    		this.amountOfPeople = m;
    		this.maxAge = l;
    	}
    	
    	public void GenereerData () {	
    		this.uit = "There are " + this.amountOfPeople + " people, with a maximum age of " + this.maxAge + " year";
    		System.out.println(this.uit);
    	}
    	
    	public void setAantalMensen(int m) throws TooSmallException {	
    		if (m < 1) {
    			throw new TooSmallException("It must be at least 1.");
    		}
    		this.amountOfPeople = m;		
    	}
    	
    	public int getAantalMensen() {
    		return this.amountOfPeople;
    	}
    	
    	public void setMaxLeeftijd (int l) throws TooSmallException {	
    		if (l <= 0)
    		{
    			throw new TooSmallException("age must be greater then 0.");
    		}
    		this.maxAge = l;
    	}
    	
    	public int getMaxLeeftijd() {
    		return this.maxAge;
    	}
    
    	public String Oplossing() {
    		return this.uit;
    	}
    
    	
    	/**
    	 * Commandline version
    	 * @param args
    	 * @throws TooSmallException 
    	 */
    	public static void main(String[] args) throws TooSmallException {
    		
    		TestModel test = new TestModel(25, 60);
    		test.GenereerData();
    	}
    
    }
    the exception class:
    Java Code:
    package testprogram.model;
    
    public class TooSmallException extends Exception {
    	
    	/**
    	 * Constructor voor een NegativeValueException.
    	 * @param errorTekst een String met een pasende omschrijving
    	 * van de situatie.
    	 */
    	public TooSmallException(String errorTekst){
    		super(errorTekst);
    	}	
    }
    That about the model, in the TestModel class there is a Main class that works for the command line.

    Now the views.
    The views are 3 seperate panels that come together on a frame. These are 4 seperate classes, and they must stay seperate.

    The classes are:
    ButtonPanel: This class puts 2 buttons on a JPanel (1 for generate, and 1 for cancel)
    SpinnerPanel: This class puts 2 spinners on a JPanel
    OutputPanel : this class shows the output on a textarea
    MainFrame: the 3 panels come together on a JFrame, this class holds also the main class for the gui.

    Java Code:
    /**
     * 
     */
    package testprogram.view;
    
    import javax.swing.JButton;
    import javax.swing.JPanel;
    
    import testprogram.controllers.Exitlistener;
    
    
    /**
     * @author ikke
     * Class that puts 2 buttons on a panel.
     *
     */
    public class ButtonPanel extends JPanel {
    	
    	//aanmaken van de knoppen
    	private JButton btnGenerate;
    	private JButton btnCancel;
    	
    	public ButtonPanel() {
    		
    		//declareren van de knoppen
    		this.btnGenerate = new JButton("Genereren");
    		this.btnCancel = new JButton("Cancel");
    		
    		//toevoegen aan de panel
    		this.add(this.btnGenerate);
    		this.add(this.btnCancel);
    		
    		//listener van de Cancel knop
    		this.btnCancel.addActionListener(new Exitlistener());
    	}
    }
    Java Code:
    /**
     * 
     */
    package testprogram.view;
    
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JSpinner;
    import javax.swing.SpinnerNumberModel;
    
    /**
     * @author ikke
     * Class that makes 2 spinners
     *
     */
    public class SpinnerPanel extends JPanel {
    
    	//aanmaken van de spinners
    	public JSpinner spnrPeople;
    	public JSpinner spnrAge;
    	
    	//aanmaken van de labels
    	private JLabel lblPeople;
    	private JLabel lblAge;
    	
    	public SpinnerPanel() {
    		
    		//declareren van de spinners met bijhorende labels en instellen van de spinners
    		this.lblPeople = new JLabel("number of people");
    		this.lblAge = new JLabel("Maximum age");
    		this.spnrPeople = new JSpinner();
    		this.spnrAge = new JSpinner();
    		this.spnrPeople.setModel(new SpinnerNumberModel(100, 1, 1000, 1));
    		this.spnrAge.setModel(new SpinnerNumberModel(60, 0, 120, 1));
    		
    		//toevoegen aan de panel
    		this.add(this.lblPeople);
    		this.add(this.spnrPeople);
    		this.add(this.lblAge);
    		this.add(this.spnrAge);	
    	}	
    }
    Java Code:
    /**
     * 
     */
    package testprogram.view;
    
    import javax.swing.JPanel;
    import javax.swing.JTextArea;
    
    /**
     * @author ikke
     * Class that shows the output in a textarea
     *
     */
    public class OutputPanel extends JPanel {
    	
    	private JTextArea output;
    	
    	public OutputPanel() {
    		
    		//declareren van de textarea
    		this.output = new JTextArea("Here must be the string that gets generated.");
    		
    		//toevoegen aan de panel
    		this.add(this.output);
    		
    	}
    
    }
    Java Code:
    /**
     * 
     */
    package testprogram.view;
    
    import java.awt.BorderLayout;
    
    import javax.swing.JFrame;
    
    
    /**
     * @author ikke
     * Class that puts the 3 JPanels on a JFrame
     *
     */
    public class MainFrame extends JFrame {
    	
    	//atributen aanmaken
    	private ButtonPanel buttons = new ButtonPanel();
    	private SpinnerPanel spinners = new SpinnerPanel();
    	private OutputPanel uit = new OutputPanel();
    	
    	public MainFrame(String titel) {
    		
    		super(titel);
    		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    		this.setLocation(200, 200);
    		
    		//toevoegen van de panels
    		this.add(this.buttons, BorderLayout.SOUTH);
    		this.add(this.uit, BorderLayout.CENTER);
    		this.add(this.spinners, BorderLayout.NORTH);
    		
    		//alles samenvoegen en ervoor zorgen dat er aan de frame niets veranderd kan worden
    		this.pack();
    		this.setResizable(false);
    		this.setVisible(true);
    	}
    	
    	
    	
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		new MainFrame("This is just an example.");
    	}
    
    }
    The problem that I have is implementing the listeners.
    The meaning is that i have to set the number of people with a JSpinner and also the maximum age.
    And when i press the Generete button, the text on the textarea must change with the amount of people and the maximum age.
    The only listener that gives me no problem is the listener for the cancel butten there is only holds System.exit(0);

    Can someone help me with the listeners please?

    Sorry for the long text and huge lot of code.

    In case someone wants the code, ask it, and I will put the source code in zipformat.


    greetings,
    Sven

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: Problem with actionlisteners en changelisteners

    Except this line of code:

    Java Code:
    //listener van de Cancel knop
    this.btnCancel.addActionListener(new Exitlistener());
    I didn't see any listener used in your program.

  3. #3
    SVENeRGy1 is offline Member
    Join Date
    Mar 2013
    Posts
    3
    Rep Power
    0

    Default Re: Problem with actionlisteners en changelisteners

    That is correct, the problem is that I don't know what to write.

    It is the meaning that the generate button provide a string in the textareafield. But how do I get the 2 values from the spinners in the textareafield when I press the generate button?

  4. #4
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: Problem with actionlisteners en changelisteners

    To add the listener to the generate button you can do the same as you did with the cancel button. The other part is to get the value of the spinner. To allow other object to access the JSpinner values your SpinnerPanel should expose both of these JSpinner. You can add a public getter method to get this object which latter can be passed to the ButtonPanel.

    Another way is to use a PropertyChangeListener mechanism.
    Last edited by wsaryada; 03-18-2013 at 11:27 AM.

  5. #5
    SVENeRGy1 is offline Member
    Join Date
    Mar 2013
    Posts
    3
    Rep Power
    0

    Default Re: Problem with actionlisteners en changelisteners

    Ok I will try that out. tnx

  6. #6
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,930
    Rep Power
    5

    Default Re: Problem with actionlisteners en changelisteners

    Another approach is to subClass JSpinner. You can then use a convenience object to store anything you want retrieved from the spinner. Within you subclass you can have a get method to retrieve your homemade object. You can be alerted that information is available by firing off a propertyChange event and using a propertyChangeListener.

    You can also use the regular listeners with JSpinner. Just remember to add an instance of the subclass and not JSpinner (assuming that is the route you take).

    The tutorials in my signature below cover Swing components and their listeners in pretty good detail. And they provide working implementations and source.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Organization of actionListeners
    By John5788 in forum AWT / Swing
    Replies: 4
    Last Post: 12-28-2011, 07:35 AM
  2. JFrames and JPanels with ActionListeners
    By atomant in forum New To Java
    Replies: 3
    Last Post: 11-30-2010, 08:26 PM
  3. Replies: 2
    Last Post: 05-28-2010, 03:48 PM
  4. Question about ActionListeners and JAR files
    By Psyclone in forum AWT / Swing
    Replies: 6
    Last Post: 03-20-2010, 01:08 PM
  5. Replies: 1
    Last Post: 12-07-2008, 06:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •