Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: TextArea Editable value does not change

  1. #1
    Member
    Join Date
    Jun 2011
    Posts
    94
    My Mood
    Amazed
    Thanks
    22
    Thanked 1 Time in 1 Post

    Default TextArea Editable value does not change

    Let me paste the code first:
    import java.awt.EventQueue;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JMenuBar;
    import javax.swing.JMenu;
    import javax.swing.JMenuItem;
    import javax.swing.JSplitPane;
    import javax.swing.JTextArea;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
     
     
    public class SolveItHere extends JFrame {
     
     
    	private JTextArea textOne;
    	private JTextArea textTwo;
    	private JMenu mnMod;
    	private static SolveItHere frame = new SolveItHere();;
    		private boolean writeMode = true;
     
    	public static void main(String[] args) {
     
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					frame.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});		
    	}
     
    	public SolveItHere() {
    		setBounds(100, 100, 450, 465);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    		JMenuBar menuBar = new JMenuBar();
    		setJMenuBar(menuBar);
     
    		mnMod = new JMenu("Mod");
    		menuBar.add(mnMod);
     
    		JMenuItem mmEdit = new JMenuItem("Edit Mode");
    		mmEdit.addMouseListener(new MouseAdapter() {
    			@Override
    			public void mousePressed(MouseEvent e) {
    				writeMode = true;
    				//see if it is changed
    				System.out.println(writeMode);
    				frame.revalidate();
    				frame.repaint();
     
    			}
    		});
    		mnMod.add(mmEdit);
     
    		JMenuItem mmView = new JMenuItem("View Mode");
    		mmView.addMouseListener(new MouseAdapter() {
    			@Override
    			public void mousePressed(MouseEvent e) {
    				writeMode = false;
    				//see if it is changed
    				System.out.println(writeMode);
    				frame.revalidate();
    				frame.repaint();
     
    			}
    		});
    		mnMod.add(mmView);
     
    		JMenu mnFile = new JMenu("File");
    		menuBar.add(mnFile);
     
    		JMenuItem mnOpen = new JMenuItem("Open");
     
    		mnFile.add(mnOpen);
     
    		JMenuItem mnSave = new JMenuItem("Save");
     
    		mnFile.add(mnSave);
     
    		JMenuItem mnDelete = new JMenuItem("Delete");
    		mnFile.add(mnDelete);
     
    		JSplitPane splitPane = new JSplitPane();
    		splitPane.setResizeWeight(0.5);
    		getContentPane().add(splitPane);
     
    		JScrollPane scrollPane = new JScrollPane();
    		splitPane.setLeftComponent(scrollPane);
     
    		textOne = new JTextArea();
    		textOne.setEditable(writeMode);
    		scrollPane.setViewportView(textOne);
     
    		JScrollPane scrollPane_1 = new JScrollPane();
    		splitPane.setRightComponent(scrollPane_1);
     
    		textTwo = new JTextArea();
    		textTwo.setEditable(writeMode);
    		scrollPane_1.setViewportView(textTwo);
     
     
    	}
     
    }

    So, textOne and textTwo are two text areas. Their Editable feature is set by a boolean named writeMode. In the beginning, it is set to true. So I can type whatever I want into the areas. However, I want to disable this after a while.
    Under the Mod menu, there are two choices: Edit Mode (mmEdit menu item) and View Mode (mmView menu item). I thought I can perform this task easily by changing the value of writeMode. But nothing happened. So I googled it a little. I found out that revalidate invalidates and revalidates the components. I tried this, but it did not help. Then I tried repaint() with it. That did not help either.

    I also tried revalidating and repainting the textOne and textTwo components. But that failed too.

    So, I want to change the Editable feature of these components, but I cannot do it. Can you guide me please?

    EDIT: I put some sysout statements to see if the value really changes. It does change.
    This frame was created using WindowBuilder.
    It does not give me any errors about anything.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,584
    Thanks
    49
    Thanked 2,233 Times in 2,205 Posts

    Default Re: TextArea Editable value does not change

    by changing the value of writeMode.
    Then you have to pass its value to the object whose state you want to change by calling the set method.

    Is this your problem?
      int x = 5;
      int y = x;  //  copy x's value to y
      x = 7;      //  Give x a new value. This does not change the value of y.
    Values are given to variables as statements are executed. The value is copied to the receiving variable which does not retain a connection to where it got the value from.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jun 2011
    Posts
    94
    My Mood
    Amazed
    Thanks
    22
    Thanked 1 Time in 1 Post

    Default Re: TextArea Editable value does not change

    Thanks for the reply Norm.

    So you say, when I pass a value of a boolean variable to a JTextArea object, it does not get the variable, but it only gets the value of the variable?
    Then when I change the value of the boolean variable (writeMode in this case), the JTextArea does not know about it?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,584
    Thanks
    49
    Thanked 2,233 Times in 2,205 Posts

    Default Re: TextArea Editable value does not change

    Yes. There will not be a connection to the variable that was used to pass the value. The value at the time of the call to the set method was saved and used. No changes to the variable will change the original value that was passed.
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    beer-in-box (February 21st, 2013)

  6. #5
    Member
    Join Date
    Jun 2011
    Posts
    94
    My Mood
    Amazed
    Thanks
    22
    Thanked 1 Time in 1 Post

    Default Re: TextArea Editable value does not change

    Thanks. That made my variable useless for this case.

Similar Threads

  1. [SOLVED] Error for JCombobox Editable
    By remedys in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 30th, 2013, 10:09 PM
  2. How to clear the textArea?
    By dynamix24 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 25th, 2013, 07:46 AM
  3. Editable JTable
    By ellias2007 in forum AWT / Java Swing
    Replies: 1
    Last Post: June 2nd, 2012, 02:26 PM
  4. Non-Editable DateField in J2me
    By thiruv in forum Java ME (Mobile Edition)
    Replies: 0
    Last Post: December 22nd, 2011, 04:03 AM
  5. JTable - multiple lines in a cell AND cells not editable?
    By friday in forum AWT / Java Swing
    Replies: 3
    Last Post: January 10th, 2011, 07:44 AM