Display all class objects in gui labels

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kjkrudh
    New Member
    • Sep 2007
    • 9

    #1

    Display all class objects in gui labels

    Hello all,
    I'm having a problem with displaying my entire list of employees from a text file.
    Opening and storing are working fine, but displaying to my gui it only shows the last employee on the list.
    I'll insert my code here:

    [CODE=java] private void actionPerformed tbDisplayEmploy ees(javax.swing .event.Ancestor Event evt) {
    // Code to Display Employee pay info:Need to open myData.txt or enter in new emps first:

    try{
    BufferedReader inputStream = null;
    inputStream = new BufferedReader( new FileReader("myD ata.txt"));
    if(inputStream == null){
    JOptionPane.sho wMessageDialog( this, ("To display Employees you must open myData.txt" + "\n"
    + "or submit new employees."));
    }

    final double SS = .03;
    final double FICA = .05;
    for(int i=0;i<numString s;i++){
    tmpEmp = new TEmployee();
    tmpEmp = myCompany.getEm ployee(i);
    String showLastName = tmpEmp.getEmpLa stName();

    lblDisplayEmpLa stName.setText( showLastName);

    double hours = Double.valueOf( tmpEmp.getEmpHo urs());
    double payrate = Double.valueOf( tmpEmp.getEmpHr Rate());
    double grosspay = (hours * payrate);
    NumberFormat nf = NumberFormat.ge tCurrencyInstan ce(Locale.US);
    nf.format(gross pay);
    String showgrosspay = Double.toString (grosspay);

    lblDisplayGross Pay.setText(sho wgrosspay);

    //Calculate deductions
    double totalSSdeduct =(grosspay * SS);
    double totalFICAdeduct = (grosspay * FICA);
    double totalDeduct = (totalSSdeduct + totalFICAdeduct );
    nf.format(total Deduct);

    nf.format(total SSdeduct);
    String showSSdeduct = Double.toString (totalSSdeduct) ;
    lblDisplayEmpSS Deductions.setT ext(showSSdeduc t);

    nf.format(total FICAdeduct);
    String showFICAdeduct = Double.toString (totalFICAdeduc t);
    lblDisplayEmpFI CA.setText(show FICAdeduct);

    double netpay = (grosspay - totalDeduct);
    nf.format(netpa y);
    String shownetpay = Double.toString (netpay);
    lblDisplayNetPa y.setText(shown etpay);
    }
    } catch (FileNotFoundEx ception e) {
    System.err.prin tln("FileStream sTest: " + e);
    } catch (IOException e) {
    System.err.prin tln("FileStream sTest: " + e);
    }//end try/catch
    }[/CODE]

    I'd love to hear any ideas that may help.
    Last edited by r035198x; Oct 24 '07, 06:23 PM. Reason: corrected the code tags
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    I've given your code a long look and I'm failing to understand what is going on.

    I can however say that when you do textField.setTe xt("someValue" ) whatever value that was previously on the Textfield becomes over written by the text "someValue" .

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by kjkrudh
      Hello all,
      I'm having a problem with displaying my entire list of employees from a text file.
      Opening and storing are working fine, but displaying to my gui it only shows the last employee on the list.
      You have one label for the last name display; one label for the payment etc. etc.
      When you set them over and over again only the last values will be shown of course.

      It's as if you had done:

      [code=java]
      x= 1;
      x= 2;
      x= 3;
      [/code]

      Guess what the value of x will be after this snippet of code has finished.

      kind regards,

      Jos

      Comment

      • kjkrudh
        New Member
        • Sep 2007
        • 9

        #4
        You are both seeing exactly my problem. Now I need to display this entire list, not just one in the label. Do I use a different JComponent to display these in (JTable possibly)? and how do I set each string into the table(or whatever is best) up until the list is done?

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by kjkrudh
          You are both seeing exactly my problem. Now I need to display this entire list, not just one in the label. Do I use a different JComponent to display these in (JTable possibly)? and how do I set each string into the table(or whatever is best) up until the list is done?
          Yep, use a JTable and build your own TableModel. Extend from the AbstractTableMo del;
          you don't have to do (almost) anything at all then. You already have your Employees
          in an array or List so all you have to do is return a String from one of the Employee
          fields given a column index value. A simply switch can do that. You only need to
          implement three simple methods:

          [code=java]
          public int getRowCount();
          public int getColumnCount( );
          public Object getValueAt(int row, int column);
          [/code]

          The most important thing for now is: read the API docs for those two classes.

          kind regards,

          Jos

          Comment

          • kjkrudh
            New Member
            • Sep 2007
            • 9

            #6
            Table works great. Thank you!

            Comment

            Working...