Results 1 to 3 of 3
  1. #1
    unhurt is offline Member
    Join Date
    Oct 2007
    Posts
    21
    Rep Power
    0

    Question how to work with MutableAttributeSet or SimpleAttributeSet

    hi, im new to this forums, can i say hi here?

    what im trying to figure out is how do i work with those 2(or 3 if counted StyledEditorKit)

    im trying to do a simple typing program, after my char on my JTextPane matches my input from my KeyEvent, the colour will change to my preset colour(mark as correct) i can't figure out how do i use mutable or simple, i know how to go around with highlighting the next character with the caret by using forwardAction but i just don't know how to change the colour from black to anything

    i appreciate it if you could show some codes depicting when my char is selected and how the method is call or used to change the colour, thank you in advance
    Last edited by unhurt; 10-31-2007 at 10:02 AM.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.text.*;
    
    public class TextPaneExample implements ActionListener {
        JTextPane textPane;
        Style[] styles;
    
        public void actionPerformed(ActionEvent e) {
            int start = textPane.getSelectionStart();
            int end = textPane.getSelectionEnd();
            if(start != -1 && end != -1 && start != end) {
                int index = Integer.parseInt(e.getActionCommand());
                StyledDocument doc = textPane.getStyledDocument();
                int length = end - start;
                doc.setCharacterAttributes(start, length, styles[index], false);
            }
        }
    
        private JScrollPane getTextComponent() {
            System.setProperty("swing.aatext", "true");
            String text = "This component models paragraphs that are composed " +
                "of runs of character level attributes. Each paragraph may " +
                "have a logical style attached to it which contains the default " +
                "attributes to use if not overridden by attributes set on the " +
                "paragraph or character run. Components and images may be " +
                "embedded in the flow of text.";
            textPane = new JTextPane();
            StyledDocument doc = textPane.getStyledDocument();
            Style logicalStyle = doc.addStyle("base", null);
            StyleConstants.setFontFamily(logicalStyle, "Lucida Sans Unicode");
            StyleConstants.setFontSize(logicalStyle, 20);
            StyleConstants.setLineSpacing(logicalStyle, 0.125f);
            doc.setLogicalStyle(0, logicalStyle);
            try {
                doc.insertString(0, text, logicalStyle);
            } catch(BadLocationException e) {
                System.out.println("BadLocation: " + e.getMessage());
            }
            return new JScrollPane(textPane);
        }
    
        private Box getButtonBox() {
            Color[] colors = {
                Color.red, Color.green, Color.pink, Color.orange
            };
            createStyles(colors);
            Dimension d = new Dimension(75, 25);
            Box box = Box.createHorizontalBox();
            box.setBorder(BorderFactory.createTitledBorder("set " +
                                "background for selected text"));
            box.add(Box.createHorizontalGlue());
            for(int j = 0; j < colors.length; j++) {
                JButton button = new JButton();
                button.setActionCommand(String.valueOf(j));
                button.setPreferredSize(d);
                button.setMinimumSize(d);
                button.setMaximumSize(d);
                button.setBackground(colors[j]);
                button.addActionListener(this);
                box.add(button);
                box.add(Box.createHorizontalGlue());
            }
            return box;
        }
    
        private void createStyles(Color[] colors) {
            styles = new Style[colors.length];
            StyledDocument doc = textPane.getStyledDocument();
            for(int j = 0; j < styles.length; j++) {
                String name = "color " + j;
                styles[j] = doc.addStyle(name, null);
                StyleConstants.setBackground(styles[j], colors[j]);
            }
        }
    
        public static void main(String[] args) {
            TextPaneExample example = new TextPaneExample();
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.getContentPane().add(example.getTextComponent());
            f.getContentPane().add(example.getButtonBox(), "Last");
            f.setSize(400,400);
            f.setLocation(200,200);
            f.setVisible(true);
        }
    }

  3. #3
    unhurt is offline Member
    Join Date
    Oct 2007
    Posts
    21
    Rep Power
    0

    Default

    hey thanks hardwired, for providing the codes, the createStyles is the method that i want, which kinda shock me that it was so easy, as on the web i seen they all used mutable and simple and hash map

Similar Threads

  1. Will this applet ever work?
    By willemjav in forum Java Applets
    Replies: 4
    Last Post: 04-20-2008, 05:40 PM
  2. how would i get this to work...?
    By deeadeed in forum New To Java
    Replies: 6
    Last Post: 12-06-2007, 02:58 AM
  3. Work On Lucene
    By peiceonly in forum Lucene
    Replies: 1
    Last Post: 08-07-2007, 05:47 PM
  4. what do I have to install to work with JSP
    By boy22 in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 08-05-2007, 04:08 AM
  5. Program don't work
    By baltimore in forum New To Java
    Replies: 1
    Last Post: 08-04-2007, 09:51 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
  •