Results 1 to 5 of 5
  1. #1
    jiffi is offline Member
    Join Date
    Oct 2011
    Posts
    79
    Rep Power
    0

    Default selectAll() method in a JFormattedTextField

    Hello,

    I tried making a focus listener that selects the whole text in a JFormatted Text Field. The problem is, the text is only selected when the user presses tab to select the text field. When he clicks on it with the mouse, text is not selected

    Here is my code:
    Java Code:
    @Override
        public void focusGained(FocusEvent event)
        {
    
            JFormattedTextField source = (JFormattedTextField)event.getSource();
    
            if (source == redNumberLabel)
            {
                redNumberLabel.setText(redNumberLabel.getText())
                redNumberLabel.selectAll();
    
            }
    }
    Any suggestions?

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

    Default Re: selectAll() method in a JFormattedTextField

    What is the purpose of this line?

    Java Code:
    redNumberLabel.setText(redNumberLabel.getText())
    It seems redundant at best.

    Can you create a small compilable and runnable program that fully demonstrates your problem?

  3. #3
    jiffi is offline Member
    Join Date
    Oct 2011
    Posts
    79
    Rep Power
    0

    Default Re: selectAll() method in a JFormattedTextField

    I do not know the exact purpose of this line, I was trying a few things out and this seemed to make the code work. I have no idea why though.

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.lang.*;
    import javax.swing.event.*;
    import java.util.*;
    import java.text.*;
    import javax.swing.text.*;
    import java.beans.PropertyChangeListener;
    import java.beans.PropertyChangeEvent;
    import java.awt.event.*;
    import java.awt.image.*;
    import java.io.*;
    import java.net.*;
    import javax.imageio.*;
    import javax.swing.plaf.ColorUIResource;
    
    public class Sliders extends JFrame implements ChangeListener, PropertyChangeListener, FocusListener
    {
        public static final int colorMin = 0;//min color for sliders
        public static final int colorMax = 255;//max color for the sliders
        public static final int colorStart = 0;    //initial colors for the sliders    
    
        Container colorFrame;
        public JFormattedTextField redNumberLabel;// the 3 labels are to show the actual
        public JFormattedTextField greenNumberLabel;//number of the sliders at every single moment
        public JFormattedTextField blueNumberLabel;
    
        public JSlider  redSlider;//red slider for colors
        public JSlider  greenSlider;//green slider for colors
        public JSlider  blueSlider;//blue slider for colors
        public JLabel redLabel;//show text red
        public JLabel greenLabel;//show text green
        public JLabel blueLabel;//show text blue
    
        public int red = 0;//declare ints for the color chooser
        public int green = 0;
        public int blue = 0;
    
        public void process ()//default method
        {
    
        }
    
        public Sliders()
        {
            red = 0;//set all three colors to 0
            green = 0;
            blue = 0;
    
            colorFrame = getContentPane();   //initialize content pane
    
            setTitle("Color Frame");
            setSize(300,400);
            setLocationRelativeTo(null);
            setResizable(false);
            setVisible(true);
    
            colorFrame.setLayout(null);//set default layout
            colorFrame.setBackground(new ColorUIResource(red,green,blue));//set colors depending on red,green, and blue
    
            redSlider = new JSlider (JSlider.HORIZONTAL,//say that it is a horizontal slider
                colorMin,colorMax,colorStart);//set the min number, max number, and start number
            redSlider.setMajorTickSpacing(51);//set spacing between big ticks
            redSlider.setMinorTickSpacing(5);//set spacing beween small ticks
            redSlider.setPaintTicks(true);//paint the tikcs
            redSlider.setPaintLabels(true);//paint the number
            redSlider.setVisible(true);//show it
            redSlider.addChangeListener(this);//listen to it
            redSlider.setFocusable(false);//do not allow focus so that tab does not highlight it
            redSlider.setForeground(Color.white);//set the color of the font
            redSlider.setBounds(25,60,250,40);//set the bouds
            colorFrame.add(redSlider);//add it to the frame
    
            greenSlider = new JSlider (JSlider.HORIZONTAL,
                colorMin,colorMax,colorStart);
            greenSlider.setMajorTickSpacing(51);
            greenSlider.setMinorTickSpacing(5);
            greenSlider.setPaintTicks(true);
            greenSlider.setPaintLabels(true);
            greenSlider.setVisible(true);
            greenSlider.setFocusable(false);
            greenSlider.addChangeListener(this);
            greenSlider.setForeground(Color.white);
            greenSlider.setBounds(25,160,250,40);
            colorFrame.add(greenSlider);
    
            blueSlider = new JSlider (JSlider.HORIZONTAL,
                colorMin,colorMax,colorStart);
            blueSlider.setMajorTickSpacing(51);
            blueSlider.setMinorTickSpacing(5);
            blueSlider.setPaintTicks(true);
            blueSlider.setPaintLabels(true);
            blueSlider.setVisible(true);
            blueSlider.addChangeListener(this);
            blueSlider.setFocusable(false);
            blueSlider.setBounds(25,260,250,40);
            blueSlider.setForeground(Color.white);
            colorFrame.add(blueSlider); 
    
            redLabel = new JLabel("Red");//red written over red slider
            redLabel.setBounds((300-35)/2,15,35,53);//bounds
            redLabel.setForeground(Color.white);// color of font
            redLabel.setVisible(true);//show it
            colorFrame.add(redLabel);//add it
    
            redNumberLabel = new JFormattedTextField();//number showed from the slider. It is displayed center of the label
            redNumberLabel.setValue(0);//set the default value visible on screen
            redNumberLabel.setBounds(((300-35)/2)+45, 30 ,40,25);//bounds
            redNumberLabel.addPropertyChangeListener("value",this);//listen to changes in values
            redNumberLabel.addFocusListener(this);//listen when this item gets the focus from pressing tab (or clicking on it)
            redNumberLabel.setVisible(true);//show it
            colorFrame.add(redNumberLabel);//add it
    
            greenLabel = new JLabel("Green");
            greenLabel.setBounds((300-45)/2,115,45,53);
            greenLabel.setForeground(Color.white);
            greenLabel.setVisible(true);
            colorFrame.add(greenLabel);
    
            greenNumberLabel = new JFormattedTextField();
            greenNumberLabel.setValue(0);
            greenNumberLabel.setBounds(((300-45)/2)+50, 130,40,25);
            greenNumberLabel.addPropertyChangeListener("value",this);
            greenNumberLabel.addFocusListener(this);
            greenNumberLabel.setVisible(true);
            colorFrame.add(greenNumberLabel);
    
            blueLabel = new JLabel("Blue");
            blueLabel.setBounds((300-35)/2,215,35,53);
            blueLabel.setForeground(Color.white);
            blueLabel.setVisible(true);
            colorFrame.add(blueLabel);
    
            blueNumberLabel = new JFormattedTextField();
            blueNumberLabel.setValue(0);
            blueNumberLabel.setBounds(((300-35)/2)+45, 230,40,25);
            blueNumberLabel.addPropertyChangeListener("value",this);
            blueNumberLabel.addFocusListener(this);
            blueNumberLabel.setVisible(true);
            colorFrame.add(blueNumberLabel);
    
            JLabel uselessLabel = new JLabel();//debug label, used to correct a bug with the blue label 
            uselessLabel.setBounds((300-35)/2,210,35,53);//remove the label and you will see the bug it's so weird!!
            uselessLabel.setVisible(true);
            colorFrame.add(uselessLabel);
    
            colorFrame.setVisible(true);//show the color frame
        }
    
        public void stateChanged(ChangeEvent event)//to know when the sliders were changed
        {
            JSlider source = (JSlider)event.getSource();//get the name of the sliders
    
            if (source == redSlider)
            {
                red = redSlider.getValue();//get the actual value of the sliders
            }
            if (source == greenSlider)
            {
                green = greenSlider.getValue();
            }
            if (source == blueSlider)
            {
                blue = blueSlider.getValue();
            }
            redNumberLabel.setValue(red);//set the text as the value of the sliders
            redNumberLabel.setForeground(colorFrame.getBackground());//set the color of the text as the UIResource color of all 3 sliders
            blueNumberLabel.setValue(blue);
            blueNumberLabel.setForeground(colorFrame.getBackground());
            greenNumberLabel.setValue(green);
            greenNumberLabel.setForeground(colorFrame.getBackground());
    
            if (red >= 153 && blue >= 153 || blue >= 230 || green >= 153 || red >= 220)//when those numbers are exceeded,
            //change color of font so that it stays visible
            {
                redSlider.setForeground(Color.black);
                blueSlider.setForeground(Color.black);
                greenSlider.setForeground(Color.black);
                redLabel.setForeground(Color.black);
                blueLabel.setForeground(Color.black);
                greenLabel.setForeground(Color.black);
                redNumberLabel.setBackground(Color.black);
                greenNumberLabel.setBackground(Color.black);
                blueNumberLabel.setBackground(Color.black);
            }
            else
            if (red < 153 && blue < 153 || blue < 230 && green < 153 && red < 220)//when those numbers are not exceeded,
            //put the font color back to white so that it stays visible
            {
                redSlider.setForeground(Color.white);
                blueSlider.setForeground(Color.white);
                greenSlider.setForeground(Color.white);
                redLabel.setForeground(Color.white);
                blueLabel.setForeground(Color.white);
                greenLabel.setForeground(Color.white);
                redNumberLabel.setBackground(Color.white);
                greenNumberLabel.setBackground(Color.white);
                blueNumberLabel.setBackground(Color.white);
            }
    
            colorFrame.setBackground(new ColorUIResource(red,green,blue));//set backround color of the frame depending on the sliders
    
            try//write the sliders to a file to save the values
            {
                File outFile = new File("Switches/Colors.txt");
                FileOutputStream outFileStream = new FileOutputStream(outFile);
                PrintWriter outStream = new PrintWriter(outFileStream);
    
                outStream.println(red);
                outStream.println(green);
                outStream.println(blue);
    
                outStream.close();
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
    
        }
    
        public void propertyChange(PropertyChangeEvent event)//listen to the formated textFields to know when their values change
        {                                                    // either by user entry or by the sliders
            JFormattedTextField check = (JFormattedTextField)event.getSource();//get the source of the sliders
            int tempR;//create temporary ints that will set the colors
            int tempG;
            int tempB;
    
            /*
             * FOR THE FOLOWING:
             * get name of formatted textField
             * get its value
             * check if the value is between 0 and 255, if it is not, adjust it
             * set the value to the sliders
             * also set the values in the formatted text field because their values don't change automatically
             * user has to press enter so when he enters bad values and presses enter and the value is changed,
             * the correct value does not appear.
             */
            if (check == redNumberLabel)
            {
                tempR = ((Number)redNumberLabel.getValue()).intValue();
                if (tempR > 255)
                {
                    tempR = 255;
                }
                if (tempR < 0)
                {
                    tempR = 0;
                }
                redSlider.setValue(tempR);
                redNumberLabel.setValue(tempR);
            }
            if (check == greenNumberLabel)
            {
                tempG = ((Number)greenNumberLabel.getValue()).intValue();
                if (tempG > 255)
                {
                    tempG = 255;
                }
                if (tempG < 0)
                {
                    tempG = 0;
                }
                greenSlider.setValue(tempG);
                greenNumberLabel.setValue(tempG);
            }
            if (check == blueNumberLabel)
            {
                tempB = ((Number)blueNumberLabel.getValue()).intValue();
                if (tempB > 255)
                {
                    tempB = 255;
                }
                if (tempB < 0)
                {
                    tempB = 0;
                }
                blueSlider.setValue(tempB);
                blueNumberLabel.setValue(tempB);
            }
            redNumberLabel.setForeground(colorFrame.getBackground());//set the color of the text as the UIResource color of all 3 sliders
            blueNumberLabel.setForeground(colorFrame.getBackground());
            greenNumberLabel.setForeground(colorFrame.getBackground());
        }
    
        @Override
        public void focusGained(FocusEvent event)
        {
    
            JFormattedTextField source = (JFormattedTextField)event.getSource();//get the name of the items
    
            source.setText(source.getText()); 
     
            source.selectAll();//select everything in the textField
    
     
        }
    
        public void focusLost(FocusEvent event)//used only to override method FocusListener
        {
        }
        
        public static void main (String[] args)
        {
            Sliders sl = new Sliders();
            sl.setVisible(true);
        }
    }

    (Sorry if this isn't short, but this class was already working by itselft, might as well just add the main method and give you the whole thing)
    Last edited by jiffi; 12-16-2011 at 05:58 AM. Reason: remove action performed, method useless in this example

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,234
    Rep Power
    19

    Default Re: selectAll() method in a JFormattedTextField

    You might want to wrap the selectAll() in a SwingUtilities#invokeLater so that all processing of the mouse click (which will place the caret at an appropriate position in the text, removing any selection in the process) is completed before the text is selected.

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

  5. #5
    jiffi is offline Member
    Join Date
    Oct 2011
    Posts
    79
    Rep Power
    0

    Default Re: selectAll() method in a JFormattedTextField

    I wrapped it in the swingUtilities, and it still doesn't hightlight it (though it does when I press tab). I used a System.out.println() to be sure that the method executes properly, and it does.

    I am not sure about what syntax to use though, I have never used this before

    Java Code:
    SwingUtilities.invokeLater(new Runnable() {
                    public void run() 
                    {
                        source.setText(source.getText()); 
    
                        source.selectAll();//select everything in the textField
                        System.out.println("works");
                    }
                });

Similar Threads

  1. Old value reappears in JFormattedTextField
    By Victoria in forum AWT / Swing
    Replies: 7
    Last Post: 12-13-2011, 01:20 AM
  2. JFormattedTextField and MaskFormatter
    By javaw in forum AWT / Swing
    Replies: 1
    Last Post: 04-20-2010, 06:42 PM
  3. JFormattedTextField + SimpleDateFormat
    By Ralphw in forum AWT / Swing
    Replies: 1
    Last Post: 07-07-2009, 10:53 AM
  4. Problems with JFormattedTextField
    By Gatts79 in forum New To Java
    Replies: 0
    Last Post: 10-03-2008, 04:59 PM
  5. JFormattedTextField Issue
    By teracomp in forum AWT / Swing
    Replies: 1
    Last Post: 01-21-2008, 06:55 PM

Posting Permissions

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