GetPreferredSize for JLabel - basically you have to get textLength from JLabel in pixels, there are 3 correct ways, but I love: SwingUtilities. ComputeStringWidth(FontMetrics fm, String str) Now you are able to setPreferredSize for JLabel correctly (please by defalut is there needed to add +5 - +10 to the Integer that returns SwingUtilities. ComputeStringWidth) Call pack(); on the top level container (the JDialog).
1 there is no need to go to all this trouble. By invoking label. SetText(...) the component is automatically revalidated and repainted.
The preferred size is automatically recalculated. All that is necessary is the pack() after the setText(). – camickr Jul 27 at 14:16 @camickr thanks for comment, agreed in case if there is some LayoutManager used, not AbsoluteLayout();, then you can set dynamically nothing(); – mKorbel Jul 27 at 14:21 a JLabel always has a preferred size whether you are using a layout manager or not.
– camickr Jul 27 at 14:31.
Try invoking validate() on the JDialog. I just realized that this will not work. Here's a nice thread that discusses this issue.
I would recommend listening to the answer and suggestions provided by @Andrew Thompson, who's actually a prominent user here.
Another alternative is to put the text into a JTextArea and put that in a JScrollPane. E.G. Code import java.awt. *; import java.awt.event.
*; import javax.swing. *; class ExpandingText { public static void main(String args) { SwingUtilities. InvokeLater(new Runnable() { public void run() { final String s = "The quick brown fox jumped over the lazy dog."; final JTextArea textArea = new JTextArea(s,5,30); textArea.
SetWrapStyleWord(true); textArea. SetLineWrap(true); textArea. SetEditable(false); textArea.
SetFocusable(false); JButton button = new JButton("Add Text"); button. AddActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae) { textArea. Append(s); } }); JPanel panel = new JPanel(new BorderLayout(3,3)); panel.
Add(button, BorderLayout. NORTH); panel. Add(new JScrollPane(textArea), BorderLayout.
CENTER); JOptionPane. ShowMessageDialog(null, panel); } }); } }.
3 +1, Makes more sense to me as well. I doubt the users like seeing the dialig continually changing is size just because the text of one field changes. – camickr Jul 27 at 14:17.
You could use setSize and base the width off the width of the JLabel. Call this method whenever the user changes the input.
It is rarely a good idea to use setSize(). You need to know the size of all the other components on the dialog as well when determining the overall size. Let pack() do the job.
T – camickr Jul 27 at 14:19 I will update my answer to reflect the usage I thought about. – user119179 Jul 27 at 16:12.
Trying using the validate() method, this will cause the JDialog to lay out its subcomponents again EDIT This apparently will not work.
– Hunter McMillen Jul 27 at 14:06 I didn't downvote but validate() will not change the "width of the Dialog" which is what the question is asking. The pack() method is used for this. – camickr Jul 27 at 14:13 Ahh I see I thought it would.
My mistake. Blind downvotes don't let anyone know what is going on. – Hunter McMillen Jul 27 at 14:20 @Hunter, +1 to negate down-vote.
I suggested the same thing, but realized it was not true. – Moonbeam Jul 27 at 14:22.
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.