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