I searched for this issue and I found the below codes, but these codes are for Java-Android. What are the similar codes that I can use with Java
The codes are:
ArrayList<Integer> positions = new ArrayList();
Pattern p = Pattern.compile("mp"); // insert your pattern here
Matcher m = p.matcher("Simple Text, bumping , jumping");
while (m.find()) {
positions.add(m.start());
}
Spannable spanning = new SpannableString("Simple Text");
spanning.setSpan(new ForegroundColorSpan(Color.BLUE),positions.get(i), positions.get(i)+1,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textview.setText(spanning);
Are you using Java Swing?
If yes, you can put "<html>YOUR TEXT</html>" with needly customizations for you design.
Put it inside an new JLabel("<html>YOUR..."); It is the principle of Java graphics design, and today we have the JavaFX that will replace these method of interface programming
Your solution.
You can change the colour of the label.
Add this to your jframe or jlabel as you have used in your project.
Thank you
Label label = new Label("Your text");
//sets the colour of text
label.setForeground(Color.BLACK);
//sets the backround of text
label.setBackground(Color.BLUE);
Related
As title says, I have a VerticalGroup that I store bunch of Labels in. Now, it does align ALL of them to the left, kind of. The problem is that labels that are shorter than the largest label will be centered around the middle of the largest label, like so:
Here is the relevant code:
private ScrollPane chatScrollPane;
private VerticalGroup chatGroup;
...
chatGroup = new VerticalGroup();
chatScrollPane = new ScrollPane(chatGroup, game.getSkin());
stage.addActor(chatScrollPane);
... in another method that adds messages ...
String message = "a message";
Label messageLabel = new Label(message, game.getSkin());
messageLabel.setAlignment(Align.left);
chatGroup.left();
chatGroup.addActor(messageLabel);
Now, what I'm asking is how can I have EVERY message, regardless of length, be on the left (like the Welcome message in screenshot)?
Thanks in advance.
I actually found the solution after quite a bit of fiddling. Turns out I need to do chatGroup.columnAlign(Align.left); to fix the problem. Thanks anyway!
I searched through many posts and figured out that JLabel supports HTML.
So I can do
JLabel search = new JLabel("<html>Search<br/> By:</html>");
to get multiple lines. Above code will result in
Search
By:
However, What I want is something like
Search
By:
Adding spaces before "By:" will work only when the window is not resizable(And very silly lol).
Can anyone tell me how to modify this code to make it work as I wanted?
Slightly simpler HTML than seen in #MadProgrammer's answer:
new JLabel("<html><body style='text-align: right'>Search<br>By:");
Non-breaking spaces ( ) are supported:
new JLabel("<html>Search<br/> By:</html>");
If you want to have real right-alignment, use individual right-aligned labels and combine them:
JLabel search = new JLabel("Search", SwingConstants.RIGHT);
JLabel by = new JLabel("By:", SwingConstants.RIGHT);
JPanel combined = new JPanel();
combined.setOpaque(false);
combined.setLayout(new GridLayout(2, 1));
combined.add(search);
combined.add(by);
or use a read-only JTextPane instead (with \n for line breaks):
JTextPane text = new JTextPane();
SimpleAttributeSet attributes = new SimpleAttributeSet();
StyleConstants.setAlignment(attributes, StyleConstants.ALIGN_RIGHT);
StyleConstants.setFontFamily(attributes, "Default");
text.setParagraphAttributes(attributes, true);
text.setEditable(false);
text.setOpaque(false);
text.setText("Search\nBy:");
There are a number of ways you might achieve this, one of the safer ways might be to use a <table> and aligning both cells to the right...
JLabel label = new JLabel(
"<html><table border='0' cellpadding='0' cellspacing='0'>" +
"<tr><td align='right'>Search</td></tr>" +
"<tr><td align='right'>By:</td></tr></table>"
);
This overcomes issues with differences between fonts and font rendering on different platforms
I want to color specific words in a string.
At the moment I am using this:
SpannableString txt = new SpannableString(txt.toString());
Pattern color = Pattern.compile("My Text");
Matcher mat = color.matcher(txt);
while(mat.find()) {
txtt.setSpan(new ForegroundColorSpan(Color.BLUE), mat.start(), mat.end(), 0);
}
This is working perfect for one text.. but the problem is that I want color more than one piece of text in differed colors.
I already tried it with:
line = line.replace("My Text",
"<font color='#0000ff'>" + "$0" + "</font>");
But StringBuilder seems not to work with this
textViewTs.setText(Html.fromHtml(line));
UPDATE:
if i try to continue the while loop like this
SpannableString txtt = new SpannableString(txt.toString());
Pattern color = Pattern.compile("first text");
Matcher mat = color.matcher(txt);
Pattern color2 = Pattern.compile("second text");
Matcher mat2 = color2.matcher(txt);
while(mat.find()) {
txtt.setSpan(new ForegroundColorSpan(Color.BLUE), mat.start(), mat.end(), 0);
} while(mat2.find()) {
txtt.setSpan(new ForegroundColorSpan(Color.YELLOW), mat.start(), mat.end(), 0);
}
textViewTs.setText(txtt);
I get a error that reads java.lang.IllegalStateException: No successful match so far
Any good way to solve this?
try with the replace() method from the SpannableStringBuilder (http://developer.android.com/reference/android/text/SpannableStringBuilder.html) instead of using the setSpan().
I have something perfect for you! I wrote some code and uploaded it to my website. You can find the page with intrsuctions on how to use it here and the code to add to your src folder here.
I made this for LibGDX but it should work the same. All you need to do is change the code to change the color of the text and the code to draw the text! For example, if your using SurfaceView you could replace this:
if(character == 'r'){
font.setColor(Color.RED);
}
with this:
if(character == 'r'){
paint.setColor(Color.RED);
}
Just an example, I don't thinks that's what your using, right? Just change it however you need it.
How it Works:
To change the color just add codes to the string you want to draw. For example, just say you want to draw "Hello World!" with "Hello" in red and "World" in blue at coordinates 50, 40 just run this:
ColorText.draw("&rHello &bWorld!", 1, batch, 50, 40);
Note! You will have to do a lot of finetuning for this to work for you but the basic code is there for handling the string.
I'm having trouble trying to get a linebreak included in a Stringbuilder to appear in a JLabel.
I've found a couple of similar problems solved here, e.g. [here] Problems to linebreak with an int in JLabel and [here] How do I append a newline character for all lines except the last one? , along with a few others but none of them seem to work for me.
When printing to System.out, it works fine and I get a new line each time but when passed to a JLabel, everything appears as one line.
Here's the current version of the code, which has attempted to append a newline character, as well as including one in the main declaration. Neither has any effect when passed to the JLabel:
public void layoutCenter() {
JPanel central = new JPanel();
add(central, BorderLayout.CENTER);
JTabbedPane tabs = new JTabbedPane();
this.add(tabs);
// memory tab
StringBuilder mList = new StringBuilder();
memLocList = new Memory[MEM_LOCATIONS]; //Memory is a separate class
for (int i = 0; i < memLocList.length; i++) {
mList.append("\n");
memLocList[i] = null;
mList.append("Memory location: " + i + " " + memLocList[i] + "\n");
}
System.out.println(mList.toString());
JComponent memTab = makeTextPanel(mList.toString());
tabs.addTab("Memory", memTab);
}
protected JComponent makeTextPanel(String text) {
JPanel panel = new JPanel(false);
JLabel filler = new JLabel(text);
panel.add(filler);
return panel;
}
I've also tried using System.getProperty(line.separator) with similar results and can't think of anything else to try so thought I'd appeal for help here.
Thanks,
Robert.
-EDIT-
Thanks to mKorbel, changing the JLabel to a JTextPane solved it.
The two lines in question are:
JTextPane filler = new JTextPane();
filler.setText(text);
Thanks again to everyone.
JLabel isn't designated to held multilines document, there are two choices (by accepting newline or tab by default)
if document could not be decorated or styled somehow then to use JTextArea
in the case document could be decorated or styled somehow then to use JEditorPane or JTextPane
You're going to have to use <html> and <br> to get line breaks in a JLabel Swing component.
If you absolutely must use JLabel, then I suggest using one for each line.
You can make a JLabel have mulitple lines by wrapping the text in HTML tags and using br tags to add a new line.
If you news auto wrapping I suggest using a JTexrArea. You can make it uneditable and style it so it looks like a label.
You can look at this tutorial:
http://docs.oracle.com/javase/tutorial/uiswing/components/html.html
One of the example is using html to make it two lines for a JButton text. It should be very similar.
What would be an easy way to set the background color of all characters from some start to an end position in Java.
Eg:
red background from position D(4) to J(10)
String alpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
Many thanks in advance.
Edit:
Oh no, 2 '-'? Ok here is what I'm doing.
JTextArea textArea = new JTextArea("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
textArea.setFont(new Font("Courier", Font.PLAIN, 16));
//then I want red background from position D(4) to J(10)
please read Using Text Components and tons examples for that
look here for excelent workaround
Split the String into 3 separate Strings.
String a = alpha.substring(0,4);
String b = alpha.substring(4,11);
String c = alpha.substring(11);
Then display a,b, and c next to each other while only formatting b as desired.