Can we change the String Style in java from normal to bold...
Example: I wish to change the String "Name" from normal Style to bold Style. Any default
method included in java?
The question is a bit vague, but in Swing you can use HTML in order to do format the way your strings appear in components:
jLabel1.setText("<html><b>Bold text</b></html>");
jButton1.setText("<html><b><i>Bold and Italic text</i></b></html>");
A string is just the representation of some text. Applying some style information depends on the GUI framework you use: Swing, AWT, JSP, etc...
It depends where do you want to change its style ?
If you are using Swings' components or AWT or something like that then surely you can do it.
A String object in Java just holds the data. It does not have any styling associated with it. Display styles are associated with the presentation components. You sure are missing something.
If you are using a GUI System like a JLabel, JTextField etc, then you can go two ways:
eg:
JLabel myLabel = new JLabel("<html><b>My Text");
or
Font F = new Font("Calibre",Font.BOLD,12);
myLabel.setFont(f);
Hope that answers you Question.
There are two ways,
Either create a font by yourself ,
something like, Font abc = new Font("Arial",font.BOLD,15)
and for any of the components you can use the setFont() method to set the font for the component.
See the javadoc for more
Use html,
Java supports html formating on all of its swing components.
Here is How to Use HTML in Swing Components.
Any default method included in java?
So I suppose for you its better to use a font, while setting the style via html means, if only for that text ,its not default for the component.
This is a generalized answer, for more specific answer,ask more specific question :)
Related
Swing has a nice feature where some components allow text to be specified using html. For example, the JLabel component can use html formatting simply using:
JLabel jl = new JLabel("<html><body><h1>Hello World</h1></body></html>");
I find this feature to be really convenient and I am using it in several places in my app.
I would like to know what the default styling is when I use this feature. I know that I can easily override these by adding my own style attributes, but I would like to know what the starting set is, because it is different than most browser defaults.
Where can I find out the default styling (font, line spacing, etc)? I have not seen anything in the documentation. I tried to browse through the source code, but I was unable to see where the tag gets parsed and the styling gets applied.
Can someone give me a pointer as to where to look for this information, possibly in the OpenJDK source?
The default style sheet is default.css, which can be found in resources.jar under the directory javax/swing/text/html/default.css.
You can also obtain the styles programmatically:
StyleSheet sheet = new HTMLEditorKit().getStyleSheet();
I believe the first answer is correct but incomplete.
Check out SwingUtilities2.displayPropertiesToCSS().
It adds a rule to the document stylesheet based on the component's font and foreground color. It is used by BasicHTML.BasicDocument.
I've been creating something like text editor for a while and noticed one interesting feature in NetBeans: when creating Java Application from a template (for example, "Desktop Application"), it creates immutable blocks of code (they are present for viewing but they can not be modified directly).
This shot should make all that text above clear:
So, the question is: how to implement such a feature using JTextPane?
You must create a class implementing the javax.swing.text.DocumentFilter used by your editor pane for the unchanged text. For the highlighting I suppose you will have to use a javax.swing.text.Highlighter.
im trying to implement a Chat feature in my application. i have used 2 JEditorPane. one for holding chat history and the other for sending chat to the previous JEditorPane.
the JEditorPane is text/html type.
the problem i am having is when i put more than one space between characters it is automatically removed by the parser because it is HTML!
how can i make it so, that the spaces are not stripped?
example: hello world
becomes: hello world
also i am having to parse the html tags so the new messages can be added to the history window.
is there a better option than using JEditorPane? if i used JTextPane would it be easier to implement?
i would like the chat boxes/panes to be able to handle bold, URL embedding for now.
thank you and look forward to your guidance.
EDIT: im trying to replace " " with a relavent char.
newHome[1] = newHome[1].replace(" ", newChar)
what should be the newChar value?
EDIT: im trying:
newHome[1] = newHome[1].replaceAll(" ", " ");
but it is not producing the results. any ideas?
EDIT: #Thomas - thanks! for some reason i can post a note to your answer.
Using HTML markup is a quick way to get simple text formatting done in a Swing text component. However, it's not the only way.
A more sophisticated method is to use a javax.swing.text.StyledDocument to which you can attach different "styles" (hence the name). A style is basically a set of attributes, for instance, whether the text should be in bold or italics or what Color it should have.
JTextPane provides a number of convenience methods to deal with styles, and it is a subclass of JEditorPane which means it should integrate rather seamlessly into your existing code. As an example, to mark a portion of the text within a JTextPane as bold, you could use something like this:
JTextPane textPane = new JTextPane();
Style bold = textPane.addStyle("bold", null);
StyleConstants.setBold(bold, true);
textPane.setText("I'll be bold.");
textPane.getStyledDocument().setCharacterAttributes(8, 4, bold, true);
Similarly, you could define a second style that e.g. uses a blue, underlined font and which you could use to display hyperlinks.
Unfortunately, the downside is that you will have to take care of the mechanics of the links yourself. Although you can use the existing infrastructure of javax.swing.event.HyperlinkListener et al., you will be responsible for detecting mouse clicks. The same goes for hovering and changing the Cursor into a hand-symbol etc.
I want to display HTML in a JEditorPane or JTextPane, but I want the style (font size) to come from the Look and Feel. Is there a way to do this, or do you have to use embedded HTML styling?
Here is an example:
epText = new JEditorPane("text/html", content);
StyleSheet ss = ((HTMLEditorKit)epText.getEditorKit()).getStyleSheet();
ss.addRule("p {font-size:" + FontManager.getManager().getFontSize() + "}");
HTMLEditorKit kit = (HTMLEditorKit) epText.getEditorKit();
kit.setStyleSheet(ss);
epText.setEditorKit(kit);
Whenever I set the editor kit, all text disappears. Do I need to set the text everytime?
Yes.
Dig into this snippet, from the Java API for HTMLEditorKit:
Customization from current LAF
HTML provides a well known set of features without exactly specifying
the display characteristics. Swing has
a theme mechanism for its
look-and-feel implementations. It is
desirable for the look-and-feel to
feed display characteristics into the
HTML views. An user with poor vision
for example would want high contrast
and larger than typical fonts.
The support for this is provided by the StyleSheet class. The
presentation of the HTML can be
heavily influenced by the setting of
the StyleSheet property on the EditorKit.
Edit: when you set the Editor Kit, it de-installs the old kit and installs the new one. This changes the underlying model, which is why the text 'disappears'.
Read the API for more info.
But you may not need to re-create the entire kit... just add a new sheet to your style.
Call this from the main method after you set L&F.
public static void setDefaultStyle()
{
// looks like WTF but that is the way to set default CSS
StyleSheet sheet=new HTMLEditorKit().getStyleSheet();
// add your rules
sheet.addRule("...");
sheet.addRule("...");
}
EDIT: At least this should work for JTextPane.
Is it possible to have something like a JTextArea which will color some keywords based on some mappings I have ?
Here are a few other choices that you could try
http://ostermiller.org/syntax/editor.html
http://java.sun.com/products/jfc/tsc/articles/text/editor_kit/index.html
http://fifesoft.com/rsyntaxtextarea/
http://code.google.com/p/jsyntaxpane/
I am not sure, because I believe JTextArea only directly supports a single font in a single color.
According to SyntaxHighlighter class article, JTextPane is the way to go.