0

Here's my problem: I'm using a JTable and made my own AbstractTableModel. So far so good. When the text is too long, the cell renderer will truncate the text and show ellipses , but some of my databases registries also have ellipses. Ex: Program Languages I've been studying:

first registry "Visual Basic (2003-2007)"
second registry "C++ (2006-2010)"
third registry "Java (2010-...)"

Means I stopped trying to learn VB in 2007. Stopped C++ in 2010. I'm still learning Java. But when the column is not so large, it will truncate, and sometimes it shows this:

"Visual Basic (2003-..."
"C++ (2006-2010)"
"Java (2010-...)"

The user might get confused, thinking "Visual Basic (2003-..." means "Visual Basic (2003 - ...)" when it should mean "Visual Basic (2003-2007)"

Sorry if I wasn't clear enough, I hope everyone understands, I just want the JTable to truncate the text without the Ellipses.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
Avenger
  • 13
  • 3
  • Take a look at http://stackoverflow.com/questions/7971178/find-out-if-text-of-jlabel-exceeds-label-size More precisely at the first answer, it should help. – Jonathan Drapeau Jul 04 '13 at 14:55
  • I've read the answear and the post it redirects to, but the post is about CSS, and I am developping a Desktop JAVA application, but thanks for the answear and sorry if I didnt understand. – Avenger Jul 04 '13 at 15:15
  • Even the link to the example in the answer that isn't about CSS? http://stackoverflow.com/questions/3597550/ideal-method-to-truncate-a-string-with-ellipsis/3597688#3597688 And look at the answer above that one... they are all about pure Java. – Jonathan Drapeau Jul 04 '13 at 15:31
  • @JonathanDrapeau: Thank you for the link, which I think is apropos. Avenger: I (prematurely) voted to close this question for failing to "demonstrate a minimal understanding of the problem being solved." I thought you were giving your résumé, but I think you meant to give an example. I've edited the question to clarify. – trashgod Jul 04 '13 at 18:45
  • Yes it was only an exemple, the real table is about car...pieces? like selling air filters, oil, reels, etc... Some of them are for exclusive car in made in specific years, others are for any year model, so we use the "..." to specify "any year", I just cant show those "..." like the JTable does, cause our sellers WILL be confused, thanks, any and every help is welcome – Avenger Jul 04 '13 at 20:37

1 Answers1

4

This answer shows how the ellipses arise; instead of trying to defeat them, the better approach is to add your JTable to a layout that allows the table to adjust as the enclosing Window is resized. In this example, the table fills BorderLayout.Center, the default for JFrame. For a more appealing initial appearance,

Addendum: Although I don't recommend it from a usability perspective, you can omit the ellipsis in a custom subclass of DefaultTableCellRenderer that uses a custom LabelUI and overrides layoutCL(), as shown here and here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • I'm sorry, I don't think I understood. I do change my Column Widths dynamically. The point is, there are some descriptions really large, like: "Mineral Oil for Ventilated engines of New Beetle, Ecosport and Fiesta 2007/2009" if the screen (panel, jframe, jdialog) hits its maximum size, then the column also hits a maximum size and the "..." will show, sometimes changing "2007/2009" into "2007/..." if the user pays attention, he will see there's more text in the cell, but as a programmer, I can't leave this big "IF the user is smart enough to see" – Avenger Jul 04 '13 at 20:52
  • Yes, there's always _some_ text that's too long to be seen _in situ_. Among the _many_ ways to address this, I like tooltips, dialogs or some variation of [`TablePopupEditor`](http://stackoverflow.com/a/3591230/230513). – trashgod Jul 05 '13 at 00:53
  • This post is about editting the cell, I don't want to edit it. My concern is about our vendors, they will list our products, and if they see a "2001/..." they will sell the wrong one. My JTable is not editable only the Columns size and row order (sort) can be chosen by the user, though Column size changes automatically with window size. I just wanted to choose what cell overflow the JTable uses, or take away any sign of overflow. If a vendor sees "2001" they know there's something beyond, but if he sees "2001/..." he thinks the text ends there. thanks for the reply – Avenger Jul 05 '13 at 11:29
  • Right, you'd want a tooltip, dialog or (notional) `TablePopupRenderer`. Although I don't recommend it, you can omit the ellipsis in a custom [renderer](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#editrender) that uses a custom `LabelUI` and overrides `layoutCL()`, as shown in the first example cited. – trashgod Jul 05 '13 at 11:38
  • I was reading through LabelUI, trying to find a way to override the layoutCL. I found [this post](http://stackoverflow.com/questions/3447078/how-to-modify-the-clip-string-ellipses-used-by-swing-when-laying-out-text-compon) But I don't know how to register my LabelUI, any help on this? – Avenger Jul 05 '13 at 13:25
  • OK, I did the trick for a JLabel. BUT, JTable uses JTextField, and not a JLabel. TextUI doesn't have the layoutCL method. What method does the TextUI uses to clip text? – Avenger Jul 05 '13 at 13:51
  • Solved. The DefaultTableCellRenderer acceps LabelUI, I don't need to change the JTextField. What do I do now? How do I close this thread? Should I post the code? I only changed MetalLabelUI from [this post](http://stackoverflow.com/questions/3447078/how-to-modify-the-clip-string-ellipses-used-by-swing-when-laying-out-text-compon) to BasicLabelUI – Avenger Jul 05 '13 at 14:38