Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: string tabbing problem

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default string tabbing problem

    Hi,

    I'm having a problem with the tabbing space in string. Here's the code (within a class that extends DefaultListCellRenderer):

    super.setText(date + "\t\t\t\t\t\t\t\t\t\t" +
                        Double.toString(data.getDailyHigh()) + "\t\t\t\t\t\t\t\t\t\t\t" +
                        Double.toString(data.getDailyLow()) + "\t\t\t\t\t\t\t\t\t\t\t" +
                        Double.toString(data.getPreviousClose()) + "\t\t\t\t\t\t\t\t\t\t\t" +
                        Double.toString(data.getTrueRange()) + "\t\t\t\t\t\t\t\t\t\t\t" +
                        Double.toString(data.getN()));

    heres the output:



    As you can see, the tab spacing is only one space which makes \t completely pointless and useless.

    Any suggestions?
    Last edited by rhalliwell1; March 9th, 2011 at 05:19 AM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,972
    My Mood
    Hungover
    Thanks
    133
    Thanked 555 Times in 476 Posts

    Default 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?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: string tabbing problem

    One tab:



    I just want them to be aligned in columns, like they are but actually aligned.

  4. #4
    Junior Member
    Join Date
    Mar 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: string tabbing problem

    System.out.println("Tab\tTab");

    Works correctly. :S

    Maybe tabs don't work in JLists?

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,972
    My Mood
    Hungover
    Thanks
    133
    Thanked 555 Times in 476 Posts

    Default 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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    Junior Member
    Join Date
    Mar 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: string tabbing problem

    System.out.println("Tab\tTab");

    Works correctly. :S

    Maybe tabs don't work in JLists?

    SSCCE:

    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);
    	}
    }


  7. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,972
    My Mood
    Hungover
    Thanks
    133
    Thanked 555 Times in 476 Posts

    Default 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:

    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;
    		}
    	}
    }
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #8
    Junior Member
    Join Date
    Mar 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  9. #9
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,972
    My Mood
    Hungover
    Thanks
    133
    Thanked 555 Times in 476 Posts

    Default 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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. #10
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,100
    Thanks
    169
    Thanked 779 Times in 725 Posts
    Blog Entries
    5

    Default Re: string tabbing problem


  11. #11
    Junior Member
    Join Date
    Mar 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: string tabbing problem

    Quote Originally Posted by KevinWorkman View Post
    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.

  12. #12
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,972
    My Mood
    Hungover
    Thanks
    133
    Thanked 555 Times in 476 Posts

    Default Re: string tabbing problem

    Quote Originally Posted by rhalliwell1 View Post
    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?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. [SOLVED] Tabbing columns
    By SnarkKnuckle in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 24th, 2011, 09:50 PM
  2. [SOLVED] Found String Requires Double Problem.
    By NPotter86 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 17th, 2011, 12:24 PM
  3. Strange problem with drawing string
    By Asido in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 26th, 2010, 03:38 PM
  4. array/string problem
    By RSYR in forum Collections and Generics
    Replies: 1
    Last Post: December 18th, 2009, 09:24 PM
  5. String and int problem in swing program
    By duckman in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 21st, 2009, 02:28 AM