Results 1 to 9 of 9
  1. #1
    Wishes is offline Member
    Join Date
    Nov 2010
    Posts
    20
    Rep Power
    0

    Default I have problem when I want connect keyevent with text fields

    hey every body,

    its first time to me work with swing

    I am trying to build frame with text field and text area and two button (start and stop)

    in textfield the user enter the file name with its path and he must press enter

    when he press enter I must open the file if its exist and store it in array list

    if the user press start button the text will be auto scrolling if he press stop button the scrolling

    I build the frame but my first step its get the file name but I am struggling with keyevent

    this is my codes until now could any one please tell what is the mistake I did

    // main
    Java Code:
    import javax.swing.*;
    /**
     *
     * @author Bobi
     */
    public class Main {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // TODO code application logic here
           new GUI();
    
    
    
        }
    
    }
    // my interface
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.util.ArrayList;
    /**
     *
     * @author Bobi
     */
    public class GUI extends javax.swing.JFrame  {
         private JButton start;
        private JButton stop;
        private JButton enter;
        private JLabel label;
        private javax.swing.JScrollPane jScrollPane1;
        private JTextArea text;
        private JTextField filename;
        private JFrame frame;
        private ArrayList <String> inputtext = new ArrayList <String> ();
        private Font font=new Font("JSL Ancient",Font.BOLD,14);
       
        public GUI(){makeframe();}
    
        private void makeframe(){
    
            frame =new JFrame("CourseWork 2 ");
            frame.setSize(800,800);
            frame.setLocationRelativeTo(null);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    
            Container contentPane = frame.getContentPane();
            start= new JButton ("Start");
            start.setSize(150,150);
            start.setFont(font);
            stop = new JButton ("Stop");
            stop.setSize(150,150);
            text= new JTextArea(40,68);
            text.setLineWrap(true);
            text.setLineWrap(true);
            filename=new JTextField(68);
            filename.setEditable(true);
             filename.setFocusable(true);
             //*** to get the file name after press enter
         //   filename.addKeyListener(new keyenter());
            label =new JLabel ("Please eneter the file name with full path");
            label.setSize(200,100);
            label.setBackground(Color.MAGENTA);
            contentPane.setLayout(new FlowLayout());
             contentPane.add(label);
            contentPane.add(filename);
       
          //  enter.addActionListener(enteraction);
             contentPane.add(text);
            contentPane.add(start);
           contentPane.add(stop);
           
            
           
            frame.setVisible(true);
            /*frame =new JFrame("HEEEEEE");
            frame.setVisible(true);*/
          /*  contentPane.setLayout(new BorderLayout());
            contentPane.add(label,BorderLayout.NORTH);
            contentPane.add(filename,BorderLayout.AFTER_LINE_ENDS);
            contentPane.add(text,BorderLayout.CENTER);
            contentPane.add(start,BorderLayout.SOUTH);
            contentPane.add(stop,BorderLayout.AFTER_LINE_ENDS);*/
    
    
            /****************************get file name
             * ************************/
    
            /*********************
             * implment the action listener of enter key
             */
      //  private class keyenter1 implements KeyListener {
      //  }
    
    //class key
    
        }
    
    }
    Java Code:
    package javaapplication7;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import javax.swing.*;
    import java.io.*;
    import java.util.ArrayList;
    /**
     *
     * @author Bobi
     */
     public class  keyenter extends JFrame implements KeyListener  {
    
         public keyenter ()//constructor
         {}
              public  ArrayList <String> KeyPressed (KeyEvent e){
              if(e.getKeyChar() == e.VK_ENTER) {
    // if is the user press enetr we take the file path with its name to open
    
    
    }// end if
              // i want here to take the file name after the user press enter
              String name = filename.getText();
              // we use buffer reader becuase we need to read the file line by line
            try{
              FileReader fin = new FileReader(name);
              BufferedReader INPUTFILE = new BufferedReader(fin);
              //creat arraylist to store all text lines
              ArrayList <String> lines = new ArrayList <String>();
    
             while (( INPUTFILE.readLine()) != null) {
                  String Line= INPUTFILE.readLine();
                 lines.add(Line);
    }
    INPUTFILE.close();
    
              return lines;
                  }
             catch (Exception f){
    	  //Catch exception if any error occure
          System.err.println("Error: " + f.getMessage());
        }
    
        }
    }
    I am using netbeans now but i didnt use any ready frame or design because I must know how I build the event by my self

    THANK YOU
    wishes:)

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Your code won't compile, and when that happens you need to post the compile error statement and let us know which line(s) cause the error.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Also, you don't need to or want to use a key listener to capture pressing enter in a JTextField. Instead add an ActionListener to the JTextField and its actionPerformed method will be called whenever the JTextField has focus and enter has been pressed. If you do this, you will find that the actionPerformed's parameter, ActionEvent has a getSource() method which will return the JTextField that is listening for the event to occur -- it will return your JTextField. You will need to case it to JTextField but you can use it to get the field's text.

  4. #4
    Wishes is offline Member
    Join Date
    Nov 2010
    Posts
    20
    Rep Power
    0

    Default

    Hey I did compile my code and it show me the interface with all fields I design but with out adding any key listener



    when I add key listener like what I did is give this error
    run:
    Exception in thread "main" java.lang.ExceptionInInitializerError
    at javaapplication7.GUI.makeframe(GUI.java:49)
    at javaapplication7.GUI.<init>(GUI.java:26)
    at javaapplication7.Main.main(Main.java:19)
    Caused by: java.lang.RuntimeException: Uncompilable source code - javaapplication7.keyenter is not abstract and does not override abstract method keyReleased(java.awt.event.KeyEvent) in java.awt.event.KeyListener
    at javaapplication7.keyenter.<clinit>(keyenter.java:1 6)
    ... 3 more
    Java Result: 1

    because that am sure my way with key listener is wrong but I don't know what is wrong

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Again, don't use a KeyListener for this. Again, add an ActionListener to the JTextField.

    As for that error, whenever a class implements an interface, the class must implement all of the interfaces classes. If and when you need to use the KeyListener interface, the Java API will tell you exactly what those methods are. For an ActionListener though, there's only one method that your class will need to implement -- actionPerformed(ActionEvent e)

  6. #6
    Wishes is offline Member
    Join Date
    Nov 2010
    Posts
    20
    Rep Power
    0

    Default

    thank you again
    and I will search on google about your suggestion
    I didn't I could use action listener with enter

    wishes :)

  7. #7
    Wishes is offline Member
    Join Date
    Nov 2010
    Posts
    20
    Rep Power
    0

    Default

    hey

    thank I did it I road the file and I did print on screen as test it work

    but I have two questions please

    first :/can I put more than actionperformd because I have 3 components
    or I use get sources each time in one action performance

    second : can I write what I want in ACTION PERFORMANCE for example if want luanch a thread in each objects .

    thank you very much.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  9. #9
    Wishes is offline Member
    Join Date
    Nov 2010
    Posts
    20
    Rep Power
    0

    Default

    thank you my question if I do something like following code is acceptable or wrong .
    Java Code:
    public void actionPerformed(ActionEvent evt) {
    
    
       if (evt.getSource() ==filename)
    
       { //if the source is textfiel
        String name = filename.getText();
        try{ 
              FileReader fin = new FileReader(name);
              BufferedReader INPUTFILE = new BufferedReader(fin);
                // read the file line by line and store it in array list
             while (( INPUTFILE.readLine()) != null) {
                  String Line= INPUTFILE.readLine();
                 inputtext.add(Line);
            }
                       INPUTFILE.close();
    // just for test i must remove it **
             int size=inputtext.size();
             for (int i=0;i<size;i++)
             {    System.out.println(inputtext.get(i));   }
              }//end try
             catch (Exception f){
    	  //Catch exception if any error occure
              System.err.println("Error: " + f.getMessage());
                               }//end cathch
        }//end true if
                       else if (evt.getSource()==start)
       {// startthread here whic append line line to text area 
    // iS IT  O.k if I Started THREAD HERE?
       }
       else {//the source is stop button  }
           //I must stop append the text (using thread
    }
        
        }
    }
    thank you

Similar Threads

  1. disapling text fields
    By javanew in forum AWT / Swing
    Replies: 2
    Last Post: 05-06-2010, 03:39 PM
  2. searching a row using text fields
    By bigj in forum New To Java
    Replies: 1
    Last Post: 02-03-2010, 11:28 AM
  3. Problem With Text Fields!
    By freshoreo in forum AWT / Swing
    Replies: 3
    Last Post: 08-04-2008, 09:52 PM
  4. Demonstration of text fields in SWT
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-25-2008, 02:20 PM
  5. Help with text fields in Java
    By romina in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:29 AM

Posting Permissions

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