Results 1 to 1 of 1
Thread: Change button size on click
- 03-04-2011, 02:40 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 27
- Rep Power
- 0
Change button size on click
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())); } }
Similar Threads
-
Button click GUI question
By ZambonieDrivor in forum New To JavaReplies: 2Last Post: 11-29-2010, 07:48 AM -
SaveAs button click
By kasiram.p@gmail.com in forum AWT / SwingReplies: 2Last Post: 07-06-2010, 08:35 AM -
change color on mouse click
By hannerz06 in forum New To JavaReplies: 3Last Post: 03-31-2010, 09:46 PM -
How can I display on Button click?
By ntagrafix in forum New To JavaReplies: 3Last Post: 11-04-2009, 12:05 AM -
deselecting a button after the click.
By ramsrocker in forum New To JavaReplies: 10Last Post: 02-15-2009, 06:52 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks