Search Results

Search found 6 results on 1 pages for 'defaultstyleddocument'.

Page 1/1 | 1 

  • DefaultStyledDocument.styleChanged(Style style) may not run in a timely manner?

    - by Paul Reiners
    I'm experiencing an intermittent problem with a class that extends javax.swing.text.DefaultStyledDocument. This document is being sent to a printer. Most of the time the formatting of the document looks correct, but once in a while it doesn't. It looks like some of the changes in the formatting have not been applied. I took a look at the DefaultStyledDocument.styleChanged(Style style) code: /** * Called when any of this document's styles have changed. * Subclasses may wish to be intelligent about what gets damaged. * * @param style The Style that has changed. */ protected void styleChanged(Style style) { // Only propagate change updated if have content if (getLength() != 0) { // lazily create a ChangeUpdateRunnable if (updateRunnable == null) { updateRunnable = new ChangeUpdateRunnable(); } // We may get a whole batch of these at once, so only // queue the runnable if it is not already pending synchronized(updateRunnable) { if (!updateRunnable.isPending) { SwingUtilities.invokeLater(updateRunnable); updateRunnable.isPending = true; } } } } /** * When run this creates a change event for the complete document * and fires it. */ class ChangeUpdateRunnable implements Runnable { boolean isPending = false; public void run() { synchronized(this) { isPending = false; } try { writeLock(); DefaultDocumentEvent dde = new DefaultDocumentEvent(0, getLength(), DocumentEvent.EventType.CHANGE); dde.end(); fireChangedUpdate(dde); } finally { writeUnlock(); } } } Does the fact that SwingUtilities.invokeLater(updateRunnable) is called, rather than invokeAndWait(updateRunnable), mean that I can't count on my formatting changes appearing in the document before it is rendered? If that is the case, is there a way to ensure that I don't proceed with rendering until the updates have occurred?

    Read the article

  • How can I set partial text color in JTextArea

    - by ComputerJy
    I want to set color for specific lines in the text area. What I've found so far, was the following // Declarations private final DefaultStyledDocument document; private final MutableAttributeSet homeAttributeSet; private final MutableAttributeSet awayAttributeSet; // Usage in the form constructor jTextAreaLog.setDocument(document); homeAttributeSet = new SimpleAttributeSet(); StyleConstants.setForeground(homeAttributeSet, Color.blue); StyleConstants.setItalic(homeAttributeSet, true); awayAttributeSet = new SimpleAttributeSet(); StyleConstants.setForeground(awayAttributeSet, Color.red); // Setting the style of the last line final int start = jTextAreaLog.getLineStartOffset(jTextAreaLog.getLineCount() - 2); final int length = jTextAreaLog.getLineEndOffset(jTextAreaLog.getLineCount() - 1) - start; document.setCharacterAttributes(start, length, awayAttributeSet, true); But this is not working. What am I doing wrong?

    Read the article

  • Difference between the Document classes

    - by takoi
    I've been reading the javadocs trying to grasp around the swing Document API but I cant get something sensible out of it because there's so many classes: Document, StyledDocument, AbstractDocument, DefaultStyledDocument, PlainDocument, HTMLDocument, and someone mentioned DocumentFilter. This question is more on a general basis so can someone give an overview of the differences between the implementations and when the different interfaces and abstracts are for? For my specific case what I want to achieve is a data structure that will: hold three lines of text only. And attributes must not be per line or document. I will have a couple of thousand of these in some other structure so overhead is important. Anything that i can use for this or is it better to extend something? If so, what?

    Read the article

  • Java Simple Chat Box

    - by Allen
    I am trying to create a very simple chat window that simply has the ability to display some text, which i add to from time to time. However I get the following run time error when attempting to append text to the window: java.lang.ClassCastException: javax.swing.JViewport cannot be cast to javax.swing.JTextPane at ChatBox.getTextPane(ChatBox.java:41) at ChatBox.getDocument(ChatBox.java:45) at ChatBox.addMessage(ChatBox.java:50) at ImageTest2.main(ImageTest2.java:160) Here is the class to handle the basic operations: public class ChatBox extends JScrollPane { private Style style; public ChatBox() { StyleContext context = new StyleContext(); StyledDocument document = new DefaultStyledDocument(context); style = context.getStyle(StyleContext.DEFAULT_STYLE); StyleConstants.setAlignment(style, StyleConstants.ALIGN_LEFT); StyleConstants.setFontSize(style, 14); StyleConstants.setSpaceAbove(style, 4); StyleConstants.setSpaceBelow(style, 4); JTextPane textPane = new JTextPane(document); textPane.setEditable(false); this.add(textPane); } public JTextPane getTextPane() { return (JTextPane) this.getComponent(0); } public StyledDocument getDocument() { return (StyledDocument) getTextPane().getStyledDocument(); } public void addMessage(String speaker, String message) { String combinedMessage = speaker + ": " + message; StyledDocument document = getDocument(); try { document.insertString(document.getLength(), combinedMessage, style); } catch (BadLocationException badLocationException) { System.err.println("Oops"); } } } if there is a simpler way to do this, by all means let me know. I only need the text to be of a single font type, and uneditable by the user. Aside from that, I just need to be able to append text on the fly.

    Read the article

  • How to limit JTextArea max Rows and Coloums?

    - by Billbo bug
    I am using JTextArea in JScrollPane I want to limit the maximum number of lines possible and the maximum chars in each line. I need that the string will be exactly like on the screen, each line will end with '\n' (if there another line after it) and the user will be able to insert only X lines and Y chars in each line. I tried to limit the lines but i don't know exactly how many lines do i have because of the line wrapping, The line wrapping is starting new line visualy on the screen(because of the width of the JTextArea) but in the string of the component it is really the same line with no '\n' to indicate new line. I do not have an idea how to limit the max chars in each line while typing. There are 2 stages: The typing of the string- keep that the user will not be able to type more then X lines and Y chars in each line. (even if the line wrap only visualy or the user typed '/n') Insert the string to the DB- after cliking 'OK' convert the string that every line will end with "/n" even if the user did not typed it and the line was wrapped only visualy. There are few problems if i will count the chars in the line and insert '/n' in the end of the line, thats why i decided to do it in two stages. In the first stage ehile the user is typing i would rather only limit it visualy and force line wrpping or something similar. Only in the second stage when i save string i will add the '/n' even if the user did not typed it in the end of the lines! Does anyone have an idea? I know that i will have to use DocumentFilter OR StyledDocument. Here is sample code that limit only the lines to 3:(but not the chars in row to 19) private JTextArea textArea ; textArea = new JTextArea(3,19); textArea .setLineWrap(true); textArea .setDocument(new LimitedStyledDocument(3)); JScrollPane scrollPane = new JScrollPane(textArea public class LimitedStyledDocument extends DefaultStyledDocument /** Field maxCharacters */ int maxLines; public LimitedStyledDocument(int maxLines) { maxCharacters = maxLines; } public void insertString(int offs, String str, AttributeSet attribute) throws BadLocationException { Element root = this.getDefaultRootElement(); int lineCount = getLineCount(str); if (lineCount + root.getElementCount() <= maxLines){ super.insertString(offs, str, attribute); } else { Toolkit.getDefaultToolkit().beep(); } } /** * get Line Count * * @param str * @return the count of '\n' in the String */ private int getLineCount(String str){ String tempStr = new String(str); int index; int lineCount = 0; while (tempStr.length() > 0){ index = tempStr.indexOf("\n"); if(index != -1){ lineCount++; tempStr = tempStr.substring(index+1); } else{ break; } } return lineCount; } }

    Read the article

1