Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: set frame title from another class

  1. #1
    Junior Member
    Join Date
    Aug 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default set frame title from another class

    Hi,
    I have a frame which has tabs (code is below)--
    private JPanel mainPane;
    	private JFrame fr;	
    		public TabClass(){
                            super("change this title");
    			fr=new JFrame();
    			mainPane=new JPanel();
    			JTabbedPane jtp=new JTabbedPane();
    			this.setContentPane(mainPane);
     
    			jtp.addTab("General",  new General());
    			jtp.addTab("Options", new Options());
     
    			mainPane.add(jtp);
    			setVisible(true);
    			setSize(600, 600);
    		}
    	public static void main(String[] args) {
    		TabClass jp=new TabClass();		
    	}
    In the Option class (given above when adding tabs) I have a textfield where user will enter text and that text will be set as title of window.
    I did not get how to set title of frame from another class. The code of Option class is as below
    public void actionPerformed(ActionEvent e){
    			if(e.getSource()==btnSetName){
    				String getCompanyName=txtName.getText();	
    				TabClass tb=new TabClass();
    				tb.setTitle(getCompanyName);		
    			}}
    When I run above code a new widow is created and its title is set as text entered by user. Please tell me how to set frame title from another class? Any suggestion will also be appreciated.


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: set frame title from another class

    To change the state of any object from an outside class, you either need to make the field public or provide a method to access/change it.
    Considering JFrame's setTitle(String title), (inherited from Frame), you pass a string as the new title.
    Making fr public would allow fr.setTitle(title); to work.
    On the other hand the TabClass could have a method setTitle(String title); where inside the body you use the fr.setTitle(title); and keep fr private.
    (Using the method and keeping the field private would be my recommendation)

Similar Threads

  1. Why is a Frame Not Showing Up When I Run This Class?
    By Ormolu611 in forum What's Wrong With My Code?
    Replies: 18
    Last Post: April 7th, 2013, 06:22 PM
  2. Replies: 2
    Last Post: November 18th, 2012, 02:09 PM
  3. how to set my frame in the middle of the screen
    By chronoz13 in forum AWT / Java Swing
    Replies: 3
    Last Post: February 10th, 2012, 08:13 AM
  4. [SOLVED] Set Invisible or close a frame
    By Souperk in forum Java Theory & Questions
    Replies: 9
    Last Post: November 30th, 2011, 11:56 AM
  5. JFrame, frame's title doesnt appear.
    By chronoz13 in forum AWT / Java Swing
    Replies: 1
    Last Post: November 26th, 2009, 03:38 PM