Results 1 to 3 of 3
- 10-31-2007, 09:59 AM #1
Member
- Join Date
- Oct 2007
- Posts
- 21
- Rep Power
- 0
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 advanceLast edited by unhurt; 10-31-2007 at 10:02 AM.
- 10-31-2007, 09:20 PM #2
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); } }
- 11-01-2007, 06:33 AM #3
Member
- Join Date
- Oct 2007
- Posts
- 21
- Rep Power
- 0
Similar Threads
-
Will this applet ever work?
By willemjav in forum Java AppletsReplies: 4Last Post: 04-20-2008, 05:40 PM -
how would i get this to work...?
By deeadeed in forum New To JavaReplies: 6Last Post: 12-06-2007, 02:58 AM -
Work On Lucene
By peiceonly in forum LuceneReplies: 1Last Post: 08-07-2007, 05:47 PM -
what do I have to install to work with JSP
By boy22 in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 08-05-2007, 04:08 AM -
Program don't work
By baltimore in forum New To JavaReplies: 1Last Post: 08-04-2007, 09:51 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks