jlabel won't clear - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

jlabel won't clear

AestralAestral Posts: 2Member
Hello, I am making a data entry program and I want the current page number to display. To do this I need to clear a jlabel and replace its contents with the current page number(when changing pages). Instead it adds each page number to that label and does not clear the label beforehand. Here is the segment of code I am having problems with

private class ButtonHandler implements ActionListener
{



public void actionPerformed(ActionEvent e)

{
formatter8.format("%d",Page);

JButton button = (JButton) e.getSource();

if(button.equals(previousJButton)) //firstButton is the first class JButton that was defined

{
if(Page==1){
//JOptionPane.showMessageDialog(DataEntry.this,String.format("You pressed: %s",e.getActionCommand()));
label1.setText(formatter.toString());
label2.setText(formatter2.toString());
label3.setText(formatter3.toString());
label10.setText(formatter8.toString());

}
Page--;
if(Page<0)
{
Page=0;
label10.setText(formatter8.toString());


}
if(Page==0){
label1.setText(formatter.toString());
label2.setText(formatter2.toString());

label3.setText(formatter3.toString());
formatter8.format("%d",Page);


label10.setText(formatter8.toString());


}
if(Page==1){
label1.setText(formatter5.toString());
label2.setText(formatter6.toString());
label3.setText(formatter7.toString());
label10.setText("");
label10.setText(formatter8.toString());

}


}

if(button.equals(nextJButton)) //secondButton is the second class JButton defined

{
Page++;
if (Page==1){
//Panel5.removeAllChildComponents().
label1.setText(formatter5.toString());
label2.setText(formatter6.toString());
label3.setText(formatter7.toString());
label10.setText("");
label10.setText(formatter8.toString());

}
else{

label10.setText("");
label10.setText(formatter8.toString());
}

}


}

}











Here is the rest of the program if this helps.



import java.io.*;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.GridLayout;
import javax.swing.JTextField;
import javax.swing.JOptionPane;
import java.util.Formatter;
import java.util.Random;

//import java.util.*;



public class DataEntry extends JFrame implements FocusListener,KeyListener,ActionListener
{
private JButton nextJButton;
private JButton previousJButton;
private JLabel label1;
private JLabel label2;
private JLabel label3;
private JLabel label4;
private JLabel label5;
private JLabel label6;
private JLabel label7;
private JLabel label8;
private JLabel label9;
private JLabel label10;
//private JLabel label10;
private JPanel Panel1;
private JPanel Panel2;
private JPanel Panel3;
private JPanel Panel4;
private JPanel Panel5;

private JTextField textField1;
private JTextField textField2;
private JTextField textField3;
private BorderLayout layout;
private GridLayout gridLayout1;
private FlowLayout flowLayout1;

Timer timerSec=new Timer(1000,this);
Timer timerMin=new Timer(60000,this);
//Timer timer3Min=new Timer(18000,this);
private String a;
private String b;
private String c;
private String d;
private String l;
private String yy;
private int f;
private int v;
private int x;

private int Page=0;
private int z;
private int n;
private float m=0;
private int correct=0;
private int array[]=new int[24];
private int pageArray[]=new int[3];
private int incorrect=0;
private int counter2=-1;
private int timerSecCounter=59;
private int minute=1;
private int second=59;
private String zz;
private String cc;
private String timerSecACname="a";
private String timerMinACname="b";
Formatter formatter=new Formatter();
Formatter formatter2=new Formatter();
Formatter formatter3=new Formatter();
Formatter formatter4=new Formatter();
Formatter formatter5=new Formatter();
Formatter formatter6=new Formatter();
Formatter formatter7=new Formatter();
Formatter formatter8=new Formatter();
Formatter formatterClear=new Formatter();

public DataEntry()
{
super("Data Entry");

layout = new BorderLayout(5,10);
previousJButton=new JButton("Previous Page");
nextJButton=new JButton("Next Page");

timerSec.start();
timerSec.setActionCommand(timerSecACname);
timerMin.start();
timerMin.setActionCommand(timerMinACname);
//timer3Min.start();
flowLayout1= new FlowLayout();
setLayout(layout);
Panel1 = new JPanel();
Panel2=new JPanel();
Panel3=new JPanel();
Panel4=new JPanel();
Panel5=new JPanel();
Random randomNumber=new Random();

for(int counter=0;counter<array.length;counter++){

array[counter]=randomNumber.nextInt(10);

}



formatter.format("%d%d%d%d",array[0],array[1],array[2],array[3]);
formatter2.format("%d%d%d%d",array[4],array[5],array[6],array[7]);
formatter3.format("%d%d%d%d",array[8],array[9],array[10],array[11]);
formatter5.format("%d%d%d%d",array[12],array[13],array[14],array[15]);
formatter6.format("%d%d%d%d",array[16],array[17],array[18],array[19]);
formatter7.format("%d%d%d%d",array[20],array[21],array[22],array[23]);
formatter8.format("%d",Page);
formatterClear.format("");
//d=String.valueOf(z);
label1=new JLabel(formatter.toString());
label2=new JLabel(formatter2.toString());
label3=new JLabel(formatter3.toString());
label4=new JLabel("");
label5=new JLabel("");
label6=new JLabel("1");
label7=new JLabel(":");
label8=new JLabel("00");
label9=new JLabel("Time Left: ");
label10=new JLabel(formatter8.toString());



add(Panel5,BorderLayout.WEST);
//Panel5.setLayout(flowLayout1);
Panel5.add(label10);


add(Panel1,BorderLayout.NORTH);
Panel1.setLayout(flowLayout1);
Panel1.add(label1);
Panel1.add(label2);
Panel1.add(label3);

add(Panel2,BorderLayout.SOUTH);
textField2=new JTextField(4);
textField3=new JTextField(4);
textField1=new JTextField(4);

textField1.addFocusListener(this);
textField3.addFocusListener(this);
textField2.addFocusListener(this);
textField1.setName("David");
textField1.addKeyListener(this);
textField2.addKeyListener(this);
textField3.addKeyListener(this);
textField2.setName("John");
textField3.setName("Roger");

Panel2.add(textField1);
Panel2.add(textField2);
Panel2.add(textField3);
Panel2.add(previousJButton);
Panel2.add(nextJButton);

add(Panel3,BorderLayout.CENTER);
Panel3.setLayout(flowLayout1);
Panel3.add(label4);
Panel3.add(label5);

add(Panel4,BorderLayout.EAST);
Panel4.add(label9);
Panel4.add(label6);
Panel4.add(label7);
Panel4.add(label8);

ButtonHandler handler=new ButtonHandler();
previousJButton.addActionListener(handler);
nextJButton.addActionListener(handler);

}

private class ButtonHandler implements ActionListener
{



public void actionPerformed(ActionEvent e)

{
formatter8.format("%d",Page);

JButton button = (JButton) e.getSource();

if(button.equals(previousJButton)) //firstButton is the first class JButton that was defined

{
if(Page==1){
//JOptionPane.showMessageDialog(DataEntry.this,String.format("You pressed: %s",e.getActionCommand()));
label1.setText(formatter.toString());
label2.setText(formatter2.toString());
label3.setText(formatter3.toString());
label10.setText(formatter8.toString());

}
Page--;
if(Page<0)
{
Page=0;
label10.setText(formatter8.toString());


}
if(Page==0){
label1.setText(formatter.toString());
label2.setText(formatter2.toString());

label3.setText(formatter3.toString());
formatter8.format("%d",Page);


label10.setText(formatter8.toString());


}
if(Page==1){
label1.setText(formatter5.toString());
label2.setText(formatter6.toString());
label3.setText(formatter7.toString());
label10.setText("");
label10.setText(formatter8.toString());

}


}

if(button.equals(nextJButton)) //secondButton is the second class JButton defined

{
Page++;
if (Page==1){
//Panel5.removeAllChildComponents().
label1.setText(formatter5.toString());
label2.setText(formatter6.toString());
label3.setText(formatter7.toString());
label10.setText("");
label10.setText(formatter8.toString());

}
else{

label10.setText("");
label10.setText(formatter8.toString());
}

}


}

}







public void focusGained(FocusEvent e) {
System.out.println("focusGained");
if(e.getComponent() instanceof JTextField) {
JTextField jTextField = (JTextField) e.getComponent();
if(jTextField.getName() != null && jTextField.getName().equals("David")) {
label4.setText(formatter.toString());
}
if(jTextField.getName() != null && jTextField.getName().equals("John")) {
label4.setText(formatter2.toString());
}
if(jTextField.getName() != null && jTextField.getName().equals("Roger")) {
label4.setText(formatter3.toString());
}


}
}


public void focusLost(FocusEvent e) {

}


public void keyTyped(KeyEvent e) {
l=String.format("%s",e.getKeyChar());
counter2++;

b=String.valueOf(array[counter2]);


if(l.equals(b)){
correct++;
System.out.printf("Correct:%d
Incorrect:%d
",correct,incorrect);
}

else {
incorrect++;
System.out.printf("Correct:%d
Incorrect:%d
",correct,incorrect);

}


f++;
if(f==4){
textField2.requestFocus();

}
if(f==8){
textField3.requestFocus();
}
if(f==12){
f=0;
textField1.requestFocus();
m=((float)correct/12)*100;
System.out.printf("%.2f",m);
formatter4.format("%.2f",m);

if(m==0){

yy=String.valueOf(m);
label5.setText(yy);
}
else{
//yy=String.valueOf(m);
label5.setText(formatter4.toString());
}
}

}
/** Handle the key-pressed event from the text field. */
public void keyPressed(KeyEvent e) {


}

/** Handle the key-released event from the text field. */
public void keyReleased(KeyEvent e) {

}


public void actionPerformed(ActionEvent event)
{
if (event.getSource() instanceof Timer) { // MODIFIED CODE
if (event.getActionCommand().equalsIgnoreCase(timerSecACname)) // MODIFIED CODE
{
if(timerSecCounter!=0)
{
label6.setText("0");
timerSecCounter--;
cc=String.valueOf(timerSecCounter);
label8.setText(cc);
}
else{
timerSec.stop();
label8.setText("00");
System.out.println("Sec stopped: ");
}

} else if (event.getActionCommand().equalsIgnoreCase(timerMinACname)) // MODIFIED CODE
{
timerMin.stop();
label9.setText("Time Up");
System.out.println ("Min stopped: ");


}
}






}
}


Sign In or Register to comment.