0

I am having problems adding a HTML strike-through tag to a string of text in Java.

EDIT: I have posted the entire code and I am using Windows 7, Java 8 and Swing.

JLabel LocalSelectorLabel = new JLabel("<html><strike>Text Here</strike><html>");
LocalSelectorLabel.setBounds(12, 50, 55, 16);
LocalTabPanel.add(LocalSelectorLabel);

I have tried the tags: s, del and strike but all of them just give me underlined text. I have looked around for other tags that might work but I haven't found anything.

EDIT 2: This works for me, thanks:

new JLabel("<html><body><span style='text-decoration: line-through;'>Text Here</span></body></html>");

EDIT 3: Now this is interesting. I ran the code on a different computer and that one displays as strike-through and as underline as expected while the computer I used to ask this question did not. I see now why I didn't find anything when I searched for a solution.

SweSub
  • 13
  • 4
  • What is the context ? Where are you using this string ? A webapp ? A JavaFX app ? – Gaël J Nov 06 '15 at 10:02
  • It works for me. I created a simple JFrame with a label with `` and it worked just fine. Perhaps you should post more of your code, or tell us more specific details about the Java version and operating system. – RealSkeptic Nov 06 '15 at 10:08
  • Note: your string ends with `` instead of `` (note the slash). Could this be the problem? – RealSkeptic Nov 06 '15 at 10:15
  • It doesn't seem to matter if I end with ` – SweSub Nov 06 '15 at 11:02
  • `LocalSelectorLabel.setBounds(12, 50, 55, 16);` 1) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). .. – Andrew Thompson Nov 06 '15 at 11:44
  • 2
    .. 2) Please learn common Java nomenclature (naming conventions - e.g. `EachWordUpperCaseClass`, `firstWordLowerCaseMethod()`, `firstWordLowerCaseAttribute` unless it is an `UPPER_CASE_CONSTANT`) and use it consistently. – Andrew Thompson Nov 06 '15 at 11:44
  • Did anyone notice that his first `` tag contains a `/` before like `` ? – Gabriel Câmara Nov 06 '15 at 12:22
  • Sorry, the / was a typo. As for the comments about the JLabel setBounds. I have no problems with adding the label and it the layout seem to look fine on other OS. – SweSub Nov 06 '15 at 13:03

3 Answers3

8
"<html></strike>Text Here</strike><html>"

Should be:

"<html><strike>Text Here</strike><html>"

The 'opening' </strike> tag should be <strike>.

As an aside, it never hurts to check the HTML using an HTML validator. For 'Swing HTML' set it to HTML 3.2 (or 4.01 transitional, if they stop supporting 3.2).

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Sorry what was a typo – SweSub Nov 06 '15 at 12:54
  • Stop apologising and post a [mcve] that is **copy/pasted** from your IDE. No more of these idiotic hand typed 'something like my code' uncompilable code snippets! – Andrew Thompson Nov 06 '15 at 13:28
  • *"I thought it would be inappropriate to paste the whole code since it's 3000 lines+."* You're right there, but nobody is suggesting that. *"..and I thought that would distract from the actual problem."* What? And you **don't** think it is 'distracting' to post the code you're **not** using? The entire approach to asking for help is messed up, you've got almost no chance of getting help this way. If you cannot be bothered (or can't manage) to prepare and post an MCVE, I don't think you will get an answer. But continue with the current approach if you like, it is *your* problem after all. – Andrew Thompson Nov 06 '15 at 13:46
  • Like I said, I'm new to overflow and I am not very good at asking questions I guess but I got the answer I needed so I thank you all for putting up with my bs – SweSub Nov 06 '15 at 14:16
5

As a continue of @Gaël answer, using css you can achive your desired result with the follwing code:

new JLabel("<html><body><span style='text-decoration: line-through;'>Text Here</span></body></html>");
Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
1

Be aware that <strike> tag is not part of HTML5, you should use CSS to achieve this.

EDIT : you're using Swing, so HTML 3.2 and <strike> tag exists (but still not recommended for future updates).

Gaël J
  • 11,274
  • 4
  • 17
  • 32
  • 3
    Swing supports [HTML 3.2](http://docs.oracle.com/javase/7/docs/api/javax/swing/text/html/package-summary.html), not HTML5. – RealSkeptic Nov 06 '15 at 10:07
  • Might there be any other tag than strike that is a part of HTML? – SweSub Nov 06 '15 at 10:11
  • @SweSub what are you using ? Swing ? JavaFX ? – Gaël J Nov 06 '15 at 10:47
  • I am using Swing. And I have added more information about the problem – SweSub Nov 06 '15 at 11:02
  • Could CSS be used the same way? To modify different parts of the next. For example one word italic and one word strikethrough. If so, could you show me how to do it please? – SweSub Nov 06 '15 at 13:05
  • @SweSub Swing doesn't support CSS. Nor is this going to change in the future (Swing is now in maintenance mode, there will be no radical changes in it. The current active GUI platform in Java is JavaFX). – RealSkeptic Nov 06 '15 at 14:21
  • But this worked: `new JLabel("Text Here");` Isn't that CSS? Sorry for my newbieness – SweSub Nov 06 '15 at 14:23
  • @SweSub Actually, I'm surprised at that. The `` tag is not supported in HTML 3.2, nor is CSS. It must be an extension that was added at some point to Swing. – RealSkeptic Nov 06 '15 at 16:27