JLabel which hides text after reaching certain length or number of values?

JLabel will automatically add '...' when there is not enough room to display it's contents. So if you are looking to constrain by pixel width just set the maximum size on a label and use a layout manager that obeys this setting (GridbagLayout maybe).

JLabel will automatically add '...' when there is not enough room to display it's contents. So if you are looking to constrain by pixel width just set the maximum size on a label and use a layout manager that obeys this setting (GridbagLayout maybe). However you'd probably want to constrain by a certain number of names.

Here's an example with a label showing the first four names in front of a '...' button. When the button is clicked it changes the text of the label to show all names, and the button removes itself from the layout. Full names text is available on tooltip.

Import java.awt. Color; import java.awt. Dimension; import java.awt.

GridBagLayout; import java.awt.event. ActionEvent; import java.awt.event. ActionListener; import javax.swing.

BorderFactory; import javax.swing. JButton; import javax.swing. JFrame; import javax.swing.

JLabel; import javax.swing. JPanel; public class LabelDotTest { private String fullText = ""; private String clippedText = ""; public LabelDotTest() { JFrame frame = new JFrame(); frame. SetDefaultCloseOperation(JFrame.

EXIT_ON_CLOSE); frame. SetSize(new Dimension(280, 50)); frame. SetLocationRelativeTo(null); String testNames = new String{"John", "Mary", "Peter", "Hank", "Alys", "Debbie"}; int DISPLAY_MAX = 4; for(int i=0; iLength; i++) { fullText += testNamesi; if (iSetBorder(BorderFactory.

CreateEmptyBorder()); button. SetOpaque(false); button. SetBackground(new Color(0,0,0,0)); button.

SetToolTipText(fullText); button. AddActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { label. SetText(fullText); button.getParent().

Remove(button); } }); JPanel panel = new JPanel(new GridBagLayout()); panel. Add(label); panel. Add(button); frame.

Add(panel); frame. SetVisible(true); } public static void main(String args) { new LabelDotTest(); } }.

Thanks for your detailed answer and code sample. This works exactly as my question specified. – Patrick Kiernan Apr 12 '10 at 9:25.

Not what you want but another solution would be put the short text in the text for your label and set the tooltip for the label to the long text so the user could read the full text by hovering over the label.

Not a bad suggestion, thanks – Patrick Kiernan Mar 26 '10 at 11:45.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions