How to append Text in JTextPane like JTextArea?
like
JTextPane pText1 = new JTextPane();
pText1.append( txt1.getText + "\n" );
pText1.append( txt2.getText + "\n" );
pText1.append( txt3.getText + "\n" );
Well, JTextPane works with Document model such asStyledDocument to manage text data. Because JTextPane differs from JTextArea in the sense that, we use JTextPane for styling text. However, if you need to append-string feature for your own requirement, you can easily build your won appendString function by extending JTextPane to work with:
public void appendString(String str) throws BadLocationException
{
StyledDocument document = (StyledDocument) jTextPane.getDocument();
document.insertString(document.getLength(), str, null);
// ^ or your style attribute
}
The above function first ask the text pane to for the StyledDocument associating with it. Then it make use of insertString(int offset,
String str,
AttributeSet a) function.
There is no append method in JTextPane objects. Most used, similar to "append":
JTextPane pText1 = new JTextPane();
pText1.setText(txt1.getText + "\n");
pText1.setText(pText1.getText() + txt2.getText + "\n");
pText1.setText(pText1.getText() + txt3.getText + "\n");
Related
I have a section of text where I am using .replacingOccurrences to display the users' answer within the question sentence:
Text((question.text)
.replacingOccurrences(of: "_____", with:
question.answers[question.userAnswer]
))
.font(Font.custom("ClearSans-Bold", size: 18))
.foregroundColor(.black )
.padding(.bottom, 20)
.multilineTextAlignment(.center)
I want the users' answer question.answers[question.userAnswer] to be a different colour (red/green) to the main body of the text (similar to attached image) however I'm new to SwiftUI so not sure how to add this in.
Image 1
Here's an extension for String that does pretty much what you want:
extension String {
func replacingOccurrences(of: String, with: [Text]) -> Text {
return self.components(separatedBy: of).enumerated().map({(i, s) in
return i < with.count ? Text(s) + with[i] : Text(s)
}).reduce(Text(""), { (r, t) in
return r + t
})
}
}
It uses concatenation of Text elements as George_E suggested. You can use it like this:
struct ContentView: View {
let question: String = "The witch found the concoction extremely _____. _____ makes better ones."
let answers: [String] = ["nauseate", "A friend of her's"]
var body: some View {
question.replacingOccurrences(of: "_____", with: self.answers.map({s in Text(s).foregroundColor(.red)})).foregroundColor(.secondary)
}
}
Result:
You may want to add some extra code for handling cases where the number of answers does not match the occurrences of _____.
I wanted to know how can I print those 5 objects in 1 sentence.
This is my main:
GAMES game1 = new GAMES("fifa", 300, 8.5);
GAMES game2 = new GAMES("wwe", 250, 9);
GAMES game3 = new GAMES("cod", 230, 8);
GAMES game4 = new GAMES("nba", 360, 9.5);
GAMES game5 = new GAMES("gta", 700, 6);
Console.WriteLine(game2);
This is the Override code I wrote:
public override string ToString()
{
return "Game Name:" + this.GameName + " " + "Price:" + this.Price + " " + "raiting:" + this.raiting;
}
Now what I want to know is how can I print the whole 5 objects in one go please.
You can add all games to an array and use foreach invoke method toString.
Array.foreach(elem => elem.toString)
Instead of Array, you type a name of your array.
I am trying to send and update strings inside a UITextView using and IBAction button.
the code below works fine however, every time i push the button the old text is replaced with the new one. What I am trying to do is to always append the text to the exiting one.
Any Idea?
#IBAction func equalPressed(sender: AnyObject) {
var resultString:String = "new string"
textView.text = resultString.stringByAppendingString("= " + resultLabel.text! + ";")
}
You already know how to append strings, but you're doing it two different ways. The stringByAppendingString(_:) method is fine, but Swift's + operator is clearer. I'd rewrite your existing method as follows:
#IBAction func equalPressed(sender: AnyObject) {
let resultString = "new string"
textView.text = resultString + "= " + resultLabel.text! + ";"
}
Then, to append the text rather than replace it, it's a simple change of including the old value in the new one:
textView.text = textView.text + resultString + "= " + resultLabel.text! + ";"
Or, using the += operator (x += y is short for x = x + y):
textView.text += resultString + "= " + resultLabel.text! + ";"
Redirect me if this is already a previously solved issue!
In my program, I have a Stage in which the user can view a list of content, stored in a list consisting of Strings.
goToView.setOnAction(event ->{
menuStage.close();
viewStage.show();
String horseNameList = "";
for(int i = 0; i < accountList.size(); i++){
if(accountList.get(i).userName.equals(uName)){
for(int j = 0; j < accountList.get(i).createdHorses.size(); j++){
horseNameList += accountList.get(i).createdHorses.get(j);
horseNameList += "\n" + "\n";
}
}
Text hNameListTXT = new Text(horseNameList);
hNameListTXT.setFont(Font.font("Tahoma", FontWeight.NORMAL, 12));
listVbox.getChildren().add(hNameListTXT);
}
createdHorses is a List of Strings, listVbox is as you may think a VBox where the String (which converts to a Text) is printed. Now, when I close the Stage with the following EventHandler, nothing in particular happens:
backView.setOnAction(event -> {
viewStage.close();
menuStage.show();
});
But as I then open the Stage once again (by using another EventHandler similar to the one I use to close the first Stage with), my List (or String) har been doubled. What should I do to clear the String (or possibly the Text) so that it doesn't display it twice?
What should I do to clear the String (or possibly the Text) so that it
doesn't display it twice?
The VBox has a method getChildren() which returns an ObservableList. So you could use clear() on it to remove all the children.
The program in it's entirety sorts an ArrayList of Student objects by integers highest average, last name, and also has the option to perform a search. My program works flawlessly except for my binary search, for which I can absolutely not determine the cause of failure. I have printed all the information as it comes up.
Here is the student class with the method that references the Student's first and last name (String).
public String getFirstName (){
return firstname;
}
public String getLastName(){
return lastname;
}
In addition, here is the code for the binary search. Yes, I know Collections has a method for this exact purpose, but for my class I need to write up the search myself.
private static void searchStudent(ArrayList<Student> a){
Scanner reader = new Scanner(System.in);
System.out.print("Please enter search term: ");
String term = reader.next();
//System.out.println(term + " " + term.length());
System.out.println("---SEARCH RESULTS:---");
for (int i = 0; i < a.size(); i++){
String fName = (a.get(i).getFirstName());
String lName = (a.get(i).getLastName());
//System.out.println(fName + " " + fName.length());
//System.out.println(lName + " " + lName.length());
if (term == fName){
System.out.println(a.get(i));
} else if (term == lName){
System.out.println(a.get(i));
}
}
}
In Java, you need to use .equals() to compare strings. E.g. instead of this:
if (term == fName){
you need to do this:
if (term.equals(fName)){
Otherwise, you are comparing references only.
Btw, this is not a binary search, it's a linear search. You can see one implementation of binary search e.g. here:
http://leepoint.net/notes-java/algorithms/searching/binarysearch.html
though to compare strings you would use .compareTo / .compareToIgnoreCase methods on the String class instead of < / > operators.