Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to get a tab character in a LJist or Jlabel?  RSS feed

 
Besjamain Greenaway
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm using this in the toString method of one of my objects which I'm sending to the JList, and the other use is for a JLabel I have. I tried using \t, \U0009, \t\t\t\t\t\t\t, etc., but it just won't work. Does anybody know how to accomplish this?
 
Rob Spoor
Sheriff
Posts: 21090
85
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try using HTML for the entire text, and using a PRE block:

This will also change the font though, so you'll have to play around with that.
 
Besjamain Greenaway
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply, though I forgot to say that this is not a web app, I'm very sorry for that. I have an object in which I'm overriding the toString() method. In the return statement I have something like "blablabla \t blablabla". This object is passed to a JList, but I do not see the tab appearing. Not even when I put in a whole bunch of \t\t\t\t or use the UniCode character.
 
Paul Clapham
Sheriff
Posts: 22487
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think your problem is that you are expecting the tab character to do something in the JLabel. And when you have a tab character in a JLabel, it doesn't do anything. Which seems perfectly reasonable to me. So your problem isn't getting it there, your problem is getting it to do whatever it you think it should be doing. So why don't we start with that? What do you think it should do?
 
Rob Spoor
Sheriff
Posts: 21090
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Besjamain Greenaway wrote:Thanks for your reply, though I forgot to say that this is not a web app, I'm very sorry for that.

I'm well aware of that. This is the Swing / AWT / SWT / JFace forum after all, and you did mention JList and JLabel.

This thing is, JLabel is capable of parsing HTML text (HTML 3.2 compliant) and displaying that. You can use that to your advantage, for instance by using the PRE tag.
 
Besjamain Greenaway
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Paul Clapham

Yes, I expected the tab character to do something in my JLabel.. Or Jlist.. What I think it should do. Well, use the \t I put in there Seriously, I have no idea why the JLabel or JList shouldn't output the \t or Unicode I put in the String.

@Rob Prime

Okay, for a moment there I thought there was miscommunication I will try this out!
 
Paul Clapham
Sheriff
Posts: 22487
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But the JLabel does display the tab character. It renders it as a zero-width character, right? Or maybe it renders it as a single space. The question still remains, what do you want it to do instead?
 
Besjamain Greenaway
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it displays as a zero width, I don't see it making any space. So, I want it not to be zero width, I want it to be x pixels of space that a \t defines.
 
Paul Clapham
Sheriff
Posts: 22487
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then just write something to replace it by a space. Or Rob's HTML solution might work. I was expecting you might want it to behave like a traditional tab, which causes the next character to be aligned at a predefined calculated location which is relative to some background. Anyway, now that we have the requirements clarified (I think) then hopefully we can get on with finding an implementation.
 
Besjamain Greenaway
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, actually I want to use it to align certain characters in the JList. My crude solution so far was finetuning it with the spacebar . Using the tab in the traditional sense would be great if it would be possible.
 
Rob Spoor
Sheriff
Posts: 21090
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you only want to align columns, why not use a JTable?
 
Besjamain Greenaway
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically, because I didn't know it existed and what it is used for. I will look into - thanks!
 
Paul Clapham
Sheriff
Posts: 22487
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a link to the Swing tutorial. Have a look at that, it may save you from spending a lot of time trying to figure out how to drive a nail into a board using a screwdriver in the future.

 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One easy way to align labels would be to set an empty border to the label with the required "padding".
Same way, they can be used in Renderers for JList.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!