Java Code Examples for javax.swing.text.StyledDocument#setParagraphAttributes()

The following examples show how to use javax.swing.text.StyledDocument#setParagraphAttributes() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Main.java    From desktop with The Unlicense 7 votes vote down vote up
/**
 * @main
 */
  
      
public Main() {
    initComponents();
    setLocationRelativeTo(this);
    this.setTitle("EVIL INSULT GENERATOR");
    this.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/com/imgs/app-icon.png")));

    addCombobox();

    AutoCompleteDecorator.decorate(this.cmbLanguage);
    DefaultListCellRenderer dlcr = new DefaultListCellRenderer();
    dlcr.setHorizontalAlignment(DefaultListCellRenderer.CENTER);

    cmbLanguage.setRenderer(dlcr);

    StyledDocument doc = txtPaneShow.getStyledDocument();
    SimpleAttributeSet center = new SimpleAttributeSet();
    StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);
    doc.setParagraphAttributes(0, doc.getLength(), center, false);

    try {

        Document doc1 = Jsoup.connect("http://evilinsult.com/generate_insult.php?lang=en").get();

        Elements links = doc1.select("body");
        for (Element link : links) {
            txtPaneShow.setText("\n" + link.text());
        }
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception ex) {
        txtPaneShow.setText("Insult Outage! Please Check Your Internet Connection And Try Again In Three Minutes");
    }

}
 
Example 2
Source File: SwingTextViewWrapper.java    From CrossMobile with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void setAlignment(int UITextAlignment) {
    if (alignment == UITextAlignment)
        return;
    alignment = UITextAlignment;

    StyledDocument doc = getNativeWidget().txt.getStyledDocument();
    SimpleAttributeSet attr = new SimpleAttributeSet();
    switch (UITextAlignment) {
        case crossmobile.ios.uikit.UITextAlignment.Left:
            StyleConstants.setAlignment(attr, StyleConstants.ALIGN_LEFT);
            break;
        case crossmobile.ios.uikit.UITextAlignment.Right:
            StyleConstants.setAlignment(attr, StyleConstants.ALIGN_RIGHT);
            break;
        default:
            StyleConstants.setAlignment(attr, StyleConstants.ALIGN_CENTER);
    }
    doc.setParagraphAttributes(0, doc.getLength(), attr, false);
}
 
Example 3
Source File: SummaryCellRenderer.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void clearSD (JTextPane pane, StyledDocument sd) {
    try {
        Style noindentStyle = createNoindentStyle(pane);
        sd.remove(0, sd.getLength());
        sd.setParagraphAttributes(0, sd.getLength(), noindentStyle, false);
    } catch (BadLocationException ex) {
        Exceptions.printStackTrace(ex);
    }
}
 
Example 4
Source File: LicenseWindow.java    From Hotel-Properties-Management-System with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create the frame.
 */
public LicenseWindow(final String path) {
	
	setTitle("Coder HPMSA - [License]");
	setBounds(100, 100, 550, 550);
	setBackground(Color.decode("#066d95"));
	setLocationRelativeTo(null);
	setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
	
	this.setIconImage(Toolkit.getDefaultToolkit().
			getImage(getClass().getResource(LOGOPATH)));
	
	final JScrollPane scrollPane = new JScrollPane();
	scrollPane.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
	getContentPane().add(scrollPane, BorderLayout.CENTER);
	
	editorPane = new JTextPane();
	editorPane.setBackground(new Color(255, 255, 240));
	editorPane.setFont(new Font("Verdana", Font.PLAIN, 13));
	editorPane.setBorder(new EtchedBorder(EtchedBorder.RAISED, null, null));
	editorPane.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
	editorPane.setEditable(false);
	scrollPane.setViewportView(editorPane);
	
	final StyledDocument doc = editorPane.getStyledDocument();
	final SimpleAttributeSet center = new SimpleAttributeSet();
	StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);
	doc.setParagraphAttributes(0, doc.getLength()-1, center, false);
	
	fillEditorPane(path);
	setVisible(true);
}
 
Example 5
Source File: UpdateFrame.java    From GIFKR with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void initializeComponents() {
	infoPane		= new JTextPane();
	infoPane.setText("You are using version "+version+" but the current version is " +CalebKussmaul.getLatestVersion(p)+". It is strongly reccomended that you update to take advantage of the latest additions and fixes.");
	
	StyledDocument doc = infoPane.getStyledDocument();
	SimpleAttributeSet center = new SimpleAttributeSet();
	StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);
	doc.setParagraphAttributes(0, doc.getLength(), center, false);
	
	infoPane.setEditable(false);
	infoPane.setOpaque(false);
	updateButton 	= new JButton("Download update...");
}
 
Example 6
Source File: InformationPanel.java    From stendhal with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Set the name of the zone.
 *
 * @param name
 */
void setZoneName(String name) {
	nameField.setText(name);
	StyledDocument doc = nameField.getStyledDocument();
	doc.setParagraphAttributes(0, doc.getLength(), center, false);
	// Necessary when the needed space gets smaller.
	SwingUtilities.invokeLater(new Runnable() {
		@Override
		public void run() {
			nameField.revalidate();
		}
	});
}
 
Example 7
Source File: VersionPane.java    From bigtable-sql with Apache License 2.0 5 votes vote down vote up
/**
 * Renders the content.
 */
private void init() {
    String content = getContent();
    setContentType("text/html");
    StyledDocument doc = getStyledDocument();
    SimpleAttributeSet s = new SimpleAttributeSet();
    StyleConstants.setAlignment(s, StyleConstants.ALIGN_CENTER);
    StyleConstants.setBold(s, true);
    try {
        doc.setParagraphAttributes(0,content.length(), s, false);
        doc.insertString(0, content, s);
        if (_showWebsite) {
        	String webContent = Version.getWebSite();
        	SimpleAttributeSet w = new SimpleAttributeSet();
            StyleConstants.setAlignment(w, StyleConstants.ALIGN_CENTER);
            StyleConstants.setUnderline(w, true);
            SimpleAttributeSet hrefAttr = new SimpleAttributeSet();
            hrefAttr.addAttribute(HTML.Attribute.HREF, Version.getWebSite());
            w.addAttribute(HTML.Tag.A, hrefAttr);
            doc.setParagraphAttributes(content.length(),webContent.length(), w, false);
            doc.insertString(content.length(), webContent, w);
            if (Desktop.isDesktopSupported()){
            	addMouseListener(this);
            	addMouseMotionListener(this);
            }
        }
    } catch (Exception e) {
        s_log.error("init: Unexpected exception "+e.getMessage());
    }
    setOpaque(false);

}
 
Example 8
Source File: HTMLPane.java    From wpcleaner with Apache License 2.0 5 votes vote down vote up
/**
 * Clear text.
 */
public void clearText() {
  Document doc = getDocument();
  if (doc != null) {
    if (doc instanceof StyledDocument) {
      StyledDocument styledDoc = (StyledDocument) doc;
      styledDoc.setCharacterAttributes(0, doc.getLength(), new SimpleAttributeSet(), true);
      styledDoc.setParagraphAttributes(0, doc.getLength(), new SimpleAttributeSet(), true);
    }
  }
  setText("");
}
 
Example 9
Source File: ReadLogsWindow.java    From Hotel-Properties-Management-System with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Create the frame.
 */
public ReadLogsWindow() {
	
	setTitle("Coder HPMSA - [Read Logs]");
	setBounds(100, 100, 660, 550);
	setBackground(Color.decode("#066d95"));
	setLocationRelativeTo(null);
	setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
	
	this.setIconImage(Toolkit.getDefaultToolkit().
			getImage(getClass().getResource(LOGOPATH)));
	
	final JScrollPane scrollPane = new JScrollPane();
	scrollPane.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
	getContentPane().add(scrollPane, BorderLayout.CENTER);
	
	editorPane = new JTextPane();
	editorPane.setBackground(new Color(255, 255, 240));
	editorPane.setFont(new Font("Verdana", Font.PLAIN, 13));
	editorPane.setBorder(new EtchedBorder(EtchedBorder.RAISED, null, null));
	editorPane.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
	editorPane.setEditable(false);
	scrollPane.setViewportView(editorPane);
	
	final JPanel filesPanel = new JPanel();
	filesPanel.setPreferredSize(new Dimension(200, 10));
	getContentPane().add(filesPanel, BorderLayout.EAST);
	filesPanel.setLayout(new BorderLayout(0, 0));
	
	final JScrollPane listScrollPane = new JScrollPane();
	listScrollPane.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
	listScrollPane.setViewportView(logFilesList());
	filesPanel.add(listScrollPane, BorderLayout.CENTER);
	
	final JPanel titlePanel = new JPanel();
	titlePanel.setPreferredSize(new Dimension(10, 40));
	titlePanel.setBackground(Color.decode("#066d95"));
	titlePanel.setAutoscrolls(true);
	getContentPane().add(titlePanel, BorderLayout.NORTH);
	titlePanel.setLayout(new BorderLayout(0, 0));
	
	final JLabel lblTitle = new JLabel("SYSTEM LOG RECORDS");
	lblTitle.setHorizontalTextPosition(SwingConstants.CENTER);
	lblTitle.setHorizontalAlignment(SwingConstants.CENTER);
	lblTitle.setAutoscrolls(true);
	lblTitle.setFont(new Font("Verdana", Font.BOLD, 25));
	lblTitle.setForeground(UIManager.getColor("Button.highlight"));
	titlePanel.add(lblTitle, BorderLayout.CENTER);
	
	final StyledDocument doc = editorPane.getStyledDocument();
	final SimpleAttributeSet center = new SimpleAttributeSet();
	StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);
	doc.setParagraphAttributes(0, doc.getLength(), center, false);
	
	setVisible(true);
}
 
Example 10
Source File: ViewUtils.java    From GIFKR with GNU Lesser General Public License v3.0 4 votes vote down vote up
public static void centerText(JTextPane pane) {
	StyledDocument doc = pane.getStyledDocument();
	SimpleAttributeSet center = new SimpleAttributeSet();
	StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);
	doc.setParagraphAttributes(0, doc.getLength(), center, false);
}
 
Example 11
Source File: AbstractInplaceEditor.java    From workcraft with MIT License 4 votes vote down vote up
public void edit(final String text, final Font font, final Point2D offset, final Alignment alignment, boolean multiline) {
    // Create a text pane without wrapping
    final JTextPane textPane = new JTextPane() {
        @Override
        public boolean getScrollableTracksViewportWidth() {
            return getUI().getPreferredSize(this).width <= getParent().getSize().width;
        }
    };
    textPane.setText(text.replace(NEWLINE_SEPARATOR, "\n"));

    // Align the text
    SimpleAttributeSet attributes = new SimpleAttributeSet();
    StyleConstants.setAlignment(attributes, alignment.toStyleConstant());
    StyledDocument document = textPane.getStyledDocument();
    document.setParagraphAttributes(0, document.getLength(), attributes, false);

    // Set font size similar to the current editor scale
    Viewport viewport = getEditor().getViewport();
    float fontSize = font.getSize2D() * (float) viewport.getTransform().getScaleY();
    textPane.setFont(font.deriveFont(fontSize));

    // Set actions for Shift-Enter, Enter, and Esc
    InputMap input = textPane.getInputMap();
    ActionMap actions = textPane.getActionMap();
    input.put(enter, TEXT_SUBMIT);
    actions.put(TEXT_SUBMIT, new TextSubmitAction());
    input.put(esc, TEXT_CANCEL);
    actions.put(TEXT_CANCEL, new TextCancelAction());
    if (multiline) {
        input.put(shiftEnter, INSERT_BREAK);
    }

    // Add vertical scroll (if necessary)
    final JScrollPane scrollPane = new JScrollPane(textPane);
    if (multiline) {
        scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
        scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    } else {
        scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
        scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
    }

    // Show the text editor panel
    JPanel panel = new JPanel(new BorderLayout());
    getEditor().getOverlay().add(panel);
    panel.setBorder(new EmptyBorder(0, 0, 0, 0));
    panel.add(scrollPane, BorderLayout.CENTER);
    if (multiline) {
        JLabel label = new JLabel("Press Shift-Enter for a new line ");
        label.setBorder(GuiUtils.getEmptyBorder());
        label.setHorizontalAlignment(SwingConstants.RIGHT);
        panel.add(label, BorderLayout.SOUTH);
    }

    // Set the size of the text editor panel
    AffineTransform localToRootTransform = TransformHelper.getTransformToRoot(getComponent());
    Rectangle2D bbRoot = TransformHelper.transform(getComponent(), localToRootTransform).getBoundingBox();
    bbRoot = BoundingBoxHelper.move(bbRoot, offset);
    double dw = 1.0;
    double dh = 0.5;
    if (multiline) {
        dw += bbRoot.getWidth();
        dh += getExtraHeight(text, bbRoot.getHeight());
    }
    Rectangle bbScreen = viewport.userToScreen(BoundingBoxHelper.expand(bbRoot, dw, dh));
    panel.setBounds(bbScreen.x, bbScreen.y, bbScreen.width, bbScreen.height);

    textPane.requestFocusInWindow();
    textPane.addFocusListener(new FocusListenerImplementation(textPane, panel));
}