Search code examples
javaswingactionlistenerborder-layout

Changing center JButton to a panel that changes when other buttons are pressed


I'm new so this question may seem incredibly obvious...

I'm am trying to change a border layout in Java so that the center button is a panel/Jtextarea. A panel that reacts when the other panels are pressed by saying "Going *" *being the direction. Then when I press a new button it erases the old and changes to "Going **" ** being the new direction. I have included the current code and a picture of what I'm looking for :)

Swing Layout

/*
 * BorderLayoutDemo.java
 *
 */
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;


public class BorderLayoutDemo {
public static boolean RIGHT_TO_LEFT = false;

public static void addComponentsToPane(Container pane) {

    if (!(pane.getLayout() instanceof BorderLayout)) {
        pane.add(new JLabel("Container doesn't use BorderLayout!"));
        return;
    }

    if (RIGHT_TO_LEFT) {
        pane.setComponentOrientation(
                java.awt.ComponentOrientation.RIGHT_TO_LEFT);
    }


    JButton button = new JButton("Up");
    pane.add(button, BorderLayout.PAGE_START);

    //Make the center component 400x400
    //typical usage of BorderLayout.

    button = new JButton("Going...");
    pane.setPreferredSize(new Dimension(400, 400));
    pane.add(button, BorderLayout.CENTER);

    button = new JButton("Left");
    pane.add(button, BorderLayout.LINE_START);

    button = new JButton("Down");
    pane.add(button, BorderLayout.PAGE_END);

    button = new JButton("Right");
    pane.add(button, BorderLayout.LINE_END);
}

/**
 * Create the GUI and show it.  For thread safety,
 * this method should be invoked from the
 * event dispatch thread.
 */
private static void createAndShowGUI() {

    //Create and set up the window.
    JFrame frame = new JFrame("BorderLayoutDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //Set up the content pane.
    addComponentsToPane(frame.getContentPane());
    //Use the content pane's default BorderLayout. No need for
    //setLayout(new BorderLayout());
    //Display the window.
    frame.pack();
    frame.setVisible(true);
}

public static void main(String[] args) {
    /* Use an appropriate Look and Feel */
    try {
        //UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
    } catch (UnsupportedLookAndFeelException ex) {
        ex.printStackTrace();
    } catch (IllegalAccessException ex) {
        ex.printStackTrace();
    } catch (InstantiationException ex) {
        ex.printStackTrace();
    } catch (ClassNotFoundException ex) {
        ex.printStackTrace();
    }
    /* Turn off metal's use bold fonts */
    UIManager.put("swing.boldMetal", Boolean.FALSE);

    //Schedule a job for the event dispatch thread:
    //creating and showing this application's GUI.
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
    });
}

}


Solution

  • You need to give a unique field name to the JButton in the center.

    goingButton = new JButton("Going...");
    pane.add(goingButton, BorderLayout.CENTER);
    

    Then you need to write an action listener for the other buttons that change the text of the goingButton. Here's how you set the text of a JButton.

        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                goingButton.setText("Going up");
            }       
        });
    

    Edited to just give you the modified code. You're not going to learn anything if others do your work for you.

    Here's a screen shot of the BorderLayoutDemo.

    Border Layout Demo

    And here's the code, formatted and modified for you:

    package com.ggl.testing;
    
    /*
     * BorderLayoutDemo.java
     *
     */
    import java.awt.BorderLayout;
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class BorderLayoutDemo {
        private static final boolean RIGHT_TO_LEFT = false;
    
        private JButton goingButton;
    
        public void addComponentsToPane(Container pane) {
    
            if (!(pane.getLayout() instanceof BorderLayout)) {
                pane.add(new JLabel("Container doesn't use BorderLayout!"));
                return;
            }
    
            if (RIGHT_TO_LEFT) {
                pane.setComponentOrientation(java.awt.ComponentOrientation.RIGHT_TO_LEFT);
            }
    
            pane.setPreferredSize(new Dimension(400, 400));
    
            JButton button = new JButton("Up");
            button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    goingButton.setText("Going up");
                }
            });
            pane.add(button, BorderLayout.PAGE_START);
    
            // Make the center component 400x400
            // typical usage of BorderLayout.
    
            goingButton = new JButton("Going...");
            goingButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    goingButton.setText("Going crazy");
                }
            });
            pane.add(goingButton, BorderLayout.CENTER);
    
            button = new JButton("Left");
            button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    goingButton.setText("Going left");
                }
            });
            pane.add(button, BorderLayout.LINE_START);
    
            button = new JButton("Down");
            button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    goingButton.setText("Going down");
                }
            });
            pane.add(button, BorderLayout.PAGE_END);
    
            button = new JButton("Right");
            button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    goingButton.setText("Going right");
                }
            });
            pane.add(button, BorderLayout.LINE_END);
        }
    
        /**
         * Create the GUI and show it. For thread safety, this method should be
         * invoked from the event dispatch thread.
         */
        private void createAndShowGUI() {
    
            // Create and set up the window.
            JFrame frame = new JFrame("BorderLayoutDemo");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            // Set up the content pane.
            addComponentsToPane(frame.getContentPane());
            // Use the content pane's default BorderLayout. No need for
            // setLayout(new BorderLayout());
            // Display the window.
            frame.pack();
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            /* Use an appropriate Look and Feel */
            try {
                // UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
                UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
            } catch (UnsupportedLookAndFeelException ex) {
                ex.printStackTrace();
            } catch (IllegalAccessException ex) {
                ex.printStackTrace();
            } catch (InstantiationException ex) {
                ex.printStackTrace();
            } catch (ClassNotFoundException ex) {
                ex.printStackTrace();
            }
            /* Turn off metal's use bold fonts */
            UIManager.put("swing.boldMetal", Boolean.FALSE);
    
            // Schedule a job for the event dispatch thread:
            // creating and showing this application's GUI.
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    new BorderLayoutDemo().createAndShowGUI();
                }
            });
        }
    }