How to make only part of the text bold - java

Can you tell me how I can make only part of the text bold:
TextArea dataPane = new TextArea();
dataPane.appendText("Product Version: " + "1.0");
dataPane.appendText("\nJava: " + "7");
dataPane.appendText("\nRuntime: " + "7");
dataPane.appendText("\nSystem: " + "Linux");
dataPane.appendText("\nUser directory: " + "/home/dir");
I want to make only these strings bold: Product Version, Java, Runtime, System, User directory? What will be the easiest way to do this?
UPDATE
I also tested this code:
TextArea dataPane = new TextArea();
dataPane.setPrefRowCount(10);
Text wd = new Text("Version");
wd.setFont(Font.font ("Verdana", FontWeight.BOLD, 20));
dataPane.appendText(wd + "1.0");
dataPane.appendText("\nJava: " + "7");
dataPane.appendText("\nRuntime: " + "7");
dataPane.appendText("\nSystem: " + "Linux");
dataPane.appendText("\nUser directory: " + "/home/dir");
I get this: Text#149e84241.0
The text is not properly formatted.

You could use an editor pane with the content type set to html instead of a text area. I threw an editor pane into a scratch project really quick and just used the default variable names and object properties. Here's how I produced what I believe you're looking for:
jEditorPane1.setContentType("text/html"); //by default this is text/plain
//use margin-top and margin-bottom to prevent gaps between paragraphs
//or actually customize them to create space if you desire so
jEditorPane1.setText("<p style='margin-top:0pt;'><b>Product Version:</b> 1.0</p>" +
"<p style='margin-top:0pt; margin-bottom:0pt;'><b>Java:</b> 7</p>" +
"<p style='margin-top:0pt; margin-bottom:0pt;'><b>Runtime:</b> 7</p>" +
"<p style='margin-top:0pt; margin-bottom:0pt;'><b>System:</b> Linux</p>" +
"<p style='margin-top:0pt; margin-bottom:0pt;'><b>User directory:</b> /home/dir</p>");

Related

How to get input data in textArea

I am a new programmer in javafx.
Using java FX in IDE I want to make a student information application.
I want to print the input data into the text area but it prints out in the console now.
(After clicking display button I want to show all the input data in textarea)
please help me out to solve this problem.
I am attaching my code below for your suggestion..
Main.java
Just place
textArea.setText("Name: " + t1.getText() +"\nAddress: " + t2.getText()
+"\nProvince: " + t3.getText() +"\nCity: " + t4.getText()
+"\nPostal Code: " + t5.getText() +"\nPhone Number: " + t6.getText()
+"\nEmail: " + t7.getText() + "\nCourses: "+comboBox.getItems()
+ "\nActivities: "+c1.getText() + "\n"+c2.getText());
into the action handler.
And remove the imports
import javax.swing.JTextArea;
import java.awt.*;
They are not JavaFX.
You are printing your result into the standard output stream (System.out). I assume you want to display what is printed by:
System.out.println("Name: " + t1.getText() +"\nAddress: " + t2.getText()
+"\nProvince: " + t3.getText() +"\nCity: " + t4.getText()
+"\nPostal Code: " + t5.getText() +"\nPhone Number: " + t6.getText()
+"\nEmail: " + t7.getText() + "\nCourses: "+comboBox.getItems()
+ "\nActivities: "+c1.getText() + "\n"+c2.getText());
Please visit the documentation of the TextArea class. Printing your result into the standard output stream has nothing to do with printing your result in your TextArea. Simply use a normal String that contains your result and set the text of your TextArea accordingly.

JAVA GWT DialogBox how to add new line char

I am having an issue trying to add a new line to a DialogBox. I have tried the normal "\n" and also tried putting my lines inside of HTML style tags. Neither of these attempts worked. Help is appreciated.
DialogBox dialog = new DialogBox();
dialog.setText(" Meter ID:\t\t" + blink.getMeterId() + "\n X coordinate:\t\t" + blink.getXCoord() + "\n Y coordinate:\t\t" + blink
.getYCoord());
Issue resolved via using setHTML to set the contents of the DialogBox
dialog.setHTML("<p> Meter ID: " + blink.getMeterId() + "</p> <p> X coordinate:\t\t" + blink.getXCoord() + "</p> <p> Y coordinate:\t\t" + blink
.getYCoord() + "</p>");
note this is using <p></p> for new paragraph but <br> and other html will work also
just use another \ as an escape character in a string:
new line: \\n
DialogBox dialog = new DialogBox();
dialog.setText(" Meter ID:\\t\\t" + blink.getMeterId() + "\\n X coordinate:\\t\\t" + blink.getXCoord() + "\\n Y coordinate:\\t\\t" + blink
.getYCoord());

Swing Chat HTML formatted messages

I'm trying to make a chat client to Swing. I need that message in the history of correspondence were formatted in HTML.
The problem I tried to solve with JTextPane, as it supports HTML formatting. When I did, just text display, in principle everything was normal.
But when I added emoticons using the HTML tag <img>, each time a new message, all text in the window of correspondence started twitching.
How I did it:
jTextPane.setText ("message");
When it was, the new message, I did so
jTextPane.setText ("message" + "new message"); etc.
The result had been the principle of "snake" of Tetris. As a result, I did not like how it works.
So please tell me whether it is possible to deduce that new messages using JLabel adding them to JScrollpane? How to make every new post was a separate element?
String[] split = text.split("\t\t");
String time = split[0].split("\t")[2].split(", ")[1];
String sender = split[0].split("\t")[3];
String message = split[1];
if (!jTextPane.getText().equals("Please log in!")) {
oldMsg = jTextPane.getText().substring(jTextPane.getText().indexOf("<body>") + 6, jTextPane.getText().lastIndexOf("</body>"));
if(sender.equalsIgnoreCase(login.getText())) {
msg = "<div style=\"text-align:right\">" + checkMsgOnSmile(message) + " " + "<b>" + " :" + checkSenderOnColor(sender) + "</b>" + "<span style=\"font-size:10pt\">[" + time + "]</span></div>";
} else {
msg = "<div style=\"\"><span style=\"font-size:10pt\">[" + time + "]</span> " + "<b>" + checkSenderOnColor(sender) + ":" + "</b>" + " " + checkMsgOnSmile(message);
}
String[] check = (oldMsg + msg).split("<br>");
if (check.length > 99) {
ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList(check));
arrayList.remove(0);
String str = "";
for (int i = 0; i < arrayList.size(); i++) {
str = str + arrayList.get(i).toString() + "<br>";
}
jTextPane.setText(str);
} else {
oldMsg = oldMsg.replaceAll("<span><font size=\"10pt\">", "<span style=\"font-size:10pt\">");
oldMsg = oldMsg.replaceAll("</font></span>", "</span>");
jTextPane.setText(oldMsg + msg + "<br>");
}
}
Can it be replaced by a JLabel and JScrollPane?
If you just need to display text with different colors, fonts etc.., then I find working with text and attributes is easier than using HTML. A simple example would be code like:
JTextPane textPane = new JTextPane();
textPane.setText( "Hello:" );
textPane.setEditable(false);
StyledDocument doc = textPane.getStyledDocument();
// Define a keyword attribute
Simple AttributeSet keyWord = new SimpleAttributeSet();
StyleConstants.setForeground(keyWord, Color.RED);
StyleConstants.setBackground(keyWord, Color.YELLOW);
StyleConstants.setBold(keyWord, true);
// Add some text
try
{
doc.insertString(doc.getLength(), "\nAnother line of text", keyWord );
}
catch(Exception e) {}

Set jTextArea to visible false by default

I am making an application that show the information I entered in a textfield, combo box, etc.. into a text area.
I want to hide the text area when I started up the application and when I press on a button I want to show with the desired information.
I've tried to place <nameOfTextArea>.setVisible(false); in the frame constructor, but it is still visible.
How can I start up the frame without seeing this text area?
Constructor frame:
public StudentInfoFrame() {
initComponents();
textAreaVoorOpslaanInfo.setVisible(false);
}
My button in the frame:
private void uitvoerButtonActionPerformed(java.awt.event.ActionEvent evt) {
try{
..... Variables here .....
textAreaVoorOpslaanInfo.setVisible(true);
textAreaVoorOpslaanInfo.append("Voornaam: \t\t" + voornaam + "\n"
+ "Achternaam: \t\t" + achternaam + "\n"
+ "E-mail adres: \t\t" + email + "\n"
+ "Geboortedatum: \t" + geboortedatum + "\n"
+ "Lengte: \t\t" + lengte + "m\n"
+ "Gewicht: \t\t" + gewicht + "kg\n"
+ "Geslacht: \t\t" + geslacht + "\n"
+ "Vooropleiding(en): \t" + vooropleiding + "\n"
+ "Uitwonend: \t\t" + uitwonend);
} catch (Exception e){
System.out.println(e);
}
}
My question is solved. The jTextArea was invisible, but the jScrollPane was not.
Made the jScrollPane invisible in the constructor and it worked as intended.
Thanks for the help from the people above this post.
Call JComponent#revalidate() on parent component after changing element's visibility.
Call JFrame#setVisible(true) in the end after adding all the components.
Try this one:
textAreaVoorOpslaanInfo.hide();
or this:
textAreaVoorOpslaanInfo.show();

Tooltip using html javafx

I want to add a tooltip on a checkbox of javafx. I want to make it using java code. This tooltip text depends on some object properties. My problem is that i want to have some(not all) words bold inside the tooltip. Is there any way to do it? As far as i googled i didn't found a solution or a way to do it using for example html.
It would be a charm if i could do something like this:
PaggingTest paggingTest = new PaggingTest();
Tooltip tooltip = new Tooltip(
"<b>AlgorithmType:</b> " + paggingTest.getAlgorithmType()
+ "<br/><b>Memory Pages:</b> " + paggingTest.getMemoryPages()
+ "<br/><b>Program Pages:</b> " + paggingTest.getProgramPages()
+ "<br/><b>Sample Count:</b> " + paggingTest.getSampleCount()
+ "<br/><b>Distribution Type:</b> " + paggingTest.getDistributionType());
CheckBox checkBox = new CheckBox("Test");
checkBox.setTooltip(tooltip);
testFlowPane.getChildren().add(checkBox);
You could try this:
WebView web = new WebView();
WebEngine webEngine = web.getEngine();
webEngine.loadContent
(
"<b>AlgorithmType:</b> " + paggingTest.getAlgorithmType()
+ "<br/><b>Memory Pages:</b> " + paggingTest.getMemoryPages()
+ "<br/><b>Program Pages:</b> " + paggingTest.getProgramPages()
+ "<br/><b>Sample Count:</b> " + paggingTest.getSampleCount()
+ "<br/><b>Distribution Type:</b> " + paggingTest.getDistributionType()
);
Tooltip tip = new Tooltip();
tip.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
tip.setGraphic(web);
You may have to apply some css styling to the webview to get it to blend in with your tip.

Categories

Resources