Search code examples
javaswingframes

I need to be able to change the title of the frame in java via a button in another class


I've been looking online for about 2 hours now. What I need to complete is a way to have a button in another class, change the title of the frame window for the whole program. So basically I want to make it so the customer can change the setTitle to whatever his business name is. Here is the code I have so far for both classes that will be involved in this issue.

Options.java

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Options extends JPanel {
    JLabel nameLabel;
    JTextField nameField;
    JButton sNn;

    public Options()
    {
        nameLabel = new JLabel();
        nameLabel.setText("Change Company Name:");
        nameField = new JTextField(22);
        sNn = getTitleChangeButton();
        ExitButton exitButton = new ExitButton();

        this.add(nameLabel);
        this.add(nameField);
        this.add(sNn);
        this.add(exitButton.getExitButton());   
    }

    public JButton getTitleChangeButton()
    {
        JButton ChangeTitle; 
        ChangeTitle = new JButton("Change Company Title");
        ChangeTitle.setMnemonic('C');
        ChangeTitle.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {       
                String title;       
            }
        });
            return ChangeTitle;
            }
        }

here is the second class called tabbedPane. This is the class that I want to be the superclass and have the setTitle changed via the button.

TabbedPane.java

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
//import poolCalculation.PoolVolumeCalc;
//import poolCalculation.GenTab;
//import poolCalculation.TempCalc;
import poolCalculation.Options;

public class TabbedPane extends JFrame
{
    public TabbedPane()
    {

        setTitle("Gardening INC.");

        JTabbedPane Tabs = new JTabbedPane();
        getContentPane().add(Tabs);

        JPanel jp1 = new JPanel();

        //Tabs.addTab("Pool Volume Calculator", new PoolVolumeCalc());
        //Tabs.addTab("General Tab", new GenTab());
        //Tabs.addTab("Temperature Calculation", new TempCalc());
        Tabs.addTab("Options", new Options());

        getContentPane().add(Tabs);
        setSize(400,300);
        setVisible(true);
    }

    public static void main(String[] args) {
        TabbedPane test = new TabbedPane();
        test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

}

For the life of me I can't figure out how to get the title to change for the frame when the user clicks the button in options. Here is what I have tried so far, I have tried making setTitle(title) and making that a string in the main section of TabbedPane and then using a get method to get the text from options, but that didn't work. Or I did it wrong. I tried making the button its own class, but ran into not being able to read the textfield.


Solution

  • Here is an example that demonstrates how to change the title dynamically:

    enter image description here

    import java.awt.BorderLayout;
    import java.awt.Container;
    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.JPanel;
    import javax.swing.JTextField;
    import javax.swing.SwingUtilities;
    
    public class NormalFrame extends JFrame
    {
        private static final long serialVersionUID = 1L;
    
        public NormalFrame()
        {
            super("The title");
            setDefaultCloseOperation(EXIT_ON_CLOSE);
    
            Container contentPane = getContentPane();
            contentPane.add(new CustomPanel(this));
    
            pack();
            setLocationRelativeTo(null);
        }
    
        public static void main(String[] args)
        {
            SwingUtilities.invokeLater(new Runnable()
            {
                @Override
                public void run()
                {
                    new NormalFrame().setVisible(true);
                }
            });
        }
    }
    
    class CustomPanel extends JPanel
    {
        private static final long serialVersionUID = 1L;
        private JFrame mainFrame;
        private JLabel lbl;
        private JTextField txt;
        private JButton btn;
    
        private ActionListener listener = new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                mainFrame.setTitle(txt.getText());
            }
        };
    
        public CustomPanel(JFrame mainFrame)
        {
            this.mainFrame = mainFrame;
    
            lbl = new JLabel("The new title: ");
            txt = new JTextField(10);
            btn = new JButton("Change Title");
            btn.addActionListener(listener);
    
            setLayout(new BorderLayout());
            add(lbl, BorderLayout.LINE_START);
            add(txt, BorderLayout.CENTER);
            add(btn, BorderLayout.LINE_END);
        }
    }