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 3 of 3

Thread: How can I reset the values on my display screen without the NEW values being added to the OLD values?

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

    Default How can I reset the values on my display screen without the NEW values being added to the OLD values?

    Hello Javanese,

    I am new to this website, and I need some help with my code. Would appreciate it if anyone can help. I'm writing a code which displays the exam grades that students in a class achieve. My code produces five text input boxes labelled # of grade As, # of grade Bs, # of grade Cs, # grade Ds, # of grade Es. I then enter (for example) number 5 in each of the boxes. I then press a button labelled "Display bar chart", and I get the following on the screen: Grade As : 5, Grade Bs : 5, Grade Cs : 5, Grade Ds : 5, Grade Es : 5. I then press another button labelled "Clear the screen" and the data disappears. I then press a third button labelled "Display the letters", EXPECTING that I would get the following display:
    Grade As : AAAAA, Grade Bs : BBBBB, Grade Cs : CCCCC, Grade Ds : DDDDD, Grade Es : EEEEE. In other words, instead of being represented by the number 5, I want the grades to be displayed as a string of five letters. Here's my problem : when I press the button "Display the letters", I do NOT get AAAAA, BBBBB, CCCCC, DDDDD, EEEEE. What I get is DOUBLE the letters (ie. TEN instead of five) I'm supposed to get, ie. I get AAAAAAAAAA, BBBBBBBBBB, CCCCCCCCCC, etc. It seems that when I press the "Clear the screen" button, I am NOT resetting the previous value, I'm simply CLEARING the screen and that's all. The PREVIOUS value of five is in fact being ADDED to the current value of five and as a result I'm getting a string of TEN letters (eg. AAAAAAAAAA) which is not what I want. Can anyone tell me what to do? I've thought about this for six or seven hours and I CAN'T come up with a solution.

    My second (related) problem is that when I want to display the result as a string of 5 letters, I'm getting the word "null" followed by the string of letters, eg. I'm getting "Grade As : nullAAAAAAAAAA" instead of "AAAAA". Where is the "null" coming from??? Please could someone tell me what I'm doing wrong? I'm fairly new to Java. Here's my full code :



    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;


    public class GradeChart extends JFrame
    implements ActionListener {


    private JTextField gradeAField = new JTextField( 5 );
    private JTextField gradeBField = new JTextField( 5 );
    private JTextField gradeCField = new JTextField( 5 );
    private JTextField gradeDField = new JTextField( 5 );
    private JTextField gradeEField = new JTextField( 5 );


    private int gradeAs;
    private int gradeBs;
    private int gradeCs;
    private int gradeDs;
    private int gradeEs;
    String longStringA;
    String gradeLetterA;
    String longStringB;
    String gradeLetterB;
    String longStringC;
    String gradeLetterC;
    String longStringD;
    String gradeLetterD;
    String longStringE;
    String gradeLetterE;


    private JButton displayChartButton = new JButton( "Display bar chart" );
    private JButton displayDataFieldsButton = new JButton( "Clear data fields" );
    private JButton displayLongString = new JButton ("Display the letters");
    private JButton clearScreen = new JButton ("Clear the screen");

    private Color chartPanelColor = Color.cyan;


    private final Font labelFont = new Font( "Serif", Font.BOLD, 16 );
    private final int chartLeftXA = 30;
    private final int chartLeftXB = 30;
    private final int chartLeftXC = 30;
    private final int chartLeftXD = 30;
    private final int chartLeftXE = 30;


    private final int chartTopYA = 50;
    private final int chartTopYB = 102;
    private final int chartTopYC = 154;
    private final int chartTopYD = 206;
    private final int chartTopYE = 258;


    public static void main( String[] args ) {

    GradeChart chart = new GradeChart();
    chart.setSize( 550, 550 );
    chart.createGUI();
    chart.setVisible( true );

    }


    private void createGUI() {


    setDefaultCloseOperation( EXIT_ON_CLOSE );
    Container window = getContentPane();
    window.setLayout( new FlowLayout() );


    JLabel gradeAFieldLabel = new JLabel( "# of grade As:" );
    gradeAFieldLabel.setFont( labelFont );
    window.add( gradeAFieldLabel );
    window.add( gradeAField );

    JLabel gradeBFieldLabel = new JLabel( "# of grade Bs:" );
    gradeBFieldLabel.setFont( labelFont );
    window.add( gradeBFieldLabel );
    window.add( gradeBField );

    JLabel gradeCFieldLabel = new JLabel( "# of grade Cs:" );
    gradeCFieldLabel.setFont( labelFont );
    window.add( gradeCFieldLabel );
    window.add( gradeCField );

    JLabel gradeDFieldLabel = new JLabel( "# of grade Ds:" );
    gradeDFieldLabel.setFont( labelFont );
    window.add( gradeDFieldLabel );
    window.add( gradeDField );

    JLabel gradeEFieldLabel = new JLabel( "# of grade Es:" );
    gradeEFieldLabel.setFont( labelFont );
    window.add( gradeEFieldLabel );
    window.add( gradeEField );


    displayChartButton = new JButton("Display bar chart");
    window.add( displayChartButton );
    displayChartButton.addActionListener( this );

    displayDataFieldsButton = new JButton("Clear data fields");
    window.add( displayDataFieldsButton );
    displayDataFieldsButton.addActionListener( this );

    displayLongString = new JButton("Display the letters");
    window.add( displayLongString );
    displayLongString.addActionListener( this );

    clearScreen = new JButton("Clear the screen");
    window.add( clearScreen );
    clearScreen.addActionListener( this );


    chartPanel.setPreferredSize( new Dimension( 450, 300 ) );
    chartPanel.setBackground( chartPanelColor );
    window.add( chartPanel );


    }


    public void actionPerformed( ActionEvent e ) {

    if ( e.getSource() == displayChartButton ) {

    checkAndRecordData();

    Graphics g = chartPanel.getGraphics();



    g.setColor(Color.white);
    g.fillRect(20,20,410,52);
    g.setColor(Color.black);
    g.drawRect(20,20,410,52);
    g.setColor( Color.black );
    g.drawString( "Grade As: " + gradeAs, chartLeftXA, chartTopYA );

    g.setColor(Color.white);
    g.fillRect(20,72,410,52);
    g.setColor(Color.black);
    g.drawRect(20,72,410,52);
    g.setColor( Color.black );
    g.drawString( "Grade Bs: " + gradeBs, chartLeftXB, chartTopYB );

    g.setColor(Color.white);
    g.fillRect(20,124,410,52);
    g.setColor(Color.black);
    g.drawRect(20,124,410,52);
    g.setColor( Color.black );
    g.drawString( "Grade Cs: " + gradeCs, chartLeftXC, chartTopYC );

    g.setColor(Color.white);
    g.fillRect(20,176,410,52);
    g.setColor(Color.black);
    g.drawRect(20,176,410,52);
    g.setColor( Color.black );
    g.drawString( "Grade Ds: " + gradeDs, chartLeftXD, chartTopYD );

    g.setColor(Color.white);
    g.fillRect(20,228,410,52);
    g.setColor(Color.black);
    g.drawRect(20,228,410,52);
    g.setColor( Color.black );
    g.drawString( "Grade Es: " + gradeEs, chartLeftXE, chartTopYE );

    }

    if ( e.getSource() == displayDataFieldsButton ) {

    gradeAField.setText("");
    gradeBField.setText("");
    gradeCField.setText("");
    gradeDField.setText("");
    gradeEField.setText("");

    }


    if (e.getSource() == clearScreen) {

    checkAndRecordData();

    Graphics g = chartPanel.getGraphics();

    g.setColor(Color.white);
    g.fillRect(20,20,410,52);
    g.setColor(Color.black);
    g.drawRect(20,20,410,52);
    g.setColor( Color.black );
    g.drawString( "", chartLeftXA, chartTopYA );

    g.setColor(Color.white);
    g.fillRect(20,72,410,52);
    g.setColor(Color.black);
    g.drawRect(20,72,410,52);
    g.setColor( Color.black );
    g.drawString( "", chartLeftXB, chartTopYB );

    g.setColor(Color.white);
    g.fillRect(20,124,410,52);
    g.setColor(Color.black);
    g.drawRect(20,124,410,52);
    g.setColor( Color.black );
    g.drawString( "", chartLeftXC, chartTopYC );

    g.setColor(Color.white);
    g.fillRect(20,176,410,52);
    g.setColor(Color.black);
    g.drawRect(20,176,410,52);
    g.setColor( Color.black );
    g.drawString( "", chartLeftXD, chartTopYD );

    g.setColor(Color.white);
    g.fillRect(20,228,410,52);
    g.setColor(Color.black);
    g.drawRect(20,228,410,52);
    g.setColor( Color.black );
    g.drawString( "", chartLeftXE, chartTopYE );
    }


    if (e.getSource() == displayLongString) {

    Graphics g = chartPanel.getGraphics();

    g.drawString( "Grade As: " + longStringA, 100, 50 );

    g.drawString( "Grade Bs: " + longStringB, 100, 102 );

    g.drawString( "Grade Cs: " + longStringC, 100, 154 );

    g.drawString( "Grade Ds: " + longStringD, 100, 206 );

    g.drawString( "Grade Es: " + longStringE, 100, 258 );
    }

    }


    private void checkAndRecordData() {

    int tempAs = 0;
    int tempBs = 0;
    int tempCs = 0;
    int tempDs = 0;
    int tempEs = 0;


    tempAs = Integer.parseInt( gradeAField.getText() );
    tempBs = Integer.parseInt( gradeBField.getText() );
    tempCs = Integer.parseInt( gradeCField.getText() );
    tempDs = Integer.parseInt( gradeDField.getText() );
    tempEs = Integer.parseInt( gradeEField.getText() );


    gradeAs = tempAs;
    gradeBs = tempBs;
    gradeCs = tempCs;
    gradeDs = tempDs;
    gradeEs = tempEs;

    String gradeLetterA = "A";
    for (int i=0; i<tempAs; i++)
    longStringA += gradeLetterA;

    String gradeLetterB = "B";
    for (int i=0; i<tempBs; i++)
    longStringB += gradeLetterB;

    String gradeLetterC = "C";
    for (int i=0; i<tempCs; i++)
    longStringC += gradeLetterC;

    String gradeLetterD = "D";
    for (int i=0; i<tempDs; i++)
    longStringD += gradeLetterD;

    String gradeLetterE = "E";
    for (int i=0; i<tempEs; i++)
    longStringE += gradeLetterE;
    }
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How can I reset the values on my display screen without the NEW values being added to the OLD values?

    Welcome, and please read the Announcement topic at the top of the sub-forum to learn how to correctly post code and other useful things.

    Where is chartPanel declared and initialized?

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    Bewitched (October 26th, 2013)

  4. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How can I reset the values on my display screen without the NEW values being added to the OLD values?

    Hello Greg,

    That's a good question. It would seem that I've not declared chartPanel. I guess it's back to the drawing board. I'll have a look at the Announcement topic now to see how I should post my code. Thanks for the reply.

Similar Threads

  1. Display database values into JTables
    By tommyacton in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 13th, 2013, 02:54 PM
  2. [SOLVED] compare form values with database values
    By VaniRathna in forum Java Servlet
    Replies: 2
    Last Post: October 24th, 2011, 02:48 AM
  3. Character Values Inside of Number Values
    By bgroenks96 in forum Java Theory & Questions
    Replies: 4
    Last Post: October 2nd, 2011, 08:27 PM
  4. Cannot get values from hashmap
    By uhertz in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 17th, 2011, 07:44 PM
  5. Values of Input
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: November 8th, 2009, 03:46 AM

Tags for this Thread