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.


LinkBack URL
About LinkBacks
Reply With Quote