Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8362171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:52:48+00:00 2026-06-09T11:52:48+00:00

I have an SWT Tree (via a JFace TreeViewer), that displays items in columns,

  • 0

I have an SWT Tree (via a JFace TreeViewer), that displays items in columns, some of which have long strings. The end of the string is the meaningful bit for the user, not the start, so when the text is clipped I want the clipping to occur at the start of the string and not the end. Example:

The default behaviour for a cell containing something like: “This is a very long string that completely exceeds the bounds of the tree column” is:

|This is a very l...|

Where as I want:

|... the tree column|

EDIT:

I solved this with a custom PaintItem listener as described here: http://www.eclipse.org/articles/article.php?file=Article-CustomDrawingTableAndTreeItems/index.html

Came up with the following code (not quite perfect, some duplication & magic numbers in there):

    tree.addListener(SWT.EraseItem, new Listener()
    {
        public void handleEvent(Event event)
        {
            String text = ((TreeItem)event.item).getText(event.index);
            Point size = event.gc.textExtent(text);
            TreeColumn column = ((Tree)event.widget).getColumn(event.index);                
            int columnWidth = column.getWidth() - 10; /* magic number alert - the cells have some padding - must be a way of determining this... */ 
            if(size.x > columnWidth)
            {
                event.detail &= ~SWT.FOREGROUND;
            }
        }
    });

    tree.addListener(SWT.PaintItem, new Listener()
    {               
        @Override
        public void handleEvent(Event event)
        {   
            String text = ((TreeItem)event.item).getText(event.index);
            Point size = event.gc.textExtent(text);
            TreeColumn column = ((Tree)event.widget).getColumn(event.index);                
            int columnWidth = column.getWidth() - 10; /* magic number alert - the cells have some padding - must be a way of determining this... */ 
            if(size.x > columnWidth)
            {
                drawTextTail(event, text, columnWidth);
            }                               
        }

        private void drawTextTail(Event event, String text, int columnWidth)
        {
            String clippedText = "";
            int offset = text.length() - 1; 
            String nextClippedText = text.charAt(offset) + clippedText;
            while(fits(nextClippedText, columnWidth, event.gc))
            {
                clippedText = nextClippedText;
                offset--;
                nextClippedText = text.charAt(offset) + clippedText;
            }
            event.gc.drawText("..." + clippedText, 
                    event.x + 5, /* magic number alert - the cells have some padding - must be a way of determining this... */ 
                    event.y, false);
        }

        private boolean fits(String clippedText, int columnWidth, GC gc)
        {
            Point size = gc.textExtent("..." + clippedText);                        
            return size.x < columnWidth;
        }
    });
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T11:52:50+00:00Added an answer on June 9, 2026 at 11:52 am

    Try this. The second column automatically crops the String to fit into the column.

    public static void main(String[] args) {
        Display display = new Display();
        final Shell shell = new Shell(display);
        shell.setLayout(new FillLayout());
    
        final Tree tree = new Tree(shell, SWT.NONE);
        tree.setLayout(new FillLayout());
        tree.setHeaderVisible(true);
    
        TreeColumn column = new TreeColumn(tree, SWT.NONE);
        column.pack();
        column.setWidth(100);
    
        TreeColumn column2 = new TreeColumn(tree, SWT.NONE);
        column2.pack();
        column2.setWidth(100);
    
        TreeItem item = new TreeItem(tree, SWT.NONE);
        item.setText(0, "Baaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaz");
        item.setText(1, "VERY VERY VERY VERY VERY VERY VERY VERY VERY VERY VERY long text");
    
        final TextLayout textLayout = new TextLayout(display);
    
        tree.addListener(SWT.PaintItem, new Listener() {
            public void handleEvent(Event event) {
                TreeItem item = (TreeItem) event.item;
    
                Point pt = new Point(event.x + 2, event.y + 2);
    
                for (int i = 0; i < tree.getColumnCount(); i++) {
                    Rectangle rect = item.getBounds(i);
    
                    String text = item.getText(i);
    
                    if (rect.contains(pt)) {
                        String clippedText = "";
                        int offset = text.length() - 1; 
                        int columnWidth = tree.getColumn(i).getWidth();
                        String nextClippedText = text.charAt(offset) + clippedText;
                        while(fits(nextClippedText, columnWidth, event.gc) && offset >= 0)
                        {
                            clippedText = nextClippedText;
                            offset--;
                            if(offset >= 0)
                                nextClippedText = text.charAt(offset) + clippedText;
                        }
    
                        textLayout.setText(clippedText);
    
                        textLayout.draw(event.gc, event.x + 3, event.y + 3);
                    }
                }
            }
        });
        tree.addListener(SWT.EraseItem, new Listener() {
            public void handleEvent(Event event) {
                /*
                 * indicate that we'll be drawing the foreground in the
                 * PaintItem listener
                 */
                event.detail &= ~SWT.FOREGROUND;
            }
        });
    
        shell.pack();
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
    }
    
    private static boolean fits(String clippedText, int columnWidth, GC gc)
    {
        Point size = gc.textExtent("..." + clippedText);                        
        return size.x < columnWidth;
    }
    

    It might need some fixing to really show the ... in front of the cropped String, but it’s a start.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an SWT TableViewer which, of course, displays the contents of a Table
I have an eclipse plugin project which uses some swt objects, eg - import
I have Table which has SWT.CHECK style so I can remove checked items. I
Here's a scenario: I have a java front end (RCP/SWT) app which currently has
I have an SWT/JFace application that uses the Realm (not sure of the concept)
I have the following situation I have one swt class which displays a simple
I have a SWT app that opens a OpenGL window (using the LWJGL library)
I am building an application in swt, I also have a thread running which
I have a TreeViewer where some nodes represent folders, so I wanted to show
I'm sorry if this question is too basic I have two SWT TreeViewer's in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.