Search code examples
javaswingjtextareaentertext-editor

Updating dynamic TextArea in Java


I'm trying create simple text editor with dynamic Text Area in Java.

The application, at the beginning, only have 1 Text Area. Each time I press ENTER key, the application will create a new Text Area. Its work! LOL. But, when i try to change the previous text area, that text area not changed. And the problem is because my previous Text Area was already in container. So, my question is how we updating all Text Area in the container?

Look at my code:

    import javax.swing.*;
    import javax.swing.text.*;
    import java.awt.*;
    import java.io.*;
    import java.awt.event.*;

    public class SimpleEditor extends JFrame {

            int count = 0;
            Container content = getContentPane();

            private JTextComponent[] textComp;

            public static void main(String[] args) {
                    SimpleEditor editor = new SimpleEditor();
                    editor.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    editor.setVisible(true);
            }

            // Create an editor.
            public SimpleEditor() {
                    super("Swing Editor");
                    dinamicTA();
                    content.setLayout(new FlowLayout());

                    for(int i=0;i<count;i++) {
                            content.add(textComp[i]);
                    }

                    pack();
                    content.setSize(content.getPreferredSize());
                    pack();
            }

            //create DINAMIC TEXT AREA
            public void dinamicTA () {
                    if(count==0) {
                            textComp = new JTextComponent[1];
                            textComp[0] = createTextComponent();
                            count+=1;
                    }
                    else {
                            JTextComponent[] texttemp;
                            texttemp = new JTextComponent[count];
                            for(int i=0;i<count;i++) {
                                    texttemp[i] = createTextComponent();
                            }
                            count+=1;
                            textComp = new JTextComponent[count];
                            for(int i=0;i<count-1;i++) {
                                    textComp[i] = createTextComponent();
                                    //textComp[i].setText(texttemp[i].getText()+"wow"); <-- not working
                            }
                            textComp[count-1] = createTextComponent();
                            content.add(textComp[count-1]);
                    }
            }

            // Create the JTextComponent subclass.
            protected JTextComponent createTextComponent() {
                    JTextArea ta = new JTextArea();
                    if (count%2==0)
                            ta.setForeground(Color.red);
                    else
                            ta.setForeground(Color.GREEN);
                    ta.setFont(new Font("Courier New",Font.PLAIN,12));
                    ta.setLineWrap(true);                                                                                                                           
                    ta.setWrapStyleWord(true);  
                    ta.addKeyListener(new java.awt.event.KeyAdapter() {
                            public void keyReleased(java.awt.event.KeyEvent ev) {
                                    taKeyReleased(ev);
                            }
                    });

                    ta.setColumns(15);
                    pack();
                    ta.setSize(ta.getPreferredSize());
                    pack();

                    return ta;
            }

            private void taKeyReleased(java.awt.event.KeyEvent ev) { 
                    int key = ev.getKeyCode();
                    if (key == KeyEvent.VK_ENTER) {
                            dinamicTA();

                            pack();
                            content.setSize(content.getPreferredSize());
                            pack();
                    }
            }
    }

And 2 question more. Each time i press ENTER key, text area will be create AND the previous Text Area get a break line. Do you have any idea to remove the break line? Next question: how i go to next Text Area after i press ENTER key without click new Text Area?

Sorry, too many question..hahaha. Thx before :)


Solution

  • For your first questions, I changed the code as `

    import javax.swing.*;
    import javax.swing.text.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class SimpleEditor extends JFrame {
    
            /**
         * 
         */
        private static final long serialVersionUID = 1L;
            int count = 0;
            Container content = getContentPane();
    
            private JTextComponent[] textComp;
            // Create an editor.
            public SimpleEditor() {
                    super("Swing Editor");
                    dinamicTA();
                    content.setLayout(new FlowLayout());
    
                    for(int i=0;i<count;i++) {
                            content.add(textComp[i]);
                    }
    
                    pack();
                    content.setSize(content.getPreferredSize());
                    pack();
            }
    
            //create DINAMIC TEXT AREA
            public void dinamicTA () {
                    if(count==0) {
                            textComp = new JTextComponent[1];
                            textComp[0] = createTextComponent();
                            count+=1;
                    }
                    else {
                            JTextComponent[] texttemp;
                            texttemp = textComp;
                            count+=1;
                            textComp = new JTextComponent[count];
                            for(int i=0;i<count-1;i++) {
                                    textComp[i] = texttemp[i];
                                    textComp[i].setText(textComp[i].getText()+"wow"); //<-- not working
                            }
                            textComp[count-1] = createTextComponent();
                            content.add(textComp[count-1]);
                            textComp[count-1].requestFocus(); //get focus
                    }
            }
    
            // Create the JTextComponent subclass.
            protected JTextComponent createTextComponent() {
                    final JTextArea ta = new JTextArea();
                    if (count%2==0)
                            ta.setForeground(Color.red);
                    else
                            ta.setForeground(Color.GREEN);
                    ta.setFont(new Font("Courier New",Font.PLAIN,12));
                    ta.setLineWrap(true);                                                                                                                           
                    ta.setWrapStyleWord(true);  
                    ta.addKeyListener(new java.awt.event.KeyAdapter() {
                            public void keyReleased(java.awt.event.KeyEvent ev) {
                                    taKeyReleased(ev);
                            }
                    });
    
                    ta.setColumns(15);
                    pack();
                    ta.setSize(ta.getPreferredSize());
                    pack();
    
                    return ta;
            }
    
            private void taKeyReleased(java.awt.event.KeyEvent ev) { 
                    int key = ev.getKeyCode();
                    if (key == KeyEvent.VK_ENTER) {
                            dinamicTA();
                            pack();
                            content.setSize(content.getPreferredSize());
                            pack();
                    }
            }
    
            public static void main(String[] args) {
                SimpleEditor editor = new SimpleEditor();
                editor.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                editor.setVisible(true);
        }    
    }
    

    `

    I think the problem with the original codes is : each time when you add new components, it lost reference to the previous components.