Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / December 2005

Tip: Looking for answers? Try searching our database.

How to access the titlebar of the JFrame from innerclass taskPerformer

Thread view: 
Petterson Mikael - 09 Dec 2005 11:49 GMT
Hi,

I have a problem how to change my Lab2View class titlebar text.
I need to update it with time every second. How can I access the
titlebar text with setText from taskPerformer class?

cheers,

//miakel

public class Lab2View extends JFrame implements UpdateListener {
    private JTextField numerator, denominator;

    private JTextArea result;

    private Exercise2Controller controller;

    public JLabel numeratorLabel, denominatorLabel, resultLabel, threadsLabel;
   
    private final static String newline = "\n";
   
    private JScrollPane scroll;
   
   
    public Lab2View() {
        super("Exercise 2");
        GridBagLayout gridbag = new GridBagLayout();
        this.setLayout(gridbag);
        // Add Panel with numerator text
        GridBagConstraints numeratorTextConstraints = new GridBagConstraints();
        numeratorTextConstraints.gridx = 0;
        numeratorTextConstraints.gridy = 0;
        numeratorTextConstraints.gridwidth = 2;
        numeratorTextConstraints.gridheight = 1;
        numeratorTextConstraints.fill = GridBagConstraints.HORIZONTAL;
        numeratorLabel = new JLabel("Enter numerator ");
        numeratorLabel.setAlignmentX(JLabel.LEFT_ALIGNMENT);
        gridbag.setConstraints(numeratorLabel, numeratorTextConstraints);
        this.add(numeratorLabel);

        // Add numeratorField
        GridBagConstraints numeratorConstraints = new GridBagConstraints();
        numeratorConstraints.gridx = 2;
        numeratorConstraints.gridy = 0;
        numeratorConstraints.gridwidth = 4;
        numeratorConstraints.gridheight = 1;
        numerator = new JTextField(12);
        gridbag.setConstraints(numerator, numeratorConstraints);
        this.add(numerator);

        // Add Panel with denominator text
        GridBagConstraints denominatorTextConstraints = new GridBagConstraints();
        denominatorTextConstraints.gridx = 0;
        denominatorTextConstraints.gridy = 1;
        denominatorTextConstraints.gridwidth = 2;
        denominatorTextConstraints.gridheight = 1;
        denominatorTextConstraints.fill = GridBagConstraints.HORIZONTAL;
        denominatorLabel = new JLabel("Enter denominator and press Enter ");
        denominatorLabel.setAlignmentX(JLabel.RIGHT_ALIGNMENT);
        gridbag.setConstraints(denominatorLabel, denominatorTextConstraints);
        this.add(denominatorLabel);
       
        // Add denominatorField
        GridBagConstraints denominatorConstraints = new GridBagConstraints();
        denominatorConstraints.gridx = 2;
        denominatorConstraints.gridy = 1;
        denominatorConstraints.gridwidth = 4;
        denominatorConstraints.gridheight = 1;
        denominator = new JTextField(12);
        gridbag.setConstraints(denominator, denominatorConstraints);
        this.add(denominator);
//        //listen to action and send input to contoller.
        denominator.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                // send data to the controller
                controller.input(numerator.getText(), denominator.getText());
            }
        }); // end addActionListener

        // Add result text
        GridBagConstraints resultTextConstraints = new GridBagConstraints();
        resultTextConstraints.gridx = 0;
        resultTextConstraints.gridy = 2;
        resultTextConstraints.gridwidth = 2;
        resultTextConstraints.gridheight = 1;
        resultTextConstraints.fill = GridBagConstraints.HORIZONTAL;
        resultLabel = new JLabel("RESULT:");
        resultLabel.setAlignmentX(JLabel.RIGHT_ALIGNMENT);
        gridbag.setConstraints(resultLabel, resultTextConstraints);
        this.add(resultLabel);
       

       
        GridBagConstraints resultConstraints = new GridBagConstraints();
        resultConstraints.gridx = 2;
        resultConstraints.gridy = 2;
        resultConstraints.gridwidth = 4;
        resultConstraints.gridheight = 1;
        resultConstraints.fill = GridBagConstraints.HORIZONTAL;
        result = new JTextArea();
        result.setEditable(false);
        // Add scroll bar when a lot window cannot show all results.
        scroll = new JScrollPane(result,
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        scroll.setPreferredSize(new Dimension(300, 75));
        gridbag.setConstraints(scroll, resultConstraints);
        this.add(scroll);
       
        //Add panel with number of threads
        GridBagConstraints threadsTextConstraints = new GridBagConstraints();
        threadsTextConstraints.gridx = 0;
        threadsTextConstraints.gridy = 6;
        threadsTextConstraints.gridwidth = 2;
        threadsTextConstraints.gridheight = 1;
        threadsTextConstraints.fill = GridBagConstraints.HORIZONTAL;
        threadsLabel = new JLabel("Number of Threads is  ");
        threadsLabel.setAlignmentX(JLabel.RIGHT_ALIGNMENT);
        gridbag.setConstraints(threadsLabel, threadsTextConstraints);
        this.add(threadsLabel);
        //Create a Timer for updating titlebar with time in JFrame
        int delay = 1000; //milliseconds
         ActionListener taskPerformer = new ActionListener() {
             public void actionPerformed(ActionEvent evt) {
                Calendar cal = new GregorianCalendar();
              
                  // Get the components of the time
                  String hour24 =
Integer.toString(cal.get(Calendar.HOUR_OF_DAY));     // 0..23
                  String  min = Integer.toString(cal.get(Calendar.MINUTE));
         // 0..59
                  String sec = Integer.toString(cal.get(Calendar.SECOND));
        // 0..59
                 String currentTime = hour24+":"+min+":"+sec;
                 System.out.println(currentTime);
             }
         };
         new Timer(delay, taskPerformer).start();
    }

    // set the controller to be used
    public void setController(Exercise2Controller controller) {
        this.controller = controller;
    }

    public void updateOccurred(UpdateEvent event) {
        result.append( event.getData().getN()+"/"+event.getData().getD()+" =
"+event.getData().getR()+newline);
        result.append(newline);
    }
   
   
   

}
Mark Thomas - 09 Dec 2005 14:19 GMT
Lab2View.this.setText() should work.

Mark
Mark Thomas - 09 Dec 2005 14:21 GMT
Whoops - I meant Lab2View.this.setTitle()

Mark


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.