I understand you can make a new line using <html> <br> </html> but when I add a variable (is a string type) to the text of a label this command doesn't work any more.
label.setText("blahblahblah" + variable + "<html><br>blahblahblah</html>");
I need it to output:
blahblahblah
blahblahblah
You need to have the <html> tag as the first thing in your String:
label.setText("<html>blahblahblah" + variable + "<br>blahblahblah</html>");
It should be:
label.setText("<html>blahblahblah" + variable + "<br>blahblahblah</html>");
Related
When using JLabel, is there a way to use HTML and method call together?
For example:
JLabel speedLabel = new JLabel("<html><b>Speed: </b></html>" + plane.getSpeed());
This doesn't work.
It only shows Speed: and ignores the rest.
Try to change your code to this:
JLabel speedLabel = new JLabel("<html><b>Speed: " + plane.getSpeed() + " </b></html>");
The text, or in your case the airplane speed, that is probably a double, must be in between the <html> and </html> tags, to be recognized.
it is discarding the values after html close tag if you something like this it will work JLabel speedLabel = new JLabel("<html><b>Speed: " + plane.getSpeed() + "</b></html>");
I'm looking for an easy way to make some letters in a label bold.
I have a string like this:
String r = "y = "+output0+" "+output1+"sin(x) "+output2+"cos(x)";
and a label:
Label s = new Label(r);
and I need to make the "y" and "sin(x), cos(x)" bold. I tried using HTML, but it didn't work (maybe i was using it wrong). If I set font for that label, then the whole label is bold (including those outputs) which is not what I need.
You can use html style in JLabels and in some other Java components. If you start your text with <html> and end it with </html>, the html code in your JLabel will be rendered.
This should resolve your issue:
JLabel myLabel = new JLabel();
// The following line is required to make this JLabel's text not bold as JLabel's text is bold be default.
myLabel.setFont(myLabel.getFont().deriveFont(Font.PLAIN));
myLabel.setText("<html><strong>y</strong> = " + output0 + " " + output1 + "<strong>sin(x)</strong> " + output2 + "<strong>cos(x)</strong></html>");
Try using HTML (you have to start the string with <html> and end it with </html>):
new Jlabel("<html>Normal text. <strong>This is bold.</strong></html>")
Explanation! <html> tells Java to render HTML (Hyper Text Markup Language), and <strong> tells HTML that the text inside is of strong importance, normally represented in bold.
Here is my CODE to deleted everything inside the <>.
public static void main (String [] args) throws FileNotFoundException{
Scanner console = new Scanner(System.in);
Scanner Theinput = GetUserInput (console);
while (Theinput.hasNextLine()){
String Input = Theinput.nextLine();
Scanner text = new Scanner(Input);
if (text.hasNext()){
String MyNewText = Input;
while(MyNewText.contains("<") || MyNewText.contains(">") ){
int Max = MyNewText.indexOf ( ">" );
int Min = MyNewText.indexOf ( "<" );
String Replacement = "";
String ToReplacement = MyNewText.substring (Min,Max+1);
MyNewText = MyNewText.replaceAll(ToReplacement,Replacement);
}
System.out.println (MyNewText);
}
else {
System.out.println();
}
}
}
i basically is try to converge a this text
<HEAD>
<TITLE>Basic HTML Sample Page</TITLE>
</HEAD>
<BODY BGCOLOR="WHITE">
<CENTER>
<H1>A Simple Sample Web Page</H1>
<IMG SRC="http://sheldonbrown.com/images/scb_eagle_contact.jpeg">
<H4>By Sheldon Brown</H4>
<H2>Demonstrating a few HTML features</H2>
</CENTER>
HTML is really a very simple language. It consists of ordinary text, with
commands that are enclosed by "<" and ">" characters, or bewteen an "&" and a ";". <P>
You don't really need to know much HTML to create a page, because you can copy bits
of HTML from other pages that do what you want, then change the text!<P>
This page shows on the left as it appears in your browser, and the corresponding HTML
code appears on the right. The HTML commands are linked to explanations of what they do.
<H3>Line Breaks</H3>
HTML doesn't normally use line breaks for ordinary text. A white space of any size is
treated as a single space. This is because the author of the page has no way of knowing
the size of the reader's screen, or what size type they will have their browser set for.<P>
If you want to put a line break at a particular place, you can use the "<BR>" command,
or, for a paragraph break, the "<P>" command, which will insert a blank line.
The heading command ("<4></4>") puts a blank line above and below the heading text.
<H4>Starting and Stopping Commands</H4>
Most HTML commands come in pairs: for example, "<H4>" marks the beginning of a size 4
heading, and "</H4>" marks the end of it. The closing command is always the same as the
opening command, except for the addition of the "/".<P>
Modifiers are sometimes included along with the basic command, inside the opening
command's < >. The modifier does not need to be repeated in the closing command.
<H1>This is a size "1" heading</H1>
<H2>This is a size "2" heading</H2>
<H3>This is a size "3" heading</H3>
<H4>This is a size "4" heading</H4>
<H5>This is a size "5" heading</H5>
<H6>This is a size "6" heading</H6>
<center>
<H4>Copyright ?1997, by
Sheldon Brown
</H4>
If you would like to make a link or bookmark to this page, the URL is:
<BR> http://sheldonbrown.com/web_sample1.html</body>
after all i my output stop at and everything work but just that.
I JUST HAD NO IDEA THAT WHAT HAPPEN TO THE PRINTLN()
This is a size "5" heading
This i
String out = "<TITLE>Basic HTML Sample Page</TITLE>".replaceAll("</?[a-zA-Z0-9]+?>", "");
System.out.println(out);
you can try regular expression, but it can't handle something like
<a
href="http://google.com"
target="_blank"
>google</a>
maybe you should consider using a parser, for example Jsoup
I have html div class formated accordingly....
<div class="latest-media-images">
<div class="hdr-article">LATEST IMAGES</div>
<a class="lnk-thumb" href="http://media.pc.ign.com/media/093/093395/imgs_1.html"><img id="thumbImg1" src="http://media.ignimgs.com/media/thumb/351/3513804/the-elder-scrolls-v-skyrim-20110824023151748_thumb_ign.jpg" class="latestMediaThumb" alt="" height="109" width="145"></a>
<a class="lnk-thumb" href="http://media.pc.ign.com/media/093/093395/imgs_1.html"><img id="thumbImg2" src="http://media.ignimgs.com/media/thumb/351/3513803/the-elder-scrolls-v-skyrim-20110824023149685_thumb_ign.jpg" class="latestMediaThumb" alt="" height="109" width="145"></a>
<a class="lnk-thumb" href="http://media.pc.ign.com/media/093/093395/imgs_1.html"><img id="thumbImg3" src="http://media.ignimgs.com/media/thumb/351/3513802/the-elder-scrolls-v-skyrim-20110824023147685_thumb_ign.jpg" class="latestMediaThumb" alt="" height="109" width="145"></a>
</div>
Now.... Ive been trying to think of different ways to do this.
I want to parse each URL to sepereate strings for each one...
Now i was thinking of some how parsing them into a list and then selecting each one by passing a position?
(If anyone wants to answer this please feel free too)
Or i could do something such as navigating to the div class...
Element latest_images = doc.select("div.latest-media-images");
Elements links = latest_images.getElementsByTag("img");
for (Element link : links) {
String linkHref = link.attr("href");
String linkText = link.text();
}
I was thinking of this,havent tried it out yet. I will when i get the chance.
But how will i parse each to a seperate string or a whole list using the code?(if its correct)
Feel free to leave suggestions or answers =) or let me know if the code i have above will do the trick.
Thanks,
coder-For-Life22
Here goes code sample to extract all img urls from your html using RegEx:
//I used your html with some obfuscations to test some fringe cases.
final String HTML
= "<div class=\"latest-media-images\">\n"
+ "<div class=\"hdr-article\">LATEST IMAGES</div>\n"
+ "<a class=\"lnk-thumb\" href=\"http://media.pc.ign.com/media/093/093395/imgs_1.html\"><img id=\"thumbImg1\" \n "
+ "src=\"http://media.ignimgs.com/media/thumb/351/3513804/the-elder-scrolls-v-skyrim-20110824023151748_thumb_ign.jpg\" class=\"latestMediaThumb\" alt=\"\" height=\"109\" width=\"145\"></a>\n"
+ "<a class=\"lnk-thumb\" href=\"http://media.pc.ign.com/media/093/093395/imgs_1.html\"><img id=\"thumbImg2\" src= \n"
+ "\"http://media.ignimgs.com/media/thumb/351/3513803/the-elder-scrolls-v-skyrim-20110824023149685_thumb_ign.jpg\" class=\"latestMediaThumb\" alt=\"\" height=\"109\" width=\"145\"></a>\n"
+ "<a class=\"lnk-thumb\" href=\"http://media.pc.ign.com/media/093/093395/imgs_1.html\"><img id=\"thumbImg3\" src "
+ "= \t \n "
+ "\"http://media.ignimgs.com/media/thumb/351/3513802/the-elder-scrolls-v-skyrim-20110824023147685_thumb_ign.jpg\" class=\"latestMediaThumb\" alt=\"\" height=\"109\" width=\"145\"></a>\n"
+ "</div>";
Pattern pattern = Pattern.compile ("<img[^>]*?src\\s*?=\\s*?\\\"([^\\\"]*?)\\\"");
Matcher matcher = pattern.matcher (HTML);
List<String> imgUrls = new ArrayList<String> ();
while (matcher.find ())
{
imgUrls.add (matcher.group (1));
}
for (String imgUrl : imgUrls) System.out.println (imgUrl);
The output is the same as Sahil Muthoo posted:
http://media.ignimgs.com/media/thumb/351/3513804/the-elder-scrolls-v-skyrim-20110824023151748_thumb_ign.jpg
http://media.ignimgs.com/media/thumb/351/3513803/the-elder-scrolls-v-skyrim-20110824023149685_thumb_ign.jpg
http://media.ignimgs.com/media/thumb/351/3513802/the-elder-scrolls-v-skyrim-20110824023147685_thumb_ign.jpg
If by using a link to get the html first you mean that you have an url than the only change will be that instead of using a hard-coded String you'll need to load the html first. For example, you can use Java OOB class URL:
new URL ("http://some_address").openConnection ().getInputStream ();
Elements thumbs = doc.select("div.latest-media-images img.latestMediaThumb");
List<String> thumbLinks = new ArrayList<String>();
for(Element thumb : thumbs) {
thumbLinks.add(thumb.attr("src"));
}
for(String thumb : thumbLinks) {
System.out.println(thumb);
}
Output
http://media.ignimgs.com/media/thumb/351/3513804/the-elder-scrolls-v-skyrim-20110824023151748_thumb_ign.jpg
http://media.ignimgs.com/media/thumb/351/3513803/the-elder-scrolls-v-skyrim-20110824023149685_thumb_ign.jpg
http://media.ignimgs.com/media/thumb/351/3513802/the-elder-scrolls-v-skyrim-20110824023147685_thumb_ign.jpg
Obviously you can parse the html into a DOM tree and extract all "img" nodes using XPath or CSS selector. And then iterating through them fill an array of links.
Though your code doesn't exactly do the trick.
The cycle is written to work with "a" nodes while the code before it extracts img nodes.
There's also another way: you can extract required data using RegEx which should have better performance and less memory cost.
How can I put a string containing html text to be displayed in a JavaScript function?
String test = "DisplayNext(\"String containing html tags\")";
String NextLink = "<br> Next";
JavaScript function:
function DisplayNext(Next){
alert(Next);
}
I want to pass html tags to a JavaScript function and display it using an alert box..How do I do that ??
You'll need to encode your html tags.
> becomes >
< becomes <
" becomes "
You'll also need to encode your JavaScript.
' becomes \'
Then use single quotes to quote your JavaScript (so the double quotes don't close the onclick attribute prematurely):
String test = "DisplayNext('" + encodedHTML + "')";
String nextLink = "<br /> Next";
Javascript alert boxes aren't HTML viewers. You can try replacing your alert calls with this:
http://plugins.jquery.com/plugin-tags/messagebox