How to stop TextArea from "snapping" to a different size while using GridBagLayout

Orion

I'm trying to learn about GridBagLayout and I've gotten my TextArea components to behave just the way I want except for when the window is re-sized vertically.

When the window is re-sized to be shorter the bottom TextArea snaps to a larger size instead of keeping its current size and shrinking based on weighty when it runs out of room.

I feel like it's something simple like changing the fill or something but I can't figure it out..

Code:

public class Window {

    Frame frame;
    Panel panel;
    TextArea top;
    TextArea bottom;
    GridBagConstraints topC, bottomC;

    Window(){
        createWindow();
    }


    public static void main(String[] args){
        new Window();
    }


    private void createWindow(){
        //create panel for text areas
        panel = new Panel(new GridBagLayout());

        //create top text area
        top = new TextArea();
        top.setPreferredSize(new Dimension(500,500));

        //create bottom text area
        bottom = new TextArea();
        bottom.setPreferredSize(new Dimension(500,75));

        //set constraints for top text area
        topC = new GridBagConstraints();
        topC.anchor = GridBagConstraints.NORTH;
        topC.fill = GridBagConstraints.BOTH;
        topC.gridheight = GridBagConstraints.RELATIVE;
        topC.gridwidth = GridBagConstraints.REMAINDER;
        topC.gridx = 0;
        topC.gridy = 0;
        topC.insets = new Insets(2,2,2,2);
        topC.weightx = 1.0;
        topC.weighty = 1.0;

        //set constraints for bottom text area
        bottomC = new GridBagConstraints();
        bottomC.anchor = GridBagConstraints.SOUTH;
        bottomC.fill = GridBagConstraints.BOTH;
        bottomC.gridheight = GridBagConstraints.REMAINDER;
        bottomC.gridwidth = GridBagConstraints.REMAINDER;
        bottomC.gridx = 0;
        bottomC.gridy = 1;
        bottomC.insets = new Insets(2,2,2,2);
        bottomC.weightx = 1.0;
        bottomC.weighty = 0.5;

        //add text areas to the panel
        panel.add(top, topC);
        panel.add(bottom, bottomC);

        //create frame
        frame = new JFrame();
        frame.setTitle("Client Console");
        frame.addWindowListener(new WindowAdapter(){
            @Override
            public void windowClosing(WindowEvent e){
                frame.dispose();
            }
        });
        frame.add(panel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

    }

}
MadProgrammer

Put the text areas in a scroll pane.

You might also like to take a look at Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?.

And do you really need to use AWT? It's kind of out of date (like 15 years out of date). If you can consider using Swing or JavaFX instead...

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How to stop running script by using different script

분류에서Dev

How to place a Rectangle in a new image with different size, while preserving the size and quality of the orignial Rectangle?

분류에서Dev

Is it possible to make an array with dynamic memory using while loop without asking the size of memory from user?

분류에서Dev

How to stop user from login?

분류에서Dev

How to Retrieve Data from Two Different MySQL Database using JDBC?

분류에서Dev

Javascript elements only snapping to grid from right side

분류에서Dev

Why resampling for pandas different while using cumsum?

분류에서Dev

How to stop class from being redefined when using multiple files in Visual Studio

분류에서Dev

How do I stop KeePass from automatch-ing all pages, when using the tag/title "Google"

분류에서Dev

How do i stop a while loop after a certain time in C

분류에서Dev

How can to stop the "ding" sound while pressing enter

분류에서Dev

How to stop tail -f except using kill

분류에서Dev

Angular - how to stop using hash links to navigate?

분류에서Dev

In R, how to generate document term-doucment matrix-like data frame from a list of vectors of different size?

분류에서Dev

Ignore changing size of "df -h" while using diff

분류에서Dev

How to stop a remote desktop session from ending

분류에서Dev

How to stop table from adjusting width

분류에서Dev

Start/stop a while loop?

분류에서Dev

How to trim or strip white spaces from a String while using Robot Framework

분류에서Dev

Stop Python Script from Writing to File after it reaches a certain size in linux

분류에서Dev

How is Ubuntu different from Debian?

분류에서Dev

How is bc different from dc?

분류에서Dev

GridBagLayout을 사용하여 TextArea 아래에 JButton 설정

분류에서Dev

How can I prevent the source file is different from when the module was built when using Resharper

분류에서Dev

How can I change plane size with different screen resolution?

분류에서Dev

Different layout for different screen size

분류에서Dev

Python while loop does not stop

분류에서Dev

How to move marker in googlemap while getting different latitude,longitude in android

분류에서Dev

how to stop selecting reapeted option for two label using jsp or php?

Related 관련 기사

  1. 1

    How to stop running script by using different script

  2. 2

    How to place a Rectangle in a new image with different size, while preserving the size and quality of the orignial Rectangle?

  3. 3

    Is it possible to make an array with dynamic memory using while loop without asking the size of memory from user?

  4. 4

    How to stop user from login?

  5. 5

    How to Retrieve Data from Two Different MySQL Database using JDBC?

  6. 6

    Javascript elements only snapping to grid from right side

  7. 7

    Why resampling for pandas different while using cumsum?

  8. 8

    How to stop class from being redefined when using multiple files in Visual Studio

  9. 9

    How do I stop KeePass from automatch-ing all pages, when using the tag/title "Google"

  10. 10

    How do i stop a while loop after a certain time in C

  11. 11

    How can to stop the "ding" sound while pressing enter

  12. 12

    How to stop tail -f except using kill

  13. 13

    Angular - how to stop using hash links to navigate?

  14. 14

    In R, how to generate document term-doucment matrix-like data frame from a list of vectors of different size?

  15. 15

    Ignore changing size of "df -h" while using diff

  16. 16

    How to stop a remote desktop session from ending

  17. 17

    How to stop table from adjusting width

  18. 18

    Start/stop a while loop?

  19. 19

    How to trim or strip white spaces from a String while using Robot Framework

  20. 20

    Stop Python Script from Writing to File after it reaches a certain size in linux

  21. 21

    How is Ubuntu different from Debian?

  22. 22

    How is bc different from dc?

  23. 23

    GridBagLayout을 사용하여 TextArea 아래에 JButton 설정

  24. 24

    How can I prevent the source file is different from when the module was built when using Resharper

  25. 25

    How can I change plane size with different screen resolution?

  26. 26

    Different layout for different screen size

  27. 27

    Python while loop does not stop

  28. 28

    How to move marker in googlemap while getting different latitude,longitude in android

  29. 29

    how to stop selecting reapeted option for two label using jsp or php?

뜨겁다태그

보관