Java Code Examples for javax.swing.text.StyleConstants#setAlignment()

The following examples show how to use javax.swing.text.StyleConstants#setAlignment() . 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: InformationPanel.java    From stendhal with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Create a new InformationPanel.
 */
InformationPanel() {
	setLayout(new OverlayLayout(this));
	JComponent container = SBoxLayout.createContainer(SBoxLayout.VERTICAL);
	glassPane = new JComponent(){};
	add(glassPane);
	add(container);

	// ** Zone name **
	nameField = new JTextPane();
	StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);
	nameField.setAlignmentX(CENTER_ALIGNMENT);
	nameField.setOpaque(true);
	nameField.setBackground(getBackground());
	nameField.setForeground(Color.WHITE);
	nameField.setFocusable(false);
	nameField.setEditable(false);
	container.add(nameField, SLayout.EXPAND_X);

	// ** Danger display **
	dangerIndicator = new DangerIndicator(MAX_SKULLS);
	dangerIndicator.setAlignmentX(CENTER_ALIGNMENT);
	container.add(dangerIndicator);
	// Default to safe, so that we always have a tooltip
	describeDanger(0);
}
 
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: Utility.java    From freecol with GNU General Public License v2.0 5 votes vote down vote up
public static void initStyleContext(Font font) {
    Style defaultStyle = StyleContext.getDefaultStyleContext()
        .getStyle(StyleContext.DEFAULT_STYLE);

    STYLE_CONTEXT = new StyleContext();
    Style regular = STYLE_CONTEXT.addStyle("regular", defaultStyle);
    StyleConstants.setFontFamily(regular, font.getFamily());
    StyleConstants.setFontSize(regular, font.getSize());

    Style buttonStyle = STYLE_CONTEXT.addStyle("button", regular);
    StyleConstants.setForeground(buttonStyle, LINK_COLOR);

    Style right = STYLE_CONTEXT.addStyle("right", regular);
    StyleConstants.setAlignment(right, StyleConstants.ALIGN_RIGHT);
}
 
Example 7
Source File: JViewPortBackingStoreImageTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
static void createStyles() {
    styles = new StyleContext();
    doc = new DefaultStyledDocument(styles);
    contentAttributes = new HashMap<>();

    // no attributes defined
    Style s = styles.addStyle(null, null);
    contentAttributes.put("none", s);

    Style def = styles.getStyle(StyleContext.DEFAULT_STYLE);

    Style heading = styles.addStyle("heading", def);
    StyleConstants.setFontFamily(heading, "SansSerif");
    StyleConstants.setBold(heading, true);
    StyleConstants.setAlignment(heading, StyleConstants.ALIGN_CENTER);
    StyleConstants.setSpaceAbove(heading, 10);
    StyleConstants.setSpaceBelow(heading, 10);
    StyleConstants.setFontSize(heading, 18);

    // Title
    Style sty = styles.addStyle("title", heading);
    StyleConstants.setFontSize(sty, 32);

    // author
    sty = styles.addStyle("author", heading);
    StyleConstants.setItalic(sty, true);
    StyleConstants.setSpaceBelow(sty, 25);
}
 
Example 8
Source File: JViewPortBackingStoreImageTest.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
static void createStyles() {
    styles = new StyleContext();
    doc = new DefaultStyledDocument(styles);
    contentAttributes = new HashMap<>();

    // no attributes defined
    Style s = styles.addStyle(null, null);
    contentAttributes.put("none", s);

    Style def = styles.getStyle(StyleContext.DEFAULT_STYLE);

    Style heading = styles.addStyle("heading", def);
    StyleConstants.setFontFamily(heading, "SansSerif");
    StyleConstants.setBold(heading, true);
    StyleConstants.setAlignment(heading, StyleConstants.ALIGN_CENTER);
    StyleConstants.setSpaceAbove(heading, 10);
    StyleConstants.setSpaceBelow(heading, 10);
    StyleConstants.setFontSize(heading, 18);

    // Title
    Style sty = styles.addStyle("title", heading);
    StyleConstants.setFontSize(sty, 32);

    // author
    sty = styles.addStyle("author", heading);
    StyleConstants.setItalic(sty, true);
    StyleConstants.setSpaceBelow(sty, 25);
}
 
Example 9
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 10
Source File: ToggleButtonGroupDemo.java    From radiance with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private static void setAlignment(JTextPane textPane, int alignment) {
    MutableAttributeSet attrSet = new SimpleAttributeSet();
    StyleConstants.setAlignment(attrSet, alignment);
    textPane.getStyledDocument().setParagraphAttributes(0,
            textPane.getStyledDocument().getLength(), attrSet, false);
}
 
Example 11
Source File: BasicCheckRibbon.java    From radiance with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private static void setAlignment(JTextPane textPane, int alignment) {
    MutableAttributeSet attrSet = new SimpleAttributeSet();
    StyleConstants.setAlignment(attrSet, alignment);
    textPane.getStyledDocument().setParagraphAttributes(0,
            textPane.getStyledDocument().getLength(), attrSet, false);
}
 
Example 12
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 13
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 14
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));
}
 
Example 15
Source File: StrategyCanvas.java    From iMetrica with GNU General Public License v3.0 4 votes vote down vote up
public void setIntroduction()
{  

   Style def = StyleContext.getDefaultStyleContext().
                      getStyle(StyleContext.DEFAULT_STYLE);  

   StyleConstants.setFontFamily(def, "SansSerif");

   StyleConstants.setAlignment(main_style, StyleConstants.ALIGN_LEFT);
   StyleConstants.setForeground(main_style, Color.green);
   try { doc.insertString(doc.getLength(), "Welcome to Evolution\n", main_style); }
   catch (BadLocationException e){}      
   
   
   
   
}
 
Example 16
Source File: MainPanel.java    From java-swing-tips with MIT License 4 votes vote down vote up
private static JEditorPane makeEditorPane(boolean editable) {
  JEditorPane editorPane = new JEditorPane();
  editorPane.setEditable(editable);
  editorPane.setContentType("text/html");
  editorPane.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE);
  editorPane.setText(HTML_TEXT);
  editorPane.addHyperlinkListener(e -> {
    if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
      JOptionPane.showMessageDialog(editorPane, "You click the link with the URL " + e.getURL());
    } else if (e.getEventType() == HyperlinkEvent.EventType.ENTERED) {
      tooltip = editorPane.getToolTipText();
      URL url = e.getURL();
      editorPane.setToolTipText(Objects.nonNull(url) ? url.toExternalForm() : null);
    } else if (e.getEventType() == HyperlinkEvent.EventType.EXITED) {
      editorPane.setToolTipText(tooltip);
    }
  });

  HTMLDocument doc = (HTMLDocument) editorPane.getDocument();
  Style s = doc.addStyle("button", null);
  StyleConstants.setAlignment(s, StyleConstants.ALIGN_CENTER);
  HyperlinkButton button = new HyperlinkButton(LINK);
  button.addActionListener(e -> {
    AbstractButton b = (AbstractButton) e.getSource();
    editorPane.setBackground(b.isSelected() ? Color.RED : Color.WHITE);
    JOptionPane.showMessageDialog(editorPane, "You click the link with the URL " + LINK);
  });
  button.setToolTipText("button: " + LINK);
  button.setOpaque(false);
  StyleConstants.setComponent(s, button);
  try {
    doc.insertString(doc.getLength(), "\n----\nJButton:\n", null);
    doc.insertString(doc.getLength(), LINK + "\n", doc.getStyle("button"));
    // doc.insertString(doc.getLength(), "\n", null);
  } catch (BadLocationException ex) {
    // should never happen
    RuntimeException wrap = new StringIndexOutOfBoundsException(ex.offsetRequested());
    wrap.initCause(ex);
    throw wrap;
  }
  return editorPane;
}