Results 1 to 4 of 4
  1. #1
    mikhl is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default Set JTextField editable

    Hi, :D

    I need to be able to click a JButton and have the JTextField to set so that it cannot be edited.

    The code below id the listener that I am using to change the JTextField to an editable one or an uneditable one.

    What am I doing wrong?

    Java Code:
    public void actionPerformed(ActionEvent evt)
    {
        if( evt.getActionCommand().equals("editableBtn") )
        {
              if(canEdit)
             {
                 inResult.setEditable(false);
             }
             else
             }
                 inResult.setEditable(true);
             }
         }
    }
    Thanks

  2. #2
    yellowledbet is offline Senior Member
    Join Date
    Feb 2011
    Location
    Georgia, USA
    Posts
    122
    Rep Power
    0

    Default

    It is hard to tell without more code. However, assuming that inResult is the JTextField you want to enable and disable this portion of your code is right.

    Java Code:
    inResult.setEditable(false);
    I would look first at this statement:
    Java Code:
    if( evt.getActionCommand().equals("editableBtn") )
    then at this one:
    Java Code:
    if(canEdit)
    I am not saying they are wrong but if the comparisons are not written correctly it would be what is preventing you from getting to the statement that enables and disables your text field.

    maybe add a line to System.out to make sure you are getting inside the if statement you think you are.

    Java Code:
    System.out.println("Made in First if statement")
    etc...

    One other thing is to make sure you added an actionlistener to the component with this actionCommand: "editableBtn"
    Last edited by yellowledbet; 03-28-2011 at 06:36 PM.

  3. #3
    mikhl is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default

    I have tried your System.out.print suggestion and I it is not hitting the if statement.

    I have tried to look for the problem, but I just cant see it.

    Here is my whole code if you would be kind enough to look through it.

    Java Code:
    import java.awt.*; 
    import javax.swing.*; 
    import java.awt.event.*;
    
    public class GUI extends JFrame implements ActionListener
    {
        // instance variables
        JLabel result;
        JTextField inResult;
        JButton print;
        JButton editable;
        JButton colour;
    
        boolean odd = true;
        boolean canEdit = true;
        /**
         * Constructor for objects of class GUI
         */
        public GUI()
        {
            super("HW3 GUI");
            makeFrame();
            showFrame();
        }
    
        private void makeFrame()
        {
            setLayout (new GridLayout(2, 3));
    
            result = new JLabel("Result:");
            
            inResult = new JTextField();
            inResult.setEditable(true);
            
            print = new JButton("Print");
            print.setActionCommand("printBtn");
            print.addActionListener(this);
            
            editable = new JButton("Editable");
            editable.setActionCommand("editableBtn");
            
            colour = new JButton("Colour");
            colour.setActionCommand("colourBtn");
            colour.addActionListener(this);
    
            add(result);
            add(inResult);
            add(print);
            add(editable);
            add(colour);
        }
    
        private void showFrame()
        {
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setLocationRelativeTo(null);
            setSize(500, 100);
            setVisible(true);
        }
    
        public void actionPerformed(ActionEvent evt)
        {
            if( evt.getActionCommand().equals("colourBtn")  )
            {
                if(odd)
                {
                    getContentPane().setBackground(Color.blue);
                    odd = false;
                }
                else
                {
                    getContentPane().setBackground(Color.white);
                    odd = true;
                }
            }
            else if( evt.getActionCommand().equals("printBtn") )
            {
                String text = inResult.getText();
                inResult.setText("");
                System.out.println(text);
            }
            else if( evt.getActionCommand().equals("editableBtn") )
            {
                if(canEdit)
                {
                    inResult.setEditable(false);
                    canEdit = false;
                    System.out.println("test");
                }
                else
                {
                    inResult.setEditable(true);
                    canEdit = true;
                    System.out.println("test1");
                }
            }
        }
    Thanks

  4. #4
    mikhl is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default

    Sorry I see it.
    Thanks you helped. it was a missing action listner :)

Similar Threads

  1. Editable Calendar
    By anithajerome in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 01-14-2011, 04:57 AM
  2. editable row in jxtable
    By new2java2009 in forum New To Java
    Replies: 0
    Last Post: 12-30-2010, 07:43 PM
  3. Identify JTextField and JComboBox (editable)
    By mine0926 in forum New To Java
    Replies: 2
    Last Post: 07-09-2010, 04:36 PM
  4. PDF - Editable/Writable
    By ananddevaraj in forum Java Servlet
    Replies: 0
    Last Post: 06-05-2010, 10:46 AM
  5. Replies: 1
    Last Post: 01-30-2009, 06:44 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
  •