Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8665901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:35:52+00:00 2026-06-12T17:35:52+00:00

In the below code I am attempting to move the three buttons to the

  • 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();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T17:35:53+00:00Added an answer on June 12, 2026 at 5:35 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using the code below, I am attempting to fill a Canvas with UIElements and
I commented my code below to reflect what I am attempting to do here.
I'm attempting to convert a string to a TStream. My code below gives me
Below code makes form to be submitted without html5 validation... $j(document).on(click, #client_entry_add, function(event){ ajax_submit();});
In below code when I click 'Vote' a vote results screen is displayed but
Im attempting to Convert a Price (from an API (code below)). public class Price
Please see below my code. I am attempting to bind an array of paramenters
I am attempting to use the MediaStore.ACTION_VIDEO_CAPTURE in my app, see code below. However,
I have the below code. I am attempting to 1. import an image from
I am attempting something along the line of below mentioned code. Though it works

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.