0

In the below code I am attempting to move the three buttons to the left when you click the left button. When I click it; nothing happens currently. Can anyone explain to me what I am doing wrong here? Also, for some reason it has stopped compiling correctly and I am unsure why but I BELIEVE it is because of a mistake in my code while attempting to get the buttons to move to the left when you click the button. I do NOT want the window to move. Just the buttons within the window. Does any one see what I am doing wrong and can you explain it?

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Buttons extends JFrame {
//Control Definitions

    JButton resetButton;
    JButton leftButton;
    JButton colorButton;
    JPanel buttonPanel;
// Layout Definiton
    eventHandle evt;
    FlowLayout flt;
    Point point; //to Hold Previous Window Position
    Color color; //to Hold Previous Color

    public Buttons() {
        super("Buttons Window");
        flt = new FlowLayout();//inialize the Flow Layout
        buttonPanel = new JPanel(flt);
        //inialize the buttonPanel With Flow Layout
        //initialize buttons
        resetButton = new JButton("Reset");
        leftButton = new JButton("Left");
        colorButton = new JButton("Blue");
        evt = new eventHandle(); //initiate the eventhandle class
        buttonPanel.add(leftButton); //add leftButton
        buttonPanel.add(colorButton);//add colorButton
        buttonPanel.add(resetButton);//add colorButton
        getContentPane().add(buttonPanel);//buttonPanel
        //add actionlistners
        leftButton.addActionListener(evt);
        colorButton.addActionListener(evt);
        resetButton.addActionListener(evt);
        setBounds(20, 120, 250, 70);
        //following Initate the point with Center of Scren
        point = new Point((Toolkit.getDefaultToolkit().
                getScreenSize().width - getWidth()) / 2, 
                (Toolkit.getDefaultToolkit().getScreenSize().height
                - getHeight()) / 2);
        setLocation(point); //locates the window in center
        color = buttonPanel.getBackground();//stores the initial color
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }

    class eventHandle implements ActionListener { //Event Handler

        public void actionPerformed(ActionEvent e) {
            {
                if (e.getSource() == leftButton) ///if its from leftButton
                {
                    leftButton.setAlignmentX(Component.LEFT_ALIGNMENT);
                    colorButton.setAlignmentX(Component.LEFT_ALIGNMENT);
                    resetButton.setAlignmentX(Component.LEFT_ALIGNMENT);
    //setLocation( (point.x -150), point.y);//shift the window 150 pixels left
                } else if (e.getSource() == colorButton) {
                    buttonPanel.setBackground(color.BLUE); 
                    //sets the backgorund to Blue
                } else {
                    leftButton.setAlignmentX(Component.CENTER_ALIGNMENT); 
                    //sets the location to previous location
                    colorButton.setAlignmentX(Component.CENTER_ALIGNMENT);
                    resetButton.setAlignmentX(Component.CENTER_ALIGNMENT);
                }

            }
        }
    }

    public static void main(String[] args) {
        Buttons buttonwindow = new Buttons();
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
ILikeTurtles
  • 1,012
  • 4
  • 16
  • 47
  • 2
    Your code is unreadable. Please indent it properly. – JB Nizet Oct 13 '12 at 11:44
  • *"In the below code I am attempting to move the three buttons to the left when you click the left button."* Why? – Andrew Thompson Oct 13 '12 at 11:47
  • @AndrewThompson It is a homework assignment. I would have flagged it as such but the system said that tag is now deprecated and we should NOT add it to posts. Also, I have had my posts edited heavily for posting extra information such as why I was doing something so I left that out. Below you will find the full tag information -- This tag is OBSOLETE and is in the process of being removed. Please do NOT add this tag to questions. But don't remove it without looking at the question to see if it needs cleanup. – ILikeTurtles Oct 13 '12 at 12:04

3 Answers3

1

It has stopped compiling, because you deleted one accolade, so put one accolade "}" just above the method:

public static  void main(String[] args)

and the code should compile. pls feedback.

EDIT:

Also rewrite your main method like this:

public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {

                    @Override
                    public void run() {
                        Buttons buttonwindow = new Buttons();
                    }
                }
            );           
    }

Every usage of Swing components must be done thorugh the Event Dispatch Thread (abbreviated EDT) or you will probably get unwanted visual effects. See here for explanation.

EDIT^2:

To achieve the desired behavior, rewrite the the action listener like this:

if (e.getSource() == leftButton)  {
    ((FlowLayout)buttonPanel.getLayout()).setAlignment(FlowLayout.LEFT); //1
    buttonPanel.revalidate(); //2
}
else if (e.getSource() == colorButton) {
    buttonPanel.setBackground(color.BLUE); 
} 
else {
    ((FlowLayout)buttonPanel.getLayout()).setAlignment(FlowLayout.CENTER);
    buttonPanel.revalidate();
}

Any change to the visual appereance to the Swing component must be done through the assigned layout manager, in this case FlowLayout - in line 1.

To see the change you must notify the Swing components layout manager to rearrange the components - in line 2 the revalidate() method "notifies" the layout manager to recalculate the new positions and eventually "notifies" the EDT to draw it on the screen.

linski
  • 5,046
  • 3
  • 22
  • 35
  • You and Reimeus gave me all of the information I needed to fix my program. I am UNSURE what the protocol for awarding the green checkbox is. Please advise? – ILikeTurtles Oct 13 '12 at 12:15
  • It is at your discretion completley, see [here](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) - in short : "The bottom line is to accept the answer that you found to be the most helpful to you, personally. " – linski Oct 13 '12 at 12:18
  • 1
    I have accepted the above as the answer, while also up voting Reimeus because this answer is more complete. It included both the fix necessary to repair the failure to compile as well as assisted me in moving the buttons as desired. – ILikeTurtles Oct 13 '12 at 12:38
1

You code won't compile as the static main method appears inside the inner class eventHandle. You can fix simply by moving it into the class body of the outer class Buttons.

As you have all the objects references at class level, you could do the button alignment using, for instance:

flt.setAlignment(FlowLayout.RIGHT);
buttonPanel.revalidate();
...

Here you are adjusting the layout alignment of your FlowLayout and revalidating to visually reflect the updated changes on your panel.

Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • 1
    This answer is very good but did not include the fix for failure to compile. I appreciate the help and hope my meager upvote is a consolation of some sorts since I cannot click the Green Checkbox on this one as well as Linski' post. Thank you again! – ILikeTurtles Oct 13 '12 at 12:39
  • @aaron That wasn't the question ;) – MadProgrammer Oct 13 '12 at 19:56
1

You should update the layout manager to align the components to the left or right. Try something like;

((FlowLayout)getLayout()).setAlignment(FlowLayout.LEFT);

Instead

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366