JLabel which hides text after reaching certain length or number of values
The purpose of the JLabel is to show who a message is to, like in a mail client e.g.
To: John, Mary, Peter, Frank, Tom, Harry
I will have the names in a vecto开发者_如何学Gor so can build up a string from that and then set the label's text to this string. However it has the potential to get quite long. I was think it might be nice to have something like this:
To: John, Mary, Peter, Frank, Tom, Harry, ...
Then when you click on the '...', it will expand more or just show a tool tip if you mouse over the ... Yes this idea is stolen from Thunderbird! I am open to other ideas, don't have to use a JLabel.
Has anyone got any suggestions?
Thanks.
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.
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; i<testNames.length; i++)
{
fullText += testNames[i];
if (i<DISPLAY_MAX)
clippedText += testNames[i];
if (i<testNames.length-1)
{
fullText += ", ";
if (i<DISPLAY_MAX)
clippedText += ", ";
}
}
final JLabel label = new JLabel(clippedText);
label.setToolTipText(fullText);
final JButton button = new JButton("...");
button.setBorder(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();
}
}
精彩评论