3 Replies - 1099 Views - Last Post: 11 September 2009 - 02:23 PM Rate Topic: -----

#1 jinnyishere  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 44
  • View blog
  • Posts: 127
  • Joined: 06-July 09

GUI JSpinner Help

Posted 11 September 2009 - 02:17 PM

Hi!! I'm working on a simple JSpinner, when i compile my program it gives me an error saying that
int value = spinner.getValue(); is an incompatiable types...could someone give me a hint..here is the complete program

/*
	The exampl demonstrate a simple spinner
	It allows the user to choose the time
	In addition, user can choose the am/pm
	
*
*/
 import javax.swing.*;
 import java.awt.event.*;

 public class Spinner extends JFrame
 {
 	private JPanel panel;
	private JButton ok;
	private JSpinner spinner;
	
	public Spinner()
	{
		// Sets the title of the window
		
		setTitle("JSpinner Window");
		
		// Sets the size of the window
		
		setSize(300, 200);
		
		// Sets the start location of the window
		
		setLocationRelativeTo(null);
		
		// Sets the action when a window is close
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		// Creates a spinner with initial, min, and max value
		
		spinner = new JSpinner(new SpinnerNumberModel(1, 1, 12, 1));

		
		// Creates a OK button
		
		ok = new JButton("OK");
		
		// Creates an action listener for the button
		
		ok.addActionListener(new okButtonListener());
		
		// Creates a panel
		
		panel = new JPanel();
		
		// Adds the components to the panel
		
		panel.add(spinner);
		panel.add(ok);
		
		// Adds the panel to the content pane
		
		add(panel);
		
		// Sets the visibility of the window
		
		setVisible(true);
		
	}
	
	private class okButtonListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			if(e.getSource()==ok)
			{
				int value = spinner.getValue();
				
				JOptionPane.showMessageDialog(null, "You have selected " + value);
				
			}
		}
	}
	
	public static void main(String[] args)
	{
		new Spinner();
		
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: GUI JSpinner Help

#2 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 674
  • View blog
  • Posts: 4,349
  • Joined: 24-October 08

Re: GUI JSpinner Help

Posted 11 September 2009 - 02:20 PM

getValue returns an Object reference and not an int so for sure you cant assign it to an int variable
Was This Post Helpful? 0
  • +
  • -

#3 AntonWebsters  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 88
  • View blog
  • Posts: 428
  • Joined: 15-August 09

Re: GUI JSpinner Help

Posted 11 September 2009 - 02:20 PM

Try and change int value = spinner.getValue(); to Object value = spinner.getValue();. Maybe that will work.
Was This Post Helpful? 1
  • +
  • -

#4 jinnyishere  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 44
  • View blog
  • Posts: 127
  • Joined: 06-July 09

Re: GUI JSpinner Help

Posted 11 September 2009 - 02:23 PM

View PostAntonWebsters, on 11 Sep, 2009 - 01:20 PM, said:

Try and change int value = spinner.getValue(); to Object value = spinner.getValue();. Maybe that will work.

cool..thx changing it to object value worked...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1