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

  • SEARCH
  • Home
  • 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 869527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:19:16+00:00 2026-05-15T10:19:16+00:00

I have been trying to left align buttons contained in a Box to the

  • 0

I have been trying to left align buttons contained in a Box to the left, with no success.

They align left alright, but for some reason dont shift all the way left as one would imagine.

I attach the code below. Please try compiling it and see for yourself. Seems bizarre to me.

Thanks, Eric

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class MainGUI extends Box implements ActionListener{
    //Create GUI Components
    Box centerGUI=new Box(BoxLayout.X_AXIS);
    Box bottomGUI=new Box(BoxLayout.X_AXIS);
    //centerGUI subcomponents
    JTextArea left=new JTextArea(), right=new JTextArea();
    JScrollPane leftScrollPane = new JScrollPane(left), rightScrollPane = new JScrollPane(right); 
    //bottomGUI subcomponents
    JButton encrypt=new JButton("Encrypt"), decrypt=new JButton("Decrypt"), close=new JButton("Close"), info=new JButton("Info");
    //Create Menubar components
    JMenuBar menubar=new JMenuBar();
    JMenu fileMenu=new JMenu("File");
    JMenuItem open=new JMenuItem("Open"), save=new JMenuItem("Save"), exit=new JMenuItem("Exit");
    int returnVal =0;

    public MainGUI(){
        super(BoxLayout.Y_AXIS);
        initCenterGUI();
        initBottomGUI();
        initFileMenu();
        add(centerGUI);
        add(bottomGUI);
        addActionListeners();
    }
    private void addActionListeners() {
        open.addActionListener(this); 
        save.addActionListener(this); 
        exit.addActionListener(this); 
        encrypt.addActionListener(this); 
        decrypt.addActionListener(this); 
        close.addActionListener(this); 
        info.addActionListener(this); 
    }
    private void initFileMenu() {
        fileMenu.add(open);
        fileMenu.add(save);
        fileMenu.add(exit);
        menubar.add(fileMenu);
    }
    public void initCenterGUI(){
        centerGUI.add(leftScrollPane);
        centerGUI.add(rightScrollPane);
    }
    public void initBottomGUI(){
        bottomGUI.setAlignmentX(LEFT_ALIGNMENT);
        //setBorder(BorderFactory.createLineBorder(Color.BLACK));
        bottomGUI.add(encrypt);
        bottomGUI.add(decrypt);
        bottomGUI.add(close);
        bottomGUI.add(info);
    }
    @Override
    public void actionPerformed(ActionEvent arg0) {
        // find source of the action
        Object source=arg0.getSource();
        //if action is of such a type do the corresponding action
        if(source==close){
             kill();
        }
        else if(source==open){
            //CHOOSE FILE
            File file1 =chooseFile();
            String input1=readToString(file1);
            System.out.println(input1);
            left.setText(input1);
        }
        else if(source==decrypt){
            //decrypt everything in Right Panel and output in left panel
            decrypt();
        }
        else if(source==encrypt){
            //encrypt everything in left panel and output in right panel
            encrypt();
        }
        else if(source==info){
            //show contents of info file in right panel
            doInfo();
        }
        else {
            System.out.println("Error");
            //throw new UnimplementedActionException();
        }
    }

    private void doInfo() {
        // TODO Auto-generated method stub
    }
    private void encrypt() {
        // TODO Auto-generated method stub
    }
    private void decrypt() {
        // TODO Auto-generated method stub
    }
    private String readToString(File file) {
        FileReader fr = null;
        try {
            fr = new FileReader(file);
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        }
        BufferedReader br=new BufferedReader(fr);

        String line = null;
        try {
            line = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        String input="";
        while(line!=null){
            input=input+"\n"+line;
            try {
                line=br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return input;
    }
    private File chooseFile() {
        //Create a file chooser
    final JFileChooser fc = new JFileChooser();
    returnVal = fc.showOpenDialog(fc);
        return fc.getSelectedFile();
    }
    private void kill() {
        System.exit(0);
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
       MainGUI test=new MainGUI();
       JFrame f=new JFrame("Tester");
       f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       f.setJMenuBar(test.menubar);
       f.setPreferredSize(new Dimension(600,400));
       //f.setUndecorated(true);
       f.add(test);
       f.pack();
       f.setVisible(true);
    }

}

  • 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-05-15T10:19:17+00:00Added an answer on May 15, 2026 at 10:19 am

    Read the section from the Swing tutorial on How to Use Box Layout. It explains (and has an example) of how BoxLayout works when components have different alignments.

    The simple solution is to add:

    centerGUI.setAlignmentX(LEFT_ALIGNMENT);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to reproduce this example: http://www.codeproject.com/KB/WPF/TreeViewWithViewModel.aspx For some reason my treeview
I have been trying to understand the way ActionScript's events are implemented, but I'm
I have been trying to figure out a solution but nothing has really presented
for the last hour I have been trying to figure this out myself but
I am trying to left align a div within a div that has been
I'm trying to reuse some script that I have working on another page, but
I have been trying to create LI elements with float left and an image
Hi I have been trying to get this script http://jsbin.com/ipajo5/ working but using .live()
I'm trying to create an input box with two buttons on the left and
I need some help with some CSS coding. I have been trying to get

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.