when i use this code it does not work; the text does not become bold. Why?
label1 = new JLabel();
label1.setText("Welcome, <html><strong>Hussein</strong></html>.");
Your HTML syntax is bad, since your String does not start with the html DOM root.
Try something in the lines of:
label1.setText("<html>Welcome <strong>Hussein</strong>.</html>");
Find a tutorial here.
try this:
JLabel label = new JLabel("<html><yourTagHere><yourOtherTagHere>this is your text</yourOtherTagHere></yourTagHere></html>");
You forgot to add body tag ,Try This :
label1 = new JLabel();
label1.setText("<html><body>Welcome ,<strong> Hussein</strong></body></html>");
Why are you mixing html and java. tag is in html. This is how you do in java
label = new JLabel("A label");
label.setFont(new Font("Serif", Font.BOLD, 14));
Related
I know that if I want underline a text in a JButton I have to do something like this:
JButton myButton=new JButton("Test");
Font font = myButton.getFont();
Map attributes = font.getAttributes();
attributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
myButton.setFont(font.deriveFont(attributes));
And, If I would overline a text? What should I do? Could anyone help me?
No need for the Font class here. You can use HTML directly and add the <u> and <strike> tags:
myButton = new JButton("<html><strike><u>Test</u></strike></html>");
Just adding to #maroun's answer(I'm not allowed to comment). try using HTML directly.
myButton = new JButton ("<html><font style=\"text-decoration: overline;\"> Overline text</font></html>");
You can create a CompoundBorder:
JButton button = new JButton("Using Compound Border");
Border lineBorder = BorderFactory.createMatteBorder(2, 0, 0, 0, Color.BLACK);
CompoundBorder border = new CompoundBorder(button.getBorder(), lineBorder);
button.setBorder( border );
I am using an attributed string to bold the heading names, and I would like to print out in a Text object in javafx, but I cannot figure out how. I've looked a fair amount of places online, including the java docs but nothing seems to go over this... Here is what I am trying to do:
AttributedString boldName = new AttributedString("Name: ");
boldName.addAttribute(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);
String name = innerNode.name;
Text info = new Text(**boldName.something()?** + name);
The result should be this: Name: name, something quite simple I feel but how??
You cannot use the AttributedString in JavaFX. Instead use,
On JavaFX 2.2 and earlier:
Text name = new Text("Name: ");
name.setFill(Color.BLUE);
name.setFont(Font.font("Helvetica", FontWeight.BOLD, 12));
Text info = new Text(innerNode.name);
HBox hbox = new HBox(1);
hbox.getChildren().addAll(name, info);
On JavaFX 8:
Text name = new Text("Name: ");
name.setFill(Color.BLUE);
name.setFont(Font.font("Helvetica", FontWeight.BOLD, 12));
Text info = new Text(innerNode.name);
TextFlow textFlow = new TextFlow(name, info);
Reference:
Using Text and Text Effects in JavaFX
javafx.scene.text.TextFlow
I want to personalize the JXDatePicker. More specifically I want to create an own own action button on a an own position. I tried to extract the included JButton but when I try to add it to any JPanel it only adds empty space.
JXDatePicker fDate = new JXDatePicker(new Date());
fDate.getEditor().setBorder(null);
fDate.getEditor().setEditable(false);
JButton eDate = (JButton) fDate.getComponent(1);
fDate.remove(eDate);
I don't think this approach would work anyway, but I don't know any better at the moment.
Any suggestions?
found a working solution, migt be not the cleanest way:
JXDatePicker fDate = new JXDatePicker(new Date());
//edit the Textfield
fDate.getEditor().setBorder(null);
fDate.getEditor().setEditable(false);
//edit the Button
JButton dateBtn= (JButton) fDate.getComponent(1);
Image editImage =Toolkit.getDefaultToolkit().getImage(getClass().getResource("/toolbarButtonGraphics/general/Edit16.gif"));
dateBtn.remove(eDate);
dateBtn.setIcon(new ImageIcon(editImage));
dateBtn.setFocusPainted(false);
dateBtn.setMargin(new Insets(0, 0, 0, 0));
dateBtn.setContentAreaFilled(false);
dateBtn.setBorderPainted(false);
dateBtn.setOpaque(false);
I added the dateBtn somewhere else in my view.
((JButton) startDatePicker.getComponent(1)).setIcon(new ImageIcon(((new ImageIcon(getClass().getResource("/images/calendar.png"))).getImage()).getScaledInstance(20, 20, java.awt.Image.SCALE_SMOOTH)));
Goodevening
how can have a JTextArea like in netbeans (see the pic)
(source: hostingpics.net)
my code of the JTextArea:
JTextArea infoArea = new JTextArea(10,10);
infoArea.setLineWrap(true);
infoArea.setFont(police);
infoArea.setForeground(Color.YELLOW);
infoArea.setBackground(Color.BLACK);
infoArea.setEditable(false);
JScrollPane scroll = new JScrollPane(infoArea);
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scroll.setBorder(BorderFactory.createEmptyBorder(5, 0, 10, 0));
scroll.setPreferredSize(new Dimension(sousFrame.getWidth(),90));
scroll.setFont( new Font("Arial", Font.BOLD, 13));
scroll.setBorder(BorderFactory.createTitledBorder("Output"));
thank you
I'm not sure what NetBeans uses, but we used flexdock at my last company to create dockable windows in a Java Swing application (assuming this is what you meant by "retractable").
How do I easily edit the style of the selected text in a JTextPane? There doesn't seem to be many resources on this. Even if you can direct me to a good resource on this, I'd greatly appreciate it.
Also, how do I get the current style of the selected text? I tried styledDoc.getLogicalStyle(textPane.getSelectionStart()); but it doesn't seem to be working.
Here's a code snippet to insert a formatted "Hello World!" string in a JEditorPane:
Document doc = yourEditorPane.getDocument();
StyleContext sc = new StyleContext();
Style style = sc.addStyle("yourStyle", null);
Font font = new Font("Arial", Font.BOLD, 18);
StyleConstants.setForeground(style, Color.RED);
StyleConstants.setFontFamily(style, font.getFamily());
StyleConstants.setBold(style, true);
doc.insertString(doc.getLength(), "Hello World!", style);
Take a look at the following code in this pastebin:
http://pbin.oogly.co.uk/listings/viewlistingdetail/d6fe483a52c52aa951ca15762ed3d3
The example is from here:
http://www.java2s.com/Code/Java/Swing-JFC/JTextPaneStylesExample3.htm
It looks like you can change the style using the following in an action listener:
final Style boldStyle = sc.addStyle("MainStyle", defaultStyle);
StyleConstants.setBold(boldStyle, true);
doc.setCharacterAttributes(0, 10, boldStyle, true);
It sets the style of the text between the given offset and length to a specific style.
See the full pastebin for more details. That should fix your problem though.
The easiest way to manipulate text panels is using editor kits and their associated actions. You can find a demo of this in the JDK samples (under jdk\demo\jfc\Stylepad).
Sample code that installs a StyledEditorKit and uses a FontSizeAction to manipulate the text:
public static void main(String[] args) {
// create a rich text pane
JTextPane textPane = new JTextPane();
JScrollPane scrollPane = new JScrollPane(textPane,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
// install the editor kit
StyledEditorKit editorKit = new StyledEditorKit();
textPane.setEditorKit(editorKit);
// build the menu
JMenu fontMenu = new JMenu("Font Size");
for (int i = 48; i >= 8; i -= 10) {
JMenuItem menuItem = new JMenuItem("" + i);
// add an action
menuItem
.addActionListener(new StyledEditorKit.FontSizeAction(
"myaction-" + i, i));
fontMenu.add(menuItem);
}
JMenuBar menuBar = new JMenuBar();
menuBar.add(fontMenu);
// show in a frame
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 400);
frame.setJMenuBar(menuBar);
frame.setContentPane(scrollPane);
frame.setVisible(true);
}
(Tip: if you want to use a FontFamilyAction, have a look at GraphicsEnvironment.getAvailableFontFamilyNames() and logical font family names.)
I'd recommend taking a look at Sun's Java Tutorial about editor panes.
Ok, wow. Hard question. So I have not found a way to get the style of a given character. You can, however, get the MutableAttributeSet for a given character and then test to see if the style is in that attribute set.
Style s; //your style
Element run = styledDocument.getCharacterElement(
textPane.getSelectionStart() );
MutableAttributeSet curAttr =
( MutableAttributeSet )run.getAttributes();
boolean containsIt = curAttr.containsAttributes( s );
One problem with getting the Style for a range of characters is that there may be more than one style applied to that range (example: you may select text where some is bold and some is not).
To update the selected text you can:
Style s; //your style
JTextPane textPane; //your textpane
textPane.setCharacterAttributes( s, false );
Oh, and it appears that the function getLogicalStyle doesn't work because it's returning the default style (or maybe just the style) for the paragraph that contains p, rather than the the style of the character at p.