1

I'm a newbie. Trying to make auto resize border. I made border on my frame with 2 panels. I added panels with border into first panel.

I want border which retreated from all edges. In this border panel I also added text panel and button. When I expand the window, or resize it panel with border is resizing too. But there is not indents from edges when I am using BorderLayout.

public class App {
private JFrame frame;
private JPanel panel;
private JPanel panel_1;
private JTextField textField;
private JButton addBtn;

public static void main(String args[]) {
    App app = new App();

    app.initialize();

    app.frame.pack();
    app.frame.setVisible(true);
}

private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 800, 600);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    panel = new JPanel();
    frame.getContentPane().add(panel, BorderLayout.NORTH);
    panel.setLayout(new BorderLayout(0, 0));

    panel_1 = new JPanel();
    panel_1.setPreferredSize(new Dimension(784, 40));
    panel_1.setBorder(new LineBorder(new Color(0, 0, 0)));

    panel.add(panel_1, BorderLayout.CENTER);

    textField = new JTextField();
    textField.setPreferredSize(new Dimension(6, 24));
    panel_1.add(textField);
    textField.setColumns(50);

    addBtn = new JButton("Add");
    addBtn.setPreferredSize(new Dimension(70, 24));
    panel_1.add(addBtn);
}

}

This is with BorderLayout - http://snag.gy/S43C2.jpg. Also I tried with FlowLayout in panel - http://snag.gy/ndjDG.jpg

Can you help me please?

dimads
  • 77
  • 6
  • 1
    1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). 2) Provide ASCII art or a simple drawing of the layout of the GUI at minimum size, and if resizable, with more width and height. – Andrew Thompson Feb 15 '15 at 12:58
  • 1
    You've shown what you _don't_ want; please clarify what you _do_ want. – trashgod Feb 15 '15 at 14:52
  • Ok I need something like in this screen http://snag.gy/4vUkE.jpg I need surround some components by border which has distance to edge. – dimads Feb 15 '15 at 17:55
  • .. 3) See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) – Andrew Thompson Feb 16 '15 at 00:46
  • *"..by border which has distance to edge."* Add an `EmptyBorder`.. And a tip: Add @trashgod (or whoever, the `@` is important) to *notify* the person of a new comment. Oh, and still waiting on that MCVE.. – Andrew Thompson Feb 16 '15 at 00:48
  • @Andrew Thompson: Ok I understand little, thank you. – dimads Feb 16 '15 at 19:52

2 Answers2

5

The problem is that because you set the border on a panel that you add into BorderLayout.NORTH. When you resize the window, BorderLayout.NORTH section will only resize horizontally, that's why the border will not be resized correctly.

public static void main(String args[]) {
    JavaApplication11 app = new JavaApplication11();

    app.initialize();

    app.frame.pack();
    app.frame.setVisible(true);
}

private JFrame frame;
private JPanel panel;
private JTextField textField;
private JButton addBtn;

private void initialize() {
    frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    panel = new JPanel();
    frame.add(panel);

    Border border = new CompoundBorder(new EmptyBorder(5, 10, 15, 20), new LineBorder(Color.BLACK));
    panel.setBorder(border);

    textField = new JTextField(50);
    panel.add(textField);

    addBtn = new JButton("Add");
    panel.add(addBtn);
}
Crazyjavahacking
  • 9,343
  • 2
  • 31
  • 40
  • 1
    [Don't use `setPreferredSize()` when you really mean to override `getPreferredSize()`](http://stackoverflow.com/q/7229226/230513), [absolute positioning](http://docs.oracle.com/javase/tutorial/uiswing/layout/none.html), e.g. `setBounds()`, is almost never the right solution. – trashgod Feb 15 '15 at 14:47
  • Maybe this screen show what I want - http://snag.gy/4vUkE.jpg . If setPreferredSize() and others are bad solutions, so how to set size of components? – dimads Feb 15 '15 at 18:01
  • Components are always redrawn once you resize the window. That's why preferred size might be only useful at the start, but generally it's not a good idea to rely on that. – Crazyjavahacking Feb 15 '15 at 18:03
  • So what is the problem now? Do you want to have a distance from the border? Can you please sketch/draw what you want to achieve? – Crazyjavahacking Feb 15 '15 at 18:05
  • Yes I want to have a distance from the border. I just want to understand how it makes. In this screen http://snag.gy/XBMAQ.jpg - there are distances which I marked as red arrows. I want to set different sizes on those distances. I find this panel.setBorder(BorderFactory.createTitledBorder("$Title")); But how to set distance without prefferedSize or other methods. – dimads Feb 15 '15 at 21:37
  • @dimads: Your http://snag.gy/XBMAQ.jpg image shows a GUI with more than one JPanel, using more than one layout manager. – Gilbert Le Blanc Feb 15 '15 at 23:23
  • Thank all for your advices. Now I little understand what to do. – dimads Feb 16 '15 at 16:25
3

I'm assuming that this is the GUI you're trying to create.

GUI Image

To create this GUI, you need to use multiple JPanels with more than one Swing layout manager.

Here's the hierarchy of Swing components I would use.

JFrame - border layout
   JPanel - main panel, border layout
       JPanel - text, button panel, border layout, border north
           JTextField - border center
           JButton - border east
       JScrollPane - border center
           JTable
       JPanel - button panel, flow layout, border south
           JButton (3)

You get the spacing by setting an empty border on the JPanels and JScrollPane. The empty border can be as wide as you wish.

Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111
  • Can I ask 1 more ? In my screen snag.gy/XbPbi.jpg I made 3 buttons on the bottom panel with GridLayout (0 rows and 5 coloumns). I added 3 buttons there and 2 empty panels. So I have 3 buttons same size because of GridLayout. Is it possible make other buttons like "Add" which is on the other panel same size without using getPrefferedSize(), setSize(), etc methods? Thanks – dimads Feb 16 '15 at 19:55
  • @dimads: No, it's not possible to make all of your buttons in different JPanels the same size without setting the preferred size. With some layout managers, you can't change the size of the buttons at all. – Gilbert Le Blanc Feb 17 '15 at 16:21