I have a string that I want to display in a JOptionPane.
String info = "Name:" + _name + "\n" +
"Phone:" + _phone;
I tried to add \t but it didn't work.
I tried also to
int choose = JOptionPane.showConfirmDialog(this,new JTextArea(info),"XXX",0);
But it doesn't look good.
Are there another ways to do that? (If you know about a solution where I can use something like \t it will be very usefull for me)
* In this specific example I can manually align it, but I'm looking for a general solution.
HTML formatting could help:
String info = "Name:" + _name + "<br>" +
"Phone:" + _phone + "<br>";
int choose =
JOptionPane.showConfirmDialog(this, "<html>" + info + "</html>", "XXX", 0);
Another option is to use a JTable in the JOptionPane dialog as shown in this solution.
I had something like that and it worked:
String st = "<table border = \"0\">" +
"<tr><td>VALUE1: </td><td>" + _value2 + "</td></tr>"+
"<tr><td>VALUE2: </td><td>" + _value4 + "</td></tr>" +
//.....
"</table>";
Related
I don't get it why it says it cannot find symbol append.
do i need to use Stringbuffer? i got this code on a tutorial for receipts from youtube, and the uploader disabled comments so I can't ask him directly. please help me. Im still an amateur at java.
Tell me if I need to post my whole code or what code would you want to see to see errors. thanks in adv.
Calendar timer = Calendar.getInstance();
timer.getTime();
SimpleDateFormat tTime = new SimpleDateFormat("HH:mm:ss");
tTime.format(timer.getTime());
SimpleDateFormat Tdate = new SimpleDateFormat("dd-MMM-yyyy");
Tdate.format(timer.getTime());
jtxtReceipt.append("\ Water Station Receipt:\n" +
"Reference:\t\t\t" + refs +
"\n=========================================\n" +
"Mineral:\t\t\t" + jtxtMineral.getText() + "\n\n" +
"Purified:\t\t\t" + jtxtPurified.getText() + "\n\n" +
"Travel:\t\t\t" + jtxtTravel.getText() + "\n\n" +
"VAT:\t\t\t" + jtxtVat.getText() + "\n"+
"\n========================================\n" + "\n" +
"Tax:\t\t\t" + jtxtTax2.getText() + "\n" +
"Subtotal:\t\t\t" + jtxtSubTotal.getText() + "\n" +
"Total:\t\t\t" + jtxtTotal.getText() + "\n" +
"===========================================" +
"\nDate:" + Tdate.format(timer.getTime()) +
"\ntTime:" + tTime.format(timer.getTime()) +
"\n\t\tThank you ");
The append method doesn't exist in the String class. You can either user a StringBuilder to do the job, or if it's a light concatenation, just use the + operator
The append method doesn't work on TextField Palette. So, if You're on TextField Palette, replacing that with TextArea Palette should solve the problem.
In a Java application, I am creating a String like below (by concatenation):
String notaCorrente = dataOdierna + " - " + testoNotaCorrente;
My problem is that I want to add also something like an HTML newline character at the end of this String (that will be shown into an HTML page).
How can I implement it?
The newline character in Java is "\n" which will look like this:
String notaCorrente = dataOdierna + " - " + testoNotaCorrente + "\n";
However, this will not display as you expect on your HTML page. You can try adding an html break tag, or add the
(Line Feed) and
(Carriage Return) HTML entities:
String notaCorrente = dataOdierna + " - " + testoNotaCorrente + "<br>";
or
String notaCorrente = dataOdierna + " - " + testoNotaCorrente + "

";
For a newline that will result in a line break in HTML, use
String notaCorrente = dataOdierna + " - " + testoNotaCorrente + "<br>";
For a newline that will result in a line break in your text editor, use
String notaCorrente = dataOdierna + " - " + testoNotaCorrente + System.lineSeparator();
And for both, use
String notaCorrente = dataOdierna + " - " + testoNotaCorrente + "<br>" + System.lineSeparator();
Why not \n?
\n is specific to certain operating systems, while others use \r\n. System.lineSeparator() will get you the one that is relevant to the system where you are executing your application. See the documentation for more info on this function, and Wikipedia for more info on newlines in general.
Simply, need to add <br/> (break line tag of HTML).
String notaCorrente = dataOdierna + " - " + testoNotaCorrente + "<br/>";
so, while you are going to display this content, <br/> tag will rendered on HTML page in form of new line.
I read, some StackOverflow questions that I need to use HTML stuffs.
But what would be the easiest it without any of HTML stuff.
Here's the code
label.setText(label.getText() + (String)boxTimes.getSelectedItem() + input);
This code will produce this
What I want is:
You must know a bit of basic String format:
\n line break
\t tab
So your code will be like:
String myLabel =
// 4
label.getText() + "\n\n" +
// 7:00
(String)boxTimes.getSelectedItem() + "\t" +
// - Going out....
"- " + input;
label.setText(myLabel);
But as long as JLabel does not accept \n as Abishek Manoharan pointed, you must use <br>.
String myLabel =
"<html>" +
label.getText() +
"<br/><br/>" +
(String)boxTimes.getSelectedItem() + " - " + input +
"</html>;
label.setText(myLabel);
I was faced with the same problem too and couldn't find a viable solution.
So I went ahead and used a JTextArea instead of JLabel.
JTextArea label = new JTextArea();
label.setEditable(false);
label.setBackground(null);
It gives the same look and feel of a JLabel
You can use '\n' and '\t' as you like,
and what more, the text is selectable which is not possible in JLabel.
I have created an html string in which I need to set the background image.
protected void onPostExecute(HashMap<String,String> hPlaceDetails){
String backgroundImage = ??????
String name = hPlaceDetails.get("name");
String icon = hPlaceDetails.get("icon");
String vicinity = hPlaceDetails.get("vicinity");
String lat = hPlaceDetails.get("lat");
String lng = hPlaceDetails.get("lng");
String formatted_address = hPlaceDetails.get("formatted_address");
String formatted_phone = hPlaceDetails.get("formatted_phone");
String website = hPlaceDetails.get("website");
String rating = hPlaceDetails.get("rating");
String international_phone_number = hPlaceDetails.get("international_phone_number");
String url = hPlaceDetails.get("url");
String mimeType = "text/html";
String encoding = "utf-8";
String data = "<html>"+
"<body background="+backgroundImage+"><img style='float:left' src="+icon+" /><h1><center>"+name+"</center></h1>" +
"<br style='clear:both' />" +
"<hr />"+
"<p>Vicinity : " + vicinity + "</p>" +
"<p>Location : " + lat + "," + lng + "</p>" +
"<p>Address : " + formatted_address + "</p>" +
"<p>Phone : " + formatted_phone + "</p>" +
"<p>Website : " + website + "</p>" +
"<p>Rating : " + rating + "</p>" +
"<p>International Phone : " + international_phone_number + "</p>" +
"<p>URL : <a href='" + url + "'>" + url + "</p>" +
"</body></html>";
// Setting the data in WebView
mWvPlaceDetails.loadDataWithBaseURL("", data, mimeType, encoding, "");
}
Note: I have my background image (background9.png) in the location MyProject/res/drawable-xhdpi. Please suggest how I need to set
<body background="+backgroundImage+">
Instead of using \res\drawable-xhdp\background9.png as the path as you mentioned in your comment, you should use:
<body background='file:///android_res/drawable/background9.png'>
I've just tested this and it works perfectly.
Please add a tag for android to clarify your question.
Anyway, you need to get a reference for your parent layout, by using findViewById(R.id.yourActivitysParentLayout)
You need then to set it's background using a ResourceManager. From the top of my head, the method name you will need is yourparent.setBackgroungDrawable(drawable). The drawable can be obtained from a resource manager instance.
This can all be avoided by setting it's background in the xml if possible by using android:background="#drawable/background9" //sorry not sure if .png is needed. xhdp is not though.
I have this statement:
s = s + "Id: " + lc.getID() + " Name: " + lc.getName() + "\n"
+ " Phone Number: " + lc.getPhone() + " Email: " + lc.getEmail() + "\n"
+ " Description: " + lc.getDescription() + "\n\n"
that prints this out:
Id: 1 Name: Eric
Phone Number: 8294038 Email: foo#gmail.com
Description: Cool guy Eric
I want to Bold only the titles (Id, Name, etc).
I tried this:
s = s + Html.fromHtml(" <b> Id: </b>" + lc.getID() + " <b> Name: </b>" + lc.getName() + "\n"
+ " Phone Number: " + lc.getPhone() + " Email: " + lc.getEmail() + "\n"
+ " Description: " + lc.getDescription() + "\n"
+ "\n\n");
But not only does it not bold, but it takes away the new lines (\n). Any ideas on how to get this done? Thanks.
Html.fromHtml() returns a Spanned object, designed to be put directly into a TextView or similar widget.
A Spanned is not a String.
By doing s = s + Html.fromHtml(...), you are saying "please parse this HTML into a Spanned, then throw out all the formatting to give me a String that I can concatenate onto some other String". That's not what you want -- you want to keep the formatting. But a Java String does not have formatting, and so ordinary string concatenation has no way to keep it.
Beyond that, as Manishika pointed out, newlines are ignored in HTML anyway, as you use HTML elements for vertical whitespace.
Your options include:
Generate a complete HTML snippet -- including whatever it is you are trying to concatenate it to -- and then use Html.fromHtml() on the entire thing. You may wish to use a template engine (e.g., jmustache) for that, or String.format(). Or, use StringBuilder, rather than lots of + operations (less memory churn, faster performance). Be sure to use <br/> or <p> for your line breaks/paragraph delimiters.
Use SpannableStringBuilder to assemble the string and its formatting from component parts.
Use TextUtils.concat(s, Html.fromHtml(...)) instead of s + Html.fromHtml(...), as concat() will maintain the spans that implement the formatting. While the implementation of Spanned returned by fromHtml() is not a String, both it and String are a CharSequence, and hence work with concat().
It will require a little bit of parsing on your end, but you definitely want to look into SpannableStrings.
For example, let's say I have the following string:
String s = "How now brown cow";
I would then turn it into a SpannableString by simply feeding the string to the constructor as follows:
SpannableString ss = new SpannableString(s);
From there you need your stylization with the spanned area. For this, I'll just use SubscriptSpan, though if you wish to make your own you can simply make your own class extending CharacterStyle and override the updateDrawState(TextPaint ds) method. The following is how you can set you span:
/ *
* the first argument is the span effect you want, the second and third
* are the start and end indices, respectively, and the last argument is
* for setting a flag, which you probably won't need.
*/
ss.setSpan(new SubscriptSpan(), 0, 2, 0);
And now you can just put your string straight into the TextView and it should appear how you want, like so:
myTextView.setText(ss);