14

This code compiles, I just can't get the name to change on the title bar.

import java.awt.BorderLayout;
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.JTabbedPane;  
import javax.swing.JTextArea;  
import javax.swing.JTextField;  
public class VolumeCalculator extends JFrame implements ActionListener  
{  
    private JTabbedPane jtabbedPane;  
    private JPanel options;  
    JTextField poolLengthText, poolWidthText, poolDepthText, poolVolumeText, hotTub,  
            hotTubLengthText, hotTubWidthText, hotTubDepthText, hotTubVolumeText, temp, results,
            myTitle;  
    JTextArea labelTubStatus;  
    public VolumeCalculator()  
    {  
        setSize(400, 250);  
        setVisible(true);  
        setSize(400, 250);  
        setVisible(true);  
        setTitle("Volume Calculator");  
        setSize(300, 200);  
        JPanel topPanel = new JPanel();  
        topPanel.setLayout(new BorderLayout());  
        getContentPane().add(topPanel);  

        createOptions();  

        jtabbedPane = new JTabbedPane();  

        jtabbedPane.addTab("Options", options);  

        topPanel.add(jtabbedPane, BorderLayout.CENTER);  
    } 
    /* CREATE OPTIONS */ 

    public void createOptions()  
    {  
        options = new JPanel();  
        options.setLayout(null);  
        JLabel labelOptions = new JLabel("Change Company Name:");  
        labelOptions.setBounds(120, 10, 150, 20);  
        options.add(labelOptions);  
        JTextField newTitle = new JTextField("Some Title"); 
        newTitle.setBounds(80, 40, 225, 20);  
        options.add(newTitle);
        myTitle = new JTextField();   
        myTitle.setBounds(80, 40, 225, 20); 
        myTitle.add(labelOptions); 
        JButton newName = new JButton("Set New Name");  
        newName.setBounds(60, 80, 150, 20);  
        newName.addActionListener(this);  
        options.add(newName);  
        JButton Exit = new JButton("Exit");  
        Exit.setBounds(250, 80, 80, 20);  
        Exit.addActionListener(this);  
        options.add(Exit);  
    }  
    public void actionPerformed(ActionEvent event)  
    {  
        JButton button = (JButton) event.getSource();  
        String buttonLabel = button.getText();  
        if ("Exit".equalsIgnoreCase(buttonLabel))  
        {  
            Exit_pressed();  
            return;  
        }  
        if ("Set New Name".equalsIgnoreCase(buttonLabel))  
        {  
            New_Name();  
            return;  
        }  
    }  
    private void Exit_pressed()  
    {  
        System.exit(0);  
    }  
    private void New_Name()  
    {  
        this.setTitle(myTitle.getText());  
    }  
    private void Options()  
    {  
    }  
    public static void main(String[] args)  
    {  
        JFrame frame = new VolumeCalculator();  
        frame.setSize(380, 350);  
        frame.setVisible(true);  
    }  
}
1
  • 2
    setTitle() must be called from an object instance, not from static JFrame. Be careful when naming an object instance to avoid the confusion with a class name.
    – eee
    Commented Mar 30, 2011 at 14:28

4 Answers 4

33

If your class extends JFrame then use this.setTitle(newTitle.getText());

If not and it contains a JFrame let's say named myFrame, then use myFrame.setTitle(newTitle.getText());

Now that you have posted your program, it is obvious that you need only one JTextField to get the new title. These changes will do the trick:

JTextField poolLengthText, poolWidthText, poolDepthText, poolVolumeText, hotTub,
        hotTubLengthText, hotTubWidthText, hotTubDepthText, hotTubVolumeText, temp, results,
        newTitle;

and:

    public void createOptions()
    {
        options = new JPanel();
        options.setLayout(null);
        JLabel labelOptions = new JLabel("Change Company Name:");
        labelOptions.setBounds(120, 10, 150, 20);
        options.add(labelOptions);
        newTitle = new JTextField("Some Title");
        newTitle.setBounds(80, 40, 225, 20);
        options.add(newTitle);
//        myTitle = new JTextField("My Title...");
//        myTitle.setBounds(80, 40, 225, 20);
//        myTitle.add(labelOptions);
        JButton newName = new JButton("Set New Name");
        newName.setBounds(60, 80, 150, 20);
        newName.addActionListener(this);
        options.add(newName);
        JButton Exit = new JButton("Exit");
        Exit.setBounds(250, 80, 80, 20);
        Exit.addActionListener(this);
        options.add(Exit);
    }

and:

private void New_Name()
{
    this.setTitle(newTitle.getText());
}
8
  • @Costis: It does extend JFrame. I tried what you said, but get the same error.
    – Mike
    Commented Mar 30, 2011 at 14:33
  • Try to add some text to your newTitle JTextField: newTitle.setText("title"); Commented Mar 30, 2011 at 14:35
  • @Costis: I tried this.setTitle(newTitle.getText("Title")); No luck
    – Mike
    Commented Mar 30, 2011 at 14:38
  • Change this line JTextField newTitle = new JTextField(); to JTextField newTitle = new JTextField("Some Title"); If it does not work post your entire code. Commented Mar 30, 2011 at 14:40
  • @Costis: The code above now compiles to show what it is not doing.
    – Mike
    Commented Mar 30, 2011 at 16:43
6

newTitle is a local variable where you create the fields. So when that functions ends, the variable newTitle, does not exist anymore. (The JTextField that was referenced by newTitle does still exist however.)

Thus, increase the scope of the variable, so that you can access it another method.

public SomeFrame extends JFrame {
   JTextField myTitle;//can be used anywhere in this class

   creationOfTheFields()
   {
   //other code
      myTitle = new JTextField("spam");  
      myTitle.setBounds(80, 40, 225, 20);
      options.add(myTitle);
   //blabla other code
   }

   private void New_Name()  
   {  
      this.setTitle(myTitle.getText());  
   } 
}
2
  • @lshtar: Getting close, when I type in a new name and click "Set New Name" the title disappears. I'm not sure if I have something in the wrong place or what.
    – Mike
    Commented Mar 30, 2011 at 14:52
  • @lshtar: Now if I click the "Set New Name" button the title changes to "spam". I can't change it. After I close it and re-open it the tile is set back.
    – Mike
    Commented Mar 30, 2011 at 16:09
2

these methods can help setTitle("your new title"); or super("your new title");

1
  • 1
    Can you please explain why this would help Mike with his issue?
    – John Odom
    Commented May 28, 2015 at 18:57
1

I strongly recommend you learn how to use layout managers to get the layout you want to see. null layouts are fragile, and cause no end of trouble.

Try this source & check the comments.

import java.awt.BorderLayout;
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.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class VolumeCalculator extends JFrame implements ActionListener {
    private JTabbedPane jtabbedPane;
    private JPanel options;
    JTextField poolLengthText, poolWidthText, poolDepthText, poolVolumeText, hotTub,
            hotTubLengthText, hotTubWidthText, hotTubDepthText, hotTubVolumeText, temp, results,
            myTitle;
    JTextArea labelTubStatus;

    public VolumeCalculator(){
        setSize(400, 250);
        setVisible(true);
        setSize(400, 250);
        setVisible(true);
        setTitle("Volume Calculator");
        setSize(300, 200);
        JPanel topPanel = new JPanel();
        topPanel.setLayout(new BorderLayout());
        getContentPane().add(topPanel);

        createOptions();

        jtabbedPane = new JTabbedPane();

        jtabbedPane.addTab("Options", options);

        topPanel.add(jtabbedPane, BorderLayout.CENTER);
    }
    /* CREATE OPTIONS */

    public void createOptions(){
        options = new JPanel();
        //options.setLayout(null);
        JLabel labelOptions = new JLabel("Change Company Name:");
        labelOptions.setBounds(120, 10, 150, 20);
        options.add(labelOptions);
        JTextField newTitle = new JTextField("Some Title");
        //newTitle.setBounds(80, 40, 225, 20);    
        options.add(newTitle);
        myTitle = new JTextField(20);
        // myTitle WAS NEVER ADDED to the GUI!
        options.add(myTitle);
        //myTitle.setBounds(80, 40, 225, 20);
        //myTitle.add(labelOptions);
        JButton newName = new JButton("Set New Name");
        //newName.setBounds(60, 80, 150, 20);
        newName.addActionListener(this);
        options.add(newName);
        JButton Exit = new JButton("Exit");
        //Exit.setBounds(250, 80, 80, 20);
        Exit.addActionListener(this);
        options.add(Exit);
    }

    public void actionPerformed(ActionEvent event){
        JButton button = (JButton) event.getSource();
        String buttonLabel = button.getText();
        if ("Exit".equalsIgnoreCase(buttonLabel)){
            Exit_pressed();
            return;
        }
        if ("Set New Name".equalsIgnoreCase(buttonLabel)){
            New_Name();
            return;
        }
    }

    private void Exit_pressed(){
        System.exit(0);
    }

    private void New_Name(){
        System.out.println("'" + myTitle.getText() + "'");
        this.setTitle(myTitle.getText());
    }

    private void Options(){
    }

    public static void main(String[] args){
        JFrame frame = new VolumeCalculator();
        frame.pack();
        frame.setSize(380, 350);
        frame.setVisible(true);
    }
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.