/** * A convience method to read a font from a style. * * @param style Style being modified * @return The font inside the passed style */ public static String getFontString(Style style) { String font = StyleConstants.getFontFamily(style) + "-"; if (StyleConstants.isBold(style)) font += "BOLD"; if (StyleConstants.isItalic(style)) font += "ITALIC"; if (!StyleConstants.isBold(style) && !StyleConstants.isItalic(style)) font += "PLAIN"; font += "-" + StyleConstants.getFontSize(style); return font; }
// ---------------------------------------------------------------------------- public void setAttributes(AttributeSet a) { attributes_ = new SimpleAttributeSet(a); String name = StyleConstants.getFontFamily(a); font_names_.setSelected(name); int size = StyleConstants.getFontSize(a); font_sizes_.setSelectedInt(size); updatePreview(); }
public void actionPerformed(ActionEvent e) { JTextPane editor = (JTextPane) getEditor(e); int p0 = editor.getSelectionStart(); StyledDocument doc = getStyledDocument(editor); Element paragraph = doc.getCharacterElement(p0); AttributeSet as = paragraph.getAttributes(); family = StyleConstants.getFontFamily(as); fontSize = StyleConstants.getFontSize(as); formatText = new JDialog(new JFrame(), "Font and Size", true); formatText.getContentPane().setLayout(new BorderLayout()); JPanel choosers = new JPanel(); choosers.setLayout(new GridLayout(2, 1)); JPanel fontFamilyPanel = new JPanel(); fontFamilyPanel.add(new JLabel("Font")); GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); String[] fontNames = ge.getAvailableFontFamilyNames(); fontFamilyChooser = new JComboBox<String>(); for (int i = 0; i < fontNames.length; i++) { fontFamilyChooser.addItem(fontNames[i]); } fontFamilyChooser.setSelectedItem(family); fontFamilyPanel.add(fontFamilyChooser); choosers.add(fontFamilyPanel); JPanel fontSizePanel = new JPanel(); fontSizePanel.add(new JLabel("Size")); fontSizeChooser = new JComboBox<Float>(); fontSizeChooser.setEditable(true); for (int size = 2; size <= 60; size += 2) { fontSizeChooser.addItem(new Float(size)); } fontSizeChooser.setSelectedItem(new Float(fontSize)); fontSizePanel.add(fontSizeChooser); choosers.add(fontSizePanel); JButton ok = new JButton("OK"); ok.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent ae) { accept = true; formatText.dispose(); family = (String) fontFamilyChooser.getSelectedItem(); fontSize = Float.parseFloat(fontSizeChooser.getSelectedItem().toString()); } }); JButton cancel = new JButton("Cancel"); cancel.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent ae) { formatText.dispose(); } }); JPanel buttons = new JPanel(); buttons.add(ok); buttons.add(cancel); formatText.getContentPane().add(choosers, BorderLayout.CENTER); formatText.getContentPane().add(buttons, BorderLayout.SOUTH); formatText.pack(); formatText.setVisible(true); MutableAttributeSet attr = null; if (editor != null && accept) { attr = new SimpleAttributeSet(); StyleConstants.setFontFamily(attr, family); StyleConstants.setFontSize(attr, (int) fontSize); setCharacterAttributes(editor, attr, false); } }