JSpinner auslesen

mjolne

Grünschnabel
Hallo!
Ich versuche grade mehr oder weniger erfolgreich eine GUI zu programmieren.
Jetzt stellt sich mir speziell folgendes Problem:
Ich habe einen JSpinner tmp der wie folgt aussieht:
new JSpinner(new SpinnerDateModel(start.getTime(), null, null, Calendar.YEAR)

Jetzt möchte ich den Wert der in dem JSpinner drin steht im nächsten Schritt auslesen und weiterverwenden. getValue().toString() liefert ein komplettes Datum mit Tag und Monat als z.b. "Wed Jun" sowie der Uhrzeit. Wie kann ich es erreichen das ich den Wert des JSpinner (z.b. "17.04.2006") nun auch genau so in eine Variable speichern kann?

Hat jemand vielleicht eine Idee?
 
Hallo,

schau mal hier:

Java:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.*;

public class SimpleDateFormatExample extends JFrame implements ActionListener {

	private JButton b1 = new JButton("Show Date");

	private JButton b2 = new JButton("Show Time");

	private JSpinner spinner = new JSpinner(new SpinnerDateModel());

	private SimpleDateFormat sdf1 = new SimpleDateFormat("dd.MM.yyyy");

	private SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm:ss");

	public SimpleDateFormatExample() {
		this.setTitle(this.getClass().getCanonicalName());
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setAlwaysOnTop(true);
		this.setLocationByPlatform(true);

		b1.addActionListener(this);
		b2.addActionListener(this);

		this.add(spinner, BorderLayout.CENTER);
		this.add(b1, BorderLayout.WEST);
		this.add(b2, BorderLayout.EAST);

		this.pack();
		this.setVisible(true);
	}

	public static void main(String[] args) {
		new SimpleDateFormatExample();
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == b1)
			JOptionPane.showMessageDialog(this, sdf1.format((Date) spinner
					.getValue()));
		else
			JOptionPane.showMessageDialog(this, sdf2.format((Date) spinner
					.getValue()));

	}
}


Vg Erdal
 
Zurück