Results 1 to 7 of 7
  1. #1
    leroycorbid is offline Member
    Join Date
    Feb 2012
    Posts
    4
    Rep Power
    0

    Default New user needing help adding FocusListener event to selectAll in JTextField

    Hello all;

    Been working on trying to improve this program so that when the user selects a text box for input they will not have to erase the existing text before entering their new values. I think I need to use a focus gained event to selectAll(). I have not been able to get it to work. Would anyone look at my code and suggest where I am going wrong??

    Thanks in advance LeRoy

    Java Code:
    /***********************************************************************
    ************************************************************************
    ***** GUI Temperature conversion utility  By LeRoy on 2/22/2012  *********
    ************************************************************************/
    
    			import javax.swing.*;
    			import java.awt.event.*;
    			import java.awt.*;
    			
    			
    public class GUI_TEMP_TEST extends JFrame implements FocusListener
      {
    //         ***********************DEFINE CONSTANTS**********************
    			private static final double F_TO_C = 5.0/9.0;
    			private static final double C_TO_F = 9.0/5.0;
    			private static final int    OFFSET = 32;
    			
     //        **********************DEFINE GUI OBJECTS*********************
     
    			private JLabel title, celciusLabel, fahrenLabel, blank1, blank2, blank3, blank4;
    			private JTextField celciusText, fahrenText;
    			private JButton exitBut;
    			private JPanel P1,P2,P3,P4,P5,P6;
    			
    //          *********************DEFINE ACTION HANDLERS********************
    
    			private exitButHandler ebHandler;
    			private celsHandler cHandler;
    			private fahrHandler fHandler;
    			private fListener listener;
    		
    	public GUI_TEMP_TEST()
    	  {
    //         ********************** DEFINE WINDOW **********************
    
    			setTitle("  CELCIUS TO FAHRENHEIT CONVERSION UTILITY   ");
    			
    			
    //         ***********************DEFINE OBJECTS***********************
    		
    				   title = new JLabel(" Enter Either Value for Conversion: ",SwingConstants.CENTER);
    			celciusLabel = new JLabel(" Celcius value: ",SwingConstants.RIGHT);
    			 fahrenLabel = new JLabel(" Fahrenheit value: ", SwingConstants.RIGHT);
    				  blank1 = new JLabel("     ",SwingConstants.CENTER);
    				  blank2 = new JLabel("     ",SwingConstants.CENTER);
    				  blank3 = new JLabel("     ",SwingConstants.CENTER);
    				  blank4 = new JLabel("     ",SwingConstants.CENTER);
    			  
    			 celciusText = new JTextField(5);
    			  fahrenText = new JTextField(5);
    		  
    		         exitBut = new JButton("Exit");
    		  
    //          **********************DEFINE CONTAINERS********************
    			Container P1 = getContentPane();
    
    			JPanel P2 = new JPanel();
    			JPanel P3 = new JPanel();
    			JPanel P4 = new JPanel();
    			JPanel P5 = new JPanel();
    			JPanel P6 = new JPanel();
    		  
    			P1.setLayout(new GridLayout(3,1,10,2));
    			P2.setLayout(new GridLayout(1,1));
    			P2.setBackground(Color.blue);
    			P3.setLayout(new GridLayout(1,4));
    			P4.setLayout(new GridLayout(1,3));
    			P5.setLayout(new GridLayout(1,2));
    			P6.setLayout(new GridLayout(1,2));
    		  
    //          ***********************ADD COMPONENTS**************************
    		
    			P2.add(title);
    				
    			P4.add(blank1);
    			P4.add(exitBut);
    			P4.add(blank2);
    		
    			P5.add(fahrenText);
    			P5.add(blank3);
    		
    			P6.add(celciusText);
    			P6.add(blank4);
    		
    			P3.add(fahrenLabel);
    			P3.add(P5);
    			P3.add(celciusLabel);
    			P3.add(P6);
    		
    			P1.add(P2);
    			P1.add(P3);
    			P1.add(P4);
    		
    //          ***********************ADD ACTION LISTENERS***************************
    
    			ebHandler = new exitButHandler();
    			exitBut.addActionListener(ebHandler);
    		
    			cHandler = new celsHandler();
    			celciusText.addActionListener(cHandler);
    		
    			fHandler = new fahrHandler();
    			fahrenText.addActionListener(fHandler);
    		
    			listener = new FocusListener();
    			celciusText.addFocusListener(listener);
    			
    						
    //          ***********************WINDOW DEFINITIONS*****************************
    		
    			setSize(425,125);
    			setVisible(true);
    			setDefaultCloseOperation(EXIT_ON_CLOSE);
    	  }
    	  
    //         *************************SET EVENTS*************************************
    		  
    		 private class exitButHandler implements ActionListener
    		   {
    			  public void actionPerformed(ActionEvent e)
    			    {
    					System.exit(0);
    				}
    			}
    			
    					
    		private class celsHandler implements ActionListener
    		
    		   {
    				public void actionPerformed(ActionEvent e)
    				  {
    						double celcius,fahrenheit;
    					
    					celcius = Double.parseDouble(celciusText.getText());
    					fahrenheit = celcius * C_TO_F + OFFSET;
    					fahrenText.setText(String.format(" %.2f",fahrenheit));
    				
    				 
    				 }
    		   }
    		   
    		   private class fahrHandler implements ActionListener
    		
    		   {
    				public void actionPerformed(ActionEvent e)
    				  {
    						double celcius,fahrenheit;
    					
    					fahrenheit = Double.parseDouble(fahrenText.getText());
    					celcius = (fahrenheit - OFFSET)* F_TO_C ;
    					celciusText.setText(String.format(" %.2f",celcius));
    				
    				  }
    		   }
    		   
    		   private class fListener implements FocusListener
    			{ 
    				public void focusGained(FocusEvent e)
    				{ 
    					celciusText.selectAll();
    				}
    				
    			}
    		public static void main(String[] args)
    		  {
    			GUI_TEMP_TEST testObject = new GUI_TEMP_TEST();
    		  }
    	}
    Last edited by leroycorbid; 02-25-2012 at 05:53 AM. Reason: code tags added

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

    Default Re: New user needing help adding FocusListener event to selectAll in JTextField

    Code tags added to aid readability: [code] [/code]

    Does this code compile? Especially this:
    Java Code:
    listener = new FocusListener();
    since FocusListener is an interface and you're trying to instantiate it.

  3. #3
    leroycorbid is offline Member
    Join Date
    Feb 2012
    Posts
    4
    Rep Power
    0

    Default Re: New user needing help adding FocusListener event to selectAll in JTextField

    Thanks for your quick reply;

    No it won't compile giving me the error message "FocusListener is abstract ; cannot be instantiated"

    To be honest i was trying to take what I had learned about using ActionListener and apply it to FocusListener obviously I am missing the point somewhere. Do you have any advice as to how to correctly add this function to my program? I am just about as new as they get and am taking a class to learn some of the basics, but I always bite off ore than I can chew..

    Thanks again I will see if your comment can lead me in the right direction now that you stated it the error message makes more sense.

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

    Default Re: New user needing help adding FocusListener event to selectAll in JTextField

    In the future you'll want to post all compilation errors and all exceptions so we won't have to guess at them.

    So yes, you can't instantiate a naked interface or abstract class in that way. You can however do something close -- create an anonymous inner class and use that for your FocusListener, ActionListener or sub-type of any other class or interface. For instance:

    Java Code:
          FocusListener myFocusListener = new FocusListener() {
             
             @Override
             public void focusLost(FocusEvent evt) {
                // do nothing
             }
             
             @Override
             public void focusGained(FocusEvent evt) {
                JTextComponent txtComponent = (JTextComponent)evt.getSource();
                txtComponent.selectAll();
             }
          };
    A FocusListener written like this could be added to any JTextComponent (JTextField, JTextArea, JEditorPane, JTextPane) and would work since it gets the component from the FocusEvent's getSource() method. For example we could add this single FocusListener to a bunch of JTextFields and it would work:

    Java Code:
    import java.awt.event.FocusEvent;
    import java.awt.event.FocusListener;
    
    import javax.swing.*;
    import javax.swing.text.JTextComponent;
    
    public class FooSwing  {
    
       public static void main(String[] args) {
          FocusListener myFocusListener = new FocusListener() {
             
             @Override
             public void focusLost(FocusEvent evt) {
                // do nothing
             }
             
             @Override
             public void focusGained(FocusEvent evt) {
                JTextComponent txtComponent = (JTextComponent)evt.getSource();
                txtComponent.selectAll();
             }
          };
          
          JPanel panel = new JPanel();
          JTextField[] textFields = new JTextField[4];
          for (int i = 0; i < textFields.length; i++) {
             textFields[i] = new JTextField("Hello Text Field " + (i + 1), 15);
             textFields[i].addFocusListener(myFocusListener);
             panel.add(textFields[i]);
          }
          
          JOptionPane.showMessageDialog(null, panel);
       }
    }

  5. #5
    leroycorbid is offline Member
    Join Date
    Feb 2012
    Posts
    4
    Rep Power
    0

    Default Re: New user needing help adding FocusListener event to selectAll in JTextField

    Thank you so much for your postings. After reading your first reply I reconstructed my theory and it all clicked. The book threw out the keyword implements without explaining what it did. So after some thinking I was able to make it work as an interface, once that was working I relized what i was doing with the user classes to make me able to turn it into an instance like you suggested in your second post and I was able to get it to work that way also..
    Thank you so much here is a copy of my final code
    Java Code:
    /***********************************************************************
    ************************************************************************
    ***** GUI Temperature conversion utility  By LeRoy on 2/22/2012  *********
    ************************************************************************/
    
    			import javax.swing.*;
    			import java.awt.event.*;
    			import java.awt.*;
    			
    			
    public class GUI_TEMP_TEST extends JFrame 
      {
    //         ***********************DEFINE CONSTANTS**********************
    			private static final double F_TO_C = 5.0/9.0;
    			private static final double C_TO_F = 9.0/5.0;
    			private static final int    OFFSET = 32;
    			
     //        **********************DEFINE GUI OBJECTS*********************
     
    			private JLabel title, celciusLabel, fahrenLabel, blank1, blank2, blank3, blank4;
    			private JTextField celciusText, fahrenText;
    			private JButton exitBut;
    			private JPanel P1,P2,P3,P4,P5,P6;
    			
    //          *********************DEFINE ACTION HANDLERS********************
    
    			private exitButHandler ebHandler;
    			private celsHandler cHandler;
    			private fahrHandler fHandler;
    			private fListener listener;
    		
    	public GUI_TEMP_TEST()
    	  {
    //         ********************** DEFINE WINDOW **********************
    
    			setTitle("  CELCIUS TO FAHRENHEIT CONVERSION UTILITY   ");
    			
    			
    //         ***********************DEFINE OBJECTS***********************
    		
    				   title = new JLabel(" Enter Either Value for Conversion: ",SwingConstants.CENTER);
    			celciusLabel = new JLabel(" Celcius value: ",SwingConstants.RIGHT);
    			 fahrenLabel = new JLabel(" Fahrenheit value: ", SwingConstants.RIGHT);
    				  blank1 = new JLabel("     ",SwingConstants.CENTER);
    				  blank2 = new JLabel("     ",SwingConstants.CENTER);
    				  blank3 = new JLabel("     ",SwingConstants.CENTER);
    				  blank4 = new JLabel("     ",SwingConstants.CENTER);
    			  
    			 celciusText = new JTextField(5);
    			  fahrenText = new JTextField(5);		  
    		         exitBut = new JButton("Exit");
    				 
    //          *****************ADD FOCUS LISTENER******************
    			listener = new fListener();	 
    			celciusText.addFocusListener(listener);
    			fahrenText.addFocusListener(listener);
    		  
    //          **********************DEFINE CONTAINERS********************
    			Container P1 = getContentPane();
    
    			JPanel P2 = new JPanel();
    			JPanel P3 = new JPanel();
    			JPanel P4 = new JPanel();
    			JPanel P5 = new JPanel();
    			JPanel P6 = new JPanel();
    		  
    			P1.setLayout(new GridLayout(3,1,10,2));
    			P2.setLayout(new GridLayout(1,1));
    			P2.setBackground(Color.blue);
    			P3.setLayout(new GridLayout(1,4));
    			P4.setLayout(new GridLayout(1,3));
    			P5.setLayout(new GridLayout(1,2));
    			P6.setLayout(new GridLayout(1,2));
    		  
    //          ***********************ADD COMPONENTS**************************
    		
    			P2.add(title);
    				
    			P4.add(blank1);
    			P4.add(exitBut);
    			P4.add(blank2);
    		
    			P5.add(fahrenText);
    			P5.add(blank3);
    		
    			P6.add(celciusText);
    			P6.add(blank4);
    		
    			P3.add(fahrenLabel);
    			P3.add(P5);
    			P3.add(celciusLabel);
    			P3.add(P6);
    		
    			P1.add(P2);
    			P1.add(P3);
    			P1.add(P4);
    		
    //          ***********************ADD ACTION LISTENERS***************************
    
    			ebHandler = new exitButHandler();
    			exitBut.addActionListener(ebHandler);
    		
    			cHandler = new celsHandler();
    			celciusText.addActionListener(cHandler);
    		
    			fHandler = new fahrHandler();
    			fahrenText.addActionListener(fHandler);
    					
    						
    //          ***********************WINDOW DEFINITIONS*****************************
    		
    			setSize(425,125);
    			setVisible(true);
    			setDefaultCloseOperation(EXIT_ON_CLOSE);
    	  }
    	  
    //         *************************SET EVENTS*************************************
    		  
    		 private class exitButHandler implements ActionListener
    		   {
    			  public void actionPerformed(ActionEvent e)
    			    {
    					System.exit(0);
    				}
    			}
    			
    					
    		private class celsHandler implements ActionListener
    		
    		   {
    				public void actionPerformed(ActionEvent e)
    				  {
    						double celcius,fahrenheit;
    					
    					celcius = Double.parseDouble(celciusText.getText());
    					fahrenheit = celcius * C_TO_F + OFFSET;
    					fahrenText.setText(String.format(" %.2f",fahrenheit));
    				
    					exitBut.requestFocus();
    				 
    				 }
    		   }
    		   
    		   private class fahrHandler implements ActionListener
    		
    		   {
    				public void actionPerformed(ActionEvent e)
    				  {
    						double celcius,fahrenheit;
    					
    					fahrenheit = Double.parseDouble(fahrenText.getText());
    					celcius = (fahrenheit - OFFSET)* F_TO_C ;
    					celciusText.setText(String.format(" %.2f",celcius));
    					
    					exitBut.requestFocus();
    				  }
    		    }
    		   
    		   private class fListener implements FocusListener
    		     {
    		    
    				public void focusGained(FocusEvent e)
    					{
    						
    						if(e.getComponent() instanceof JTextField)
                            ((JTextField)(e.getComponent())).selectAll();
    					}
    				public void focusLost(FocusEvent e)
    					{
    						
    					}
    			  }
    					
    				
    		public static void main(String[] args)
    		  {
    			GUI_TEMP_TEST testObject = new GUI_TEMP_TEST();
    		  }
    	}
    Thanks again LeRoy
    Last edited by leroycorbid; 02-25-2012 at 08:18 AM.

  6. #6
    leroycorbid is offline Member
    Join Date
    Feb 2012
    Posts
    4
    Rep Power
    0

    Default Re: New user needing help adding FocusListener event to selectAll in JTextField

    Quick question how did you post your code in the java format in your reply?

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

    Default Re: New user needing help adding FocusListener event to selectAll in JTextField

    To format your code, place the tag [java] above the pasted code block and tag [/java] below the code block. You can edit your posts above and put in these tags right now if you wish.

Similar Threads

  1. selectAll() method in a JFormattedTextField
    By jiffi in forum New To Java
    Replies: 4
    Last Post: 12-16-2011, 07:09 AM
  2. Adding List to JTextField
    By nipuna in forum AWT / Swing
    Replies: 7
    Last Post: 09-09-2010, 07:28 AM
  3. Adding jTextField() content to the database?
    By Stephen Douglas in forum New To Java
    Replies: 12
    Last Post: 04-08-2010, 10:50 AM
  4. having trouble with event handling and JTextField
    By v1nsai in forum New To Java
    Replies: 5
    Last Post: 06-01-2009, 03:19 PM
  5. Replies: 4
    Last Post: 03-25-2009, 05:54 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
  •