9 Replies - 183 Views - Last Post: 21 February 2012 - 09:13 PM Rate Topic: -----

#1 jennyownz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 21-February 12

GUI Help

Posted 21 February 2012 - 06:35 PM

Hi guys,
I have an assignment to create a GUI that has 2 buttons: Add Name and Display File. The add name one is my issue. When I click it, the button never releases, and the window ends up freezing. I've been trying to figure it out for a while with no prevail. I would greatly appreciate any feedback. Thanks!

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.io.IOException;
import java.io.FileReader;
import java.io.PrintWriter;
public class NameReader extends JFrame 
{
  private JLabel nameL;
  private JTextField nameTF;
  private JButton dFileB, addNameB;
  private DisplayFileHandler dfHandler;
  private AddNameHandler anHandler;
  private static final int WIDTH = 400;
  private static final int HEIGHT = 200;
  
  
  public void name()
{
  nameL = new JLabel("Enter name: ");
  nameTF = new JTextField(15);
  dFileB = new JButton("Display File");
  dfHandler = new DisplayFileHandler();
  dFileB.addActionListener(dfHandler);
  addNameB = new JButton("Add Name");
  anHandler = new AddNameHandler();
  addNameB.addActionListener(anHandler);
  setTitle("Display or Add Names");
  Container pane = getContentPane();
  pane.setLayout(new GridLayout(5,2));
  pane.add(nameL);
  pane.add(nameTF);
  pane.add(dFileB);
  pane.add(addNameB);
  setSize(WIDTH, HEIGHT);
  setVisible(true);
  setDefaultCloseOperation(EXIT_ON_CLOSE);
}
  private class DisplayFileHandler implements ActionListener
  {
    public void actionPerformed(ActionEvent e)
    {
      try{
        FileReader inFile = new FileReader("input.txt.txt");
        BufferedReader br = new BufferedReader(inFile); 
        String name= br.readLine(), str="";
        while(name!=null)
        {
          str+= name + "\n";
          name= br.readLine();
        }

        JOptionPane.showMessageDialog(null, str, "Names", JOptionPane.PLAIN_MESSAGE);
        System.exit(0);
      }
      catch (IOException ex)
      {
        JOptionPane.showMessageDialog(null, "File not found.", "Error", JOptionPane.PLAIN_MESSAGE);
      }
    }
  }
  
  private class AddNameHandler implements ActionListener
  {
    public void actionPerformed(ActionEvent e)
    {
      try
      {
        String name= nameTF.getText();
        FileReader inFile = new FileReader("input.txt.txt");
        PrintWriter outFile = new PrintWriter("output.txt");
        BufferedReader br = new BufferedReader(inFile); 
        String message="", names= br.readLine();
        String last, last1;
 
        while(names!= null)
        {
          last= names.substring(names.indexOf(" "), names.length()-1);
          last1= name.substring(name.indexOf(" "), name.length()-1);
          if(last1.compareTo(last)>0)
          {
            outFile.println(name + "\n");
            outFile.println(name + "\n");
            message+= name + "\n";
            message+= names + "\n";
          }
          else
          {
            outFile.println(names + "\n");
            message+= names + "\n";
            names= br.readLine();
          }
        }
        JOptionPane.showMessageDialog(null, message, "Names", JOptionPane.PLAIN_MESSAGE);
        System.exit(0);
      }
      catch (IOException ex)
        {
            JOptionPane.showMessageDialog(null, "File not found.", "Error", JOptionPane.PLAIN_MESSAGE);
        }
    }
  }

  public static void main(String[]args)
  {
    NameReader read = new NameReader();
    read.name();
  }
}


Is This A Good Question/Topic? 0
  • +

Replies To: GUI Help

#2 Ghlavac  Icon User is offline

  • D.I.C Addict

Reputation: 83
  • View blog
  • Posts: 505
  • Joined: 14-January 09

Re: GUI Help

Posted 21 February 2012 - 06:59 PM

Names is never nulled, so its an infinite loop.
Was This Post Helpful? 0
  • +
  • -

#3 jennyownz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 21-February 12

Re: GUI Help

Posted 21 February 2012 - 07:07 PM

Well I thought that names would contain the value "null" if the end of the file is reached, since names reads the next line. Is that not true?
Was This Post Helpful? 0
  • +
  • -

#4 guido-granobles  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 171
  • View blog
  • Posts: 616
  • Joined: 02-December 09

Re: GUI Help

Posted 21 February 2012 - 07:14 PM

What exactly you need ? just to know whether or not the two last names are equals ?. In that case you should use the method equals instead of compareTo.

And the br.readline should be out of the else statement, becuase if the first last1 is equal to last then the next line never is going to be read.
Was This Post Helpful? 0
  • +
  • -

#5 jennyownz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 21-February 12

Re: GUI Help

Posted 21 February 2012 - 07:32 PM

The file input.txt has a few names listed- one on each line. They are already in alphabetical order by last name. We are supposed to have a text field that allows the user to enter a name to be added to the list, and place it in the correct place in the list (and then write that to a output file....etc.
Was This Post Helpful? 0
  • +
  • -

#6 guido-granobles  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 171
  • View blog
  • Posts: 616
  • Joined: 02-December 09

Re: GUI Help

Posted 21 February 2012 - 07:43 PM

Ok I got it. But this line.
  names= br.readLine();


Should be out side of the if/else statement because when the if statement is true that line never is going to be execute again and the while loop never ends.
Was This Post Helpful? 0
  • +
  • -

#7 guido-granobles  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 171
  • View blog
  • Posts: 616
  • Joined: 02-December 09

Re: GUI Help

Posted 21 February 2012 - 07:52 PM

I think that it would be better if you read the whole file and store each name in an ArrayList because you will have to use bubble sort algorithm in order to find the right place for the new name and then write again the names in the ArrayList to the file. The way you are doing it it's not right because find the first position and you place the name in there but what about the other words.
Was This Post Helpful? 0
  • +
  • -

#8 jennyownz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 21-February 12

Re: GUI Help

Posted 21 February 2012 - 08:28 PM

Won't an arrayList make it more complicated? It seems from my instructor that he wants us to keep it simple. I just can't figure out how to successfully compare 2 strings, as I was trying to do before. Here's the actual lab description:

The program then reads each name in turn from the file input.txt and compares it with the new name to be added. If the last name of new name is greater than or equal to the last name of the name read from the file input.txt, the name read from the file is written to a file called output.txt; otherwise, the new name is written to the file output.txt followed by the name read from the file, and then the rest of the names are read from the file input.txt and written back to the file output.txt. This ensures that the file output.txt has the names, including the new name added, in alphabetical order by last name.
Was This Post Helpful? 0
  • +
  • -

#9 jennyownz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 21-February 12

Re: GUI Help

Posted 21 February 2012 - 08:49 PM

AddNameHandler almost works- It just puts the read name in the first line no matter what the value of it is.

private class AddNameHandler implements ActionListener
  {
    public void actionPerformed(ActionEvent e)
    {
      try
      {
        String name= nameTF.getText();
        FileReader inFile = new FileReader("input.txt.txt");
        PrintWriter outFile = new PrintWriter("output.txt");
        BufferedReader br = new BufferedReader(inFile); 
        String message="", names= br.readLine();
        String last, last1;
 
        while(names!= null)
        {
          last= names.substring(names.indexOf(" "), names.length()-1);
          last1= name.substring(name.indexOf(" "), name.length()-1);
          if(last1.compareTo(last)>=0)
          {
            outFile.println(name + "\n");
            outFile.println(names + "\n");
            message+= name + "\n";
            message+= names + "\n";
            names=br.readLine();
            while(names!=null)
            {
              message+= names + "\n";
              names=br.readLine();
            }
            break;
          }
          else
          {
            outFile.println(names + "\n");
            message+= names + "\n";
            names= br.readLine();
          }
        }
        JOptionPane.showMessageDialog(null, message, "Names", JOptionPane.PLAIN_MESSAGE);
        System.exit(0);
      }
      catch (IOException ex)
        {
            JOptionPane.showMessageDialog(null, "File not found.", "Error", JOptionPane.PLAIN_MESSAGE);
        }
    }
  }

Was This Post Helpful? 0
  • +
  • -

#10 jennyownz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 21-February 12

Re: GUI Help

Posted 21 February 2012 - 09:13 PM

Sorry for so many posts- I can't figure out how to edit them! However, good news! I figured it out. Thanks for all the help!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1