ANW00D
ANW00D

Reputation: 23

Overload JFrame setTitle(String) to setTitle(int)

My lesson is to change the setTitle method of JFrame, so it allows integer as a parameter. How to do that? I have to overload that method, right? Anything I tried in the setTitle method ends in a stack overflow.

import javax.swing.*;

public class MyFrame extends JFrame
{       
    MyFrame()
    {
        super();
        setSize(400, 400); // Standard initial size
        setVisible(true);
        setDefaultCloseOperation(MyFrame.EXIT_ON_CLOSE);
    }

    MyFrame(int size)
    {
        this();
        setSize(size, size); 
    }

    public void setTitle(int title)
    {           


    }

}


public class MainClass 
{
    public static void main(String[] args) 
    {   
        MyFrame frame = new MyFrame();
        frame.setTitle(1000);
    }
}

Upvotes: 2

Views: 1487

Answers (2)

SeKa
SeKa

Reputation: 1835

You seem to be on the right track with respect to overloading the method. Try:

public void setTitle(int title)
{           
    super.setTitle(""+title);
}

I didn't see a requirement to restrict the original String parameter; this is just adding another overloaded method to your subclass.

Note: Agree with Robin that this is a somewhat strange and contrived example... since normally a title is a String, so why change it...

Upvotes: 1

mKorbel
mKorbel

Reputation: 109815

methods setTitle from JFrames API

public void setTitle(String title)
Sets the title for this frame to the specified string.

then frame.setTitle("1000"); will be works

Upvotes: 1

Related Questions