Hello,

I am trying to create a pretty specific datepicker and am having some problems.

I have create a JPanel which has several buttons arranged vertically representing specific months that I need in the MM format (ie. 10 - for October).

What I want is when the user clicks the button, the button expands vertically down to show the MMMM format (ie. October) which since the button expanded vertically would be rotate 90 degrees (which is a problem I still haven't tackled yet).

I have successfully registered actions which change the background and foreground color of the button, but trying to change the size isn't working.

I have include the code below.

Thanks for any help.


Java Code:
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;


public class MonthSelector extends JPanel implements ActionListener,
		KeyListener, ChangeListener, FocusListener {

	protected int numMonths;
	protected Date months[];
	protected JButton monthButtons[];
	Color selectedColor;
	Color defaultColor;
	JButton selectedMonth;
	SimpleDateFormat longMonthFormat;
	SimpleDateFormat shortMonthFormat;

	public MonthSelector() {

		longMonthFormat = new SimpleDateFormat("MMMM");
		shortMonthFormat = new SimpleDateFormat("MM");
		
		
		// ---TESTING
		testingInitialization();

		monthButtons = new JButton[numMonths];
		setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));

		for (int ii = 0; ii < numMonths; ii++) {
			monthButtons[ii] = new JButton(shortMonthFormat.format(months[ii]));
			monthButtons[ii].setAlignmentX(LEFT_ALIGNMENT);
			monthButtons[ii].setMargin(new Insets(2, 2, 2, 2));
			//monthButtons[ii].setMaximumSize(new Dimension(200, 40));
			monthButtons[ii].setPreferredSize(new Dimension(20, 20));
			monthButtons[ii].setActionCommand(Integer.toString(ii));
			monthButtons[ii].addActionListener(this);
			add(monthButtons[ii]);

		}
		defaultColor = monthButtons[1].getBackground();
		selectedColor = new Color(72, 96, 104);
		
	}

	private void testingInitialization() {

		numMonths = 3;
		Calendar startTime = new GregorianCalendar(2006, Calendar.OCTOBER, 1, 12, 30, 00);
		months = new Date[numMonths];

		for (int ii = 0; ii < numMonths; ii++) {
			months[ii] = startTime.getTime();
			
			startTime.add(Calendar.MONTH,1);
		}

	}

	private void setTrack(int trackNum) {

		if (selectedMonth != null){
			selectedMonth.setBackground(defaultColor);
			selectedMonth.setForeground(Color.BLACK);
			selectedMonth.setPreferredSize(new Dimension(20, 20));
		}

		
		selectedMonth = monthButtons[trackNum];
		
		
		selectedMonth.setBackground(selectedColor);
		selectedMonth.setForeground(Color.WHITE);
		selectedMonth.setPreferredSize(new Dimension(20, 100));
		
		repaint();
	}

	public static void main(String[] args) throws IOException {

		MonthSelector monthSelector = new MonthSelector();

		monthSelector.setOpaque(true);

		JFrame f = new JFrame();
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.setContentPane(monthSelector);

		f.setLocation(200, 200);
		f.pack();
		f.setVisible(true);
	}

	public void focusGained(FocusEvent arg0) {
		// TODO Auto-generated method stub

	}

	public void focusLost(FocusEvent arg0) {
		// TODO Auto-generated method stub

	}

	public void stateChanged(ChangeEvent arg0) {
		// TODO Auto-generated method stub

	}

	public void keyPressed(KeyEvent arg0) {
		// TODO Auto-generated method stub

	}

	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub

	}

	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub

	}

	public void actionPerformed(ActionEvent event) {
		// TODO Auto-generated method stub

		JButton button = (JButton) event.getSource();

		setTrack(Integer.parseInt(button.getActionCommand()));

	}
}