Swing: How to code "set colors" for the text from the user side?

48 views Asked by At

I must do project for studies (homework) where We upgrade utilities. One my feature is "coloring text".

I never did It, but I have small idea. I want to bind for example $0 to $9 with colors: BLUE,RED,BLACK,WHITE... (screen shot). When I confirm setting "$x" isn't visible but text must have changed color.

I need something information, article or something to learn how to do It. I know, I must do new class with constructor of colors and 2nd class where I have multi-line text and single line text where I should implement this colors -that's all.

Thanks for each help. Regards.

PS. Sorry for my not perfect English

https://i.stack.imgur.com/NVruZ.jpg

enter image description here

Classes where I should implement this option with colors:

package com.horstmann.violet.product.diagram.propertyeditor.baseeditors;

    import javax.swing.*;
    import javax.swing.text.JTextComponent;

    import com.horstmann.violet.product.diagram.property.text.LineText;
    import com.horstmann.violet.product.diagram.property.text.MultiLineText;

    /**
     * A property editor for the MultiLineText type.
     */
    public class MultiLineTextEditor extends LineTextEditor
    {
        protected void setSourceEditor()
        {
            this.source = (MultiLineText) getValue();
        }
        protected LineText getSourceEditor()
        {
            return this.source;
        }
        protected JTextComponent createTextComponent()
        {
            return new JTextArea(ROWS, COLUMNS);
        }

        protected JComponent createScrollPanel(JTextComponent textComponent)
        {
            return new JScrollPane(textComponent);
        }

        private MultiLineText source;

        private static final int ROWS = 5;
    }


    package com.horstmann.violet.product.diagram.propertyeditor.baseeditors;

    import com.horstmann.violet.product.diagram.property.text.LineText;
    import com.horstmann.violet.product.diagram.property.text.SingleLineText;

    import javax.swing.*;
    import javax.swing.text.JTextComponent;

    public class SingleLineTextEditor extends LineTextEditor
    {
        protected void setSourceEditor()
        {
            this.source = (SingleLineText) getValue();
        }
        protected LineText getSourceEditor()
        {
            return this.source;
        }
        protected JTextComponent createTextComponent()
        {
            return new JTextField(COLUMNS);
        }

        private SingleLineText source;
    }

    public abstract class LineTextEditor extends PropertyEditorSupport
    {
        protected abstract void setSourceEditor();
        protected abstract LineText getSourceEditor();
        protected abstract JTextComponent createTextComponent();

        public boolean supportsCustomEditor()
        {
            return true;
        }

        public Component getCustomEditor()
        {
            setSourceEditor();
            final JPanel panel = new JPanel();
            panel.add(getTextEditorComponent());
            return panel;
        }

        private JComponent getTextEditorComponent()
        {
            if (this.textEditorComponent == null)
            {
                final JTextComponent textComponent = createTextComponent();

                textComponent.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, tab);
                textComponent.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, shiftTab);

                textComponent.setText(getSourceEditor().toEdit());
                textComponent.getDocument().addDocumentListener(new DocumentListener()
                {
                    public void insertUpdate(DocumentEvent e)
                    {
                        getSourceEditor().setText(textComponent.getText());
                        firePropertyChange();
                    }

                    public void removeUpdate(DocumentEvent e)
                    {
                        getSourceEditor().setText(textComponent.getText());
                        firePropertyChange();
                    }

                    public void changedUpdate(DocumentEvent e)
                    {
                    }
                });
                this.textEditorComponent = createScrollPanel(textComponent);
            }
            return this.textEditorComponent;
        }

        protected JComponent createScrollPanel(JTextComponent textComponent)
        {
            return textComponent;
        }

        protected JComponent textEditorComponent;

        protected static final int COLUMNS = 30;

        protected static Set<KeyStroke> tab = new HashSet<KeyStroke>(1);
        protected static Set<KeyStroke> shiftTab = new HashSet<KeyStroke>(1);
        static
        {
            tab.add(KeyStroke.getKeyStroke("TAB"));
            shiftTab.add(KeyStroke.getKeyStroke("shift TAB"));
        }
    }
0

There are 0 answers