Results 1 to 4 of 4
  1. #1
    Tellier is offline Member
    Join Date
    Aug 2012
    Posts
    3
    Rep Power
    0

    Default JFrame freezes, MouseEvent not responding when clicking 3 out of 4 buttons.

    Hi,

    I'm trying to code a simple register which stores names together with their age in a file using the FileWriter & FileReader.
    Everything works perfectly until I've added about 2-4 names, then the JFrame stops responding to my actions except when I
    click the quitButton. There is no error or anything, the program simply stops responding to any of the actions I've coded it to look for.
    Can you see what is causing the problem? I'd love any constructive feedback!

    Java Code:
    package nameandage;
    
    import java.awt.*;
    import java.io.*;
    import java.util.*;
    import javax.swing.*;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    
    public class Register extends JFrame {
        
        static Scanner sc = new Scanner(System.in);
        FileWriter fw;
        BufferedWriter bw;
        FileReader fr;
        BufferedReader br;
        
        int cordX, cordY;
        int lineNumber = 0;
        
        static boolean addPressed, listPressed, deletePressed, quitPressed;
        
        // Double buffering
        Image dbImage;
        Graphics dbg;
        
        // Menu Buttons
        Rectangle addButton = new Rectangle(15, 330, 100, 25);
        Rectangle listButton = new Rectangle(125, 330, 100, 25);
        Rectangle deleteButton = new Rectangle(15, 365, 100, 25);
        Rectangle quitButton = new Rectangle(125, 365, 100, 25);
        
        // Variables for screen size
        int
        GWIDTH = 240,
        GHEIGHT = 400;
        
        // Dimension of GWIDTH*GHEIGHT
        Dimension screenSize = new Dimension(GWIDTH, GHEIGHT);
        
        public Register() {
            this.setTitle("Register database");
            this.setSize(screenSize);
            this.setResizable(false);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setVisible(true);
            this.setBackground(Color.WHITE);
            this.addMouseListener(new Register.MouseHandler());
            this.addMouseMotionListener(new Register.MouseHandler());
            
        }
        
        public void StartProgram() {
            try{
                fr = new FileReader("Register.txt");
                br = new BufferedReader(fr);
            
                
                while(br.readLine() != null){
                lineNumber++;
                }
                fr.close();
            } catch(IOException ioe){
                //ignore problem
            }
            
        }
        
        @Override
        public void paint(Graphics g){
            dbImage = createImage(getWidth(), getHeight());
            dbg = dbImage.getGraphics();
            draw(dbg);
            g.drawImage(dbImage, 0, 0, this);
        }
        
        public void draw(Graphics g){
            // Menu
            // Button to add person to register
            g.setColor(Color.LIGHT_GRAY);
            g.fillRect(addButton.x, addButton.y, addButton.width, addButton.height);
            g.setFont(new Font("Arial", Font.BOLD, 12));
            g.setColor(Color.BLACK);
            g.drawString("Add person", addButton.x+20, addButton.y+17);
            // Button to display the data in the register
            g.setColor(Color.LIGHT_GRAY);
            g.fillRect(listButton.x, listButton.y, listButton.width, listButton.height);
            g.setColor(Color.BLACK);
            g.drawString("Display register", listButton.x+6, listButton.y+17);
            // Button to delete the register data
            g.setColor(Color.LIGHT_GRAY);
            g.fillRect(deleteButton.x, deleteButton.y, deleteButton.width, deleteButton.height);
            g.setColor(Color.BLACK);
            g.drawString("Delete register", deleteButton.x+9, deleteButton.y+17);
            // Button to exit the program
            g.setColor(Color.LIGHT_GRAY);
            g.fillRect(quitButton.x, quitButton.y, quitButton.width, quitButton.height);
            g.setColor(Color.BLACK);
            g.drawString("Quit program", quitButton.x+12, quitButton.y+17);
    
            while(addPressed){
                try {
                    fw = new FileWriter("Register.txt", true);
                    bw = new BufferedWriter(fw);
                    String inputName;
                    String inputAge;
    
                    inputName = JOptionPane.showInputDialog("Enter the name of the person: ");
                    inputAge = JOptionPane.showInputDialog("Enter the age of the person: ");
                    
                    bw.write(inputName + " " + inputAge + "\r");
                    
                    bw.close();
                    fw = null;
                    addPressed = false;
                } catch(IOException e){
                    System.out.println("Fail - error");
                }
            } 
            if(listPressed){
                try{
                    fr = new FileReader("Register.txt");
                    br = new BufferedReader(fr);
    
                    String s;
                    int y = 50;
                    int x = 20;
                    while((s = br.readLine()) != null){
                        if(x == 20 && y < 320){
                            g.setColor(Color.BLACK);
                            g.drawString(s, x, y);
                            y += 20;
                        } else if (y > 320){
                            x = 140;
                            y = 50;
                        } else if(x == 140){
                            g.setColor(Color.BLACK);
                            g.drawString(s, x, y);
                            y += 20;
                        }
                    } 
                    
                } catch(IOException e){
                    System.out.println("Error! ");
                }
            }
            if(deletePressed){
                try {
                    fw = new FileWriter("Register.txt");
                    bw = new BufferedWriter(fw);
                    bw.write("");
                    bw.close();
                    fw = null;
                    deletePressed = false;
                } catch (IOException e){
                    //ignore
                }
                JOptionPane.showMessageDialog(null, "The list is now empty");
            }
            repaint();
        }
        
        public class MouseHandler extends MouseAdapter {
            @Override
            public void mouseClicked(MouseEvent e){
                int x = e.getX();
                int y = e.getY();
                
                if( (x > 15 && x < 115) && (y > 330 && y < 355)){
                    addPressed = true;
                    listPressed = false;
                    deletePressed = false;
                    quitPressed = false;
                } else if( (x > 125 && x < 225) && (y > 330 && y < 355)){
                    addPressed = false;
                    listPressed = true;
                    deletePressed = false;
                    quitPressed = false;
                } else if( (x > 15 && x < 115) && (y > 365 && y < 390)){
                    addPressed = false;
                    listPressed = false;
                    deletePressed = true;
                    quitPressed = false;
                } else if( (x > 125 && x < 225) && (y > 365 && y < 390)){
                    addPressed = false;
                    listPressed = false;
                    deletePressed = false;
                    quitPressed = true;
                    System.exit(0);
                } else {
                    addPressed = false;
                    listPressed = false;
                    deletePressed = false;
                    quitPressed = false; 
                }
            }
            @Override
            public void mousePressed(MouseEvent e){
                
            }
        }
        
        private void setLookAndFeel(){
            try {
                UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimusLookAndFeel");
            } catch(Exception exc) {
                // ignore error
            }
        }
        
        public static void main(String[] args) throws Exception{
            
            Register register = new Register();
            
        }
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default Re: JFrame freezes, MouseEvent not responding when clicking 3 out of 4 buttons.

    Why aren't you using ordinary JButtons and let them do all that hard work?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: JFrame freezes, MouseEvent not responding when clicking 3 out of 4 buttons.

    Have you tried debugging the code by adding println statements to print out the values of variables as they are are changed and to show when methods are executed? The print out should help you understand what the code is doing.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,300
    Rep Power
    20

    Default Re: JFrame freezes, MouseEvent not responding when clicking 3 out of 4 buttons.

    There's a lot wrong with that code. Better go through this tutorial Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials)

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Jframe freezes while doing other task
    By danoc93 in forum AWT / Swing
    Replies: 9
    Last Post: 05-14-2012, 03:14 AM
  2. JFrame freezes when button is clicked
    By JG4m3r in forum AWT / Swing
    Replies: 4
    Last Post: 04-06-2012, 03:20 AM
  3. JFrame freezes when button is clicked
    By JG4m3r in forum New To Java
    Replies: 2
    Last Post: 04-04-2012, 11:43 PM
  4. Clicking two buttons at the same time
    By Daslee in forum AWT / Swing
    Replies: 3
    Last Post: 03-21-2012, 01:15 PM
  5. Changing images by clicking arrow buttons. help?
    By ashton in forum New To Java
    Replies: 3
    Last Post: 02-08-2009, 11:29 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •