0

So I've started my own little project were I am to build a simple text editor mainly using JPanel components. For now I've set my sizes like this:

tfMain.setPreferredSize(new Dimension(550,650));

It works, but it doesn't scale very well. If I resize the JFrame all my components get's moved around, trying to fit. And my tfMain stays the same as I have set it to 550x650. So, to be clear; When you start the program, my tfMain should be 550x650. If I make the window smaller, it should become smaller, and If I make the window bigger it should get bigger.

To as the problem that the buttons are moving around I think has to do with my Layout. Right Now I'm using BorderLayout, but I think that Box or Grid might be better.

Edit: I seemed to solve the problem with the components flying around with the help of GridLayout. It did however add some padding, so I'm not sure what's up with that. Here is my code so far:

public class Viewer extends JPanel {
    private Controller controller;
    private JPanel jpEverything = new JPanel();
    private JTextArea taMain = new JTextArea();

    public Viewer(Controller controller, ButtonPanel buttonPanel){
        this.controller = controller;
        jpEverything.setLayout(new BoxLayout(jpEverything,BoxLayout.Y_AXIS));
        setPreferredSize(new Dimension(500,600));
        taMain.setPreferredSize(new Dimension(400,500));
        jpEverything.add(buttonPanel);
        jpEverything.add(taMain);
        add(jpEverything);

    }
}

This is how it looks: enter image description here

Essej
  • 892
  • 6
  • 18
  • 33
  • 1
    *" I am to build a simple text editor mainly using JPanel components."* Don't forget the text components for... the text editor. (1) See [this](http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi). (2) Use an appropriate layout manager to handle resizing. – user1803551 Dec 18 '15 at 15:01
  • 1
    And post the relevant part of your code with exact specifications on how it should behave under resizing. Be sure we can compile it. – user1803551 Dec 18 '15 at 15:02
  • 1
    I would suggest you should be using a BorderLayout as the main layout. Then you create a panel using a FlowLayout and add your buttons to the panel. Then add the panel to the frame using the `BorderLayout.PAGE_START` constraint. Then you add your JTextArea to a JScrollPane and add the scroll pane to the frame using `BorderLayout.CENTER`. Then as the frame is resized the text area will get the extra space. – camickr Dec 18 '15 at 15:39
  • We can't compile it. Read on [MCVE](http://stackoverflow.com/help/mcve). – user1803551 Dec 19 '15 at 07:20

0 Answers0