Re: string tabbing problem
I'd be curious to see an SSCCE that demonstrates this. What happens if you only place one tab between the text? What do you want to happen instead of what's happening now?
Re: string tabbing problem
One tab:
http://i.imgur.com/ptXyY.png
I just want them to be aligned in columns, like they are but actually aligned.
Re: string tabbing problem
Code :
System.out.println("Tab\tTab");
Works correctly. :S
Maybe tabs don't work in JLists?
Re: string tabbing problem
Like I said, it would be nice to see an SSCCE of what you're actually doing. Otherwise we're just guessing.
Re: string tabbing problem
Code :
System.out.println("Tab\tTab");
Works correctly. :S
Maybe tabs don't work in JLists?
SSCCE:
Code :
import javax.swing.*;
public class SSCCE extends JFrame {
public static void main(String [] args) {
new SSCCE();
}
public SSCCE() {
super("SSCCE");
setSize(200,200);
setDefaultCloseOperation(HIDE_ON_CLOSE);
setUpComponents();
setVisible(true);
}
public void setUpComponents() {
String [] listContent = new String [2];
listContent[0] = "tab\ttab";
listContent[1] = "tab\t\ttab";
JList list = new JList(listContent);
getContentPane().add("Center", list);
}
}
http://i.imgur.com/5NiSS.png
Re: string tabbing problem
Interesting find, and thanks for creating the SSCCE. Apparently other people have dealt with this issue. I googled "java tabs in jlist" and found the following solution:
Tab list renderer : ListSwing JFCJava
But in my humble opinion, that might be overkill for your solution. See if this makes sense:
Code java:
import java.awt.Component;
import javax.swing.*;
public class SSCCE extends JFrame {
public static void main(String [] args) {
new SSCCE();
}
public SSCCE() {
super("SSCCE");
setSize(200,200);
setDefaultCloseOperation(HIDE_ON_CLOSE);
setUpComponents();
setVisible(true);
}
public void setUpComponents() {
String [] listContent = new String [2];
listContent[0] = "tab\ttab";
listContent[1] = "tab\t\ttab";
JList list = new JList(listContent);
list.setCellRenderer(new CustomListRenderer());
getContentPane().add("Center", list);
}
public class CustomListRenderer extends DefaultListCellRenderer{
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
JLabel cell = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
String text = cell.getText();
text = text.replaceAll("\\t", " ");
cell.setText(text);
return cell;
}
}
}
Re: string tabbing problem
Unfortunately your solution, although very intuitive and simple, will not quite do.
Because the items in the columns are of different sizes, just adding " " will not keep them aligned but has solved the problem of having to used 15 '\t''s. I will give the other solution a go.
Thank you very much for the help.
Re: string tabbing problem
Yeah, I was going to point out that simply using tabs probably won't accomplish your goal of alignment. If I were you, I might even use a renderer that puts each cell in its own JLabel, then add them to a JPanel to keep them aligned.
Re: string tabbing problem
Re: string tabbing problem
Quote:
Originally Posted by
KevinWorkman
Yeah, I was going to point out that simply using tabs probably won't accomplish your goal of alignment. If I were you, I might even use a renderer that puts each cell in its own JLabel, then add them to a JPanel to keep them aligned.
That is a good idea! Thanks, I'll try it.
i'd rather not use a JTable but if i must then i will.
Re: string tabbing problem
Quote:
Originally Posted by
rhalliwell1
i'd rather not use a JTable but if i must then i will.
Can I ask why you don't want to use a JTable?