0

I have textArea inside panel. When I add a line border to panel, it is coming perfectly around textarea. But when I add titled border to panel some of the text in textarea is missing from the visible area. I think there is some inset issue here. Can someone please help in setting the insets to titled border?

Posting my code of layoutcomponents:

setPreferredSize(new java.awt.Dimension(250, 40));
add(summaryTextField);
Border border = (BorderFactory.createLineBorder(Color.red));
setBorder(BorderFactory.createTitledBorder(border,"Details"));
MRavindran
  • 447
  • 2
  • 10
  • 24
  • Consider providing a [runnable example](https://stackoverflow.com/help/mcve) which demonstrates your problem. This is not a code dump, but an example of what you are doing which highlights the problem you are having. This will result in less confusion and better responses – MadProgrammer Jul 03 '15 at 04:45
  • 1
    I have suspicions that `setPreferredSize(new java.awt.Dimension(250, 40));` isn't helping – MadProgrammer Jul 03 '15 at 04:46
  • setPreferredSize(new java.awt.Dimension(250, 40)); is working when i set line border to panel – MRavindran Jul 03 '15 at 04:50
  • 1
    And now it's not? Hmm.... [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi) – MadProgrammer Jul 03 '15 at 04:50
  • You've basically overridden any settings that either the layout manager or `Border` would otherwise provide and thrown away the calculations that they would otherwise provide in calculating the preferred size of the container – MadProgrammer Jul 03 '15 at 04:52
  • Hi it worked when I added gridbaglayout to panel. setPreferredSize(new java.awt.Dimension(250, 50)); – MRavindran Jul 03 '15 at 05:02
  • 1
    It worked "before" or it "now" works? `setPreferred/Minimum/MaximumSize` is a bad idea generally – MadProgrammer Jul 03 '15 at 05:04

1 Answers1

0

Hi it worked when I added gridbaglayout to panel.

 setPreferredSize(new java.awt.Dimension(250, 50));
setLayout(new GridBagLayout());
    GridBagConstraints constraints = new GridBagConstraints();
    constraints.gridx = 0;
    constraints.gridy = 0;
    constraints.gridheight = 1;
    constraints.gridwidth = 1;
    constraints.weightx = 1.0;
    constraints.weighty = 1.0;
    constraints.fill = GridBagConstraints.HORIZONTAL;
    constraints.anchor = GridBagConstraints.LINE_START;
    constraints.insets = new Insets(0, 5, 5, 0);
    add(summaryTextField, constraints);
    setBorder(BorderFactory.createTitledBorder("Details"));

Please refer to http://www.coderanch.com/t/333380/GUI/java/Adjust-border-margins-size-JPanel

MRavindran
  • 447
  • 2
  • 10
  • 24