Bottom of JScrollPane is cut off

Jonathan E

I am trying to create a simple email client and the bottom of the body is being cut-off. If I add a horizontal scroll bar, it does not appear, and the bottom of the Vertical scroll bar does not appear either.

Image

Here is my code:

   import java.awt.BorderLayout;
   import java.awt.Container;
   import java.awt.FlowLayout;
   import java.awt.Font;

   import javax.swing.JFrame;
   import javax.swing.JLabel;
   import javax.swing.JPanel;
   import javax.swing.JScrollBar;
   import javax.swing.JScrollPane;
   import javax.swing.JTextArea;
   import javax.swing.JTextField;
   import javax.swing.UIManager;


   @SuppressWarnings("serial")
   public class gui extends JFrame{

gui(String title, int x, int y){

    super(title);
    setSize(x,y);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setResizable(false);

}

public void addElements(){

    Font size30 = new Font(null, Font.PLAIN, 30);

    JPanel pnl = new JPanel();

    Container contentPane = getContentPane();

    //--- User Info ---//

    JPanel userInfo = new JPanel();

    JLabel userLabel = new JLabel("Username: ");
    JTextField userField = new JTextField(12);
    userInfo.add(userLabel);
    userInfo.add(userField);

    JLabel passLabel = new JLabel("Password: ");
    JTextField passField = new JTextField(10);
    userInfo.add(passLabel);
    userInfo.add(passField);

    JLabel serverLabel = new JLabel("Mail Server: ");
    JTextField serverField = new JTextField(10);
    userInfo.add(serverLabel);
    userInfo.add(serverField);

    JLabel portLabel = new JLabel("Server Port: ");
    JTextField portField = new JTextField(3);
    userInfo.add(portLabel);
    userInfo.add(portField);

    //--- To: CC: and Subject Fields ---//

    JPanel msgInfo = new JPanel();

    JLabel toLabel = new JLabel("To: ");
    JTextField toField = new JTextField(30);
    msgInfo.add(toLabel);
    msgInfo.add(toField);

    JLabel subLabel = new JLabel("Subject: ");
    JTextField subField = new JTextField(30);
    msgInfo.add(subLabel);
    msgInfo.add(subField);

    //--- Body ---//

    JPanel bodyPnl = new JPanel(new BorderLayout(10,10));

    JLabel bodyLabel = new JLabel("Body");
    bodyLabel.setFont(size30);

    JTextArea bodyField = new JTextArea(30,70);
    bodyField.setLineWrap(true);
    bodyField.setWrapStyleWord(true);

    JScrollPane bodyScroll = new JScrollPane(bodyField);

    bodyScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
    bodyScroll.setBounds(getX(), getY(), bodyField.getWidth(), bodyField.getHeight());

    bodyPnl.add("South",bodyScroll);

    pnl.add(userInfo);
    pnl.add(msgInfo);
    pnl.add(bodyLabel);
    pnl.add(bodyScroll);

    contentPane.add("North", pnl);

    setVisible(true);

}

}

In my main class I am just creating a new gui and then calling the addElements() function.

MadProgrammer

FlowLayout doesn't "wrap" well. Consider a different layout, GridBagLayout for example...

Also, stop "trying" to force a size onto your UI, you don't have enough control over the factors which affect sizing to do this.

Instead, rely on the layout managers and API functionality. For example, instead of calling setSize on your frame, call pack...I would have posted soon, but it took me this long to find that call...I was scratching my head wondering why the UI wouldn't layout the way I expected...

Form

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;

public class Test extends JFrame {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                Test frame = new Test("Testing", 400, 400);
            }
        });
    }

    Test(String title, int x, int y) {

        super(title);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        addElements();
        pack();
        setVisible(true);
//        setResizable(false);

    }

    public void addElements() {

        Font size30 = new Font(null, Font.PLAIN, 30);

        //--- User Info ---//
        JPanel userInfo = new JPanel(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(2, 2, 4, 2);
        gbc.gridx = 0;
        gbc.gridy = 0;
        JLabel userLabel = new JLabel("Username: ");
        JTextField userField = new JTextField(12);
        userInfo.add(userLabel, gbc);
        gbc.gridx++;
        userInfo.add(userField, gbc);

        JLabel passLabel = new JLabel("Password: ");
        JTextField passField = new JTextField(10);
        gbc.gridx++;
        userInfo.add(passLabel, gbc);
        gbc.gridx++;
        userInfo.add(passField, gbc);

        JLabel serverLabel = new JLabel("Mail Server: ");
        JTextField serverField = new JTextField(10);
        gbc.gridx++;
        userInfo.add(serverLabel, gbc);
        gbc.gridx++;
        userInfo.add(serverField, gbc);

        JLabel portLabel = new JLabel("Server Port: ");
        JTextField portField = new JTextField(3);
        gbc.gridx++;
        userInfo.add(portLabel, gbc);
        gbc.gridx++;
        userInfo.add(portField, gbc);

        gbc = new GridBagConstraints();
        gbc.insets = new Insets(2, 2, 4, 2);
        gbc.gridx = 0;
        gbc.gridy = 0;
        //--- To: CC: and Subject Fields ---//
        JPanel msgInfo = new JPanel(new GridBagLayout());

        JLabel toLabel = new JLabel("To: ");
        JTextField toField = new JTextField(30);
        msgInfo.add(toLabel, gbc);
        gbc.gridx++;
        msgInfo.add(toField, gbc);

        JLabel subLabel = new JLabel("Subject: ");
        JTextField subField = new JTextField(30);
        gbc.gridx++;
        msgInfo.add(subLabel, gbc);
        gbc.gridx++;
        msgInfo.add(subField, gbc);

        //--- Body ---//
//        JPanel bodyPnl = new JPanel(new GridBagLayout());
//        gbc = new GridBagConstraints();
//        gbc.insets = new Insets(2, 2, 4, 2);
//        gbc.gridx = 0;
//        gbc.gridy = 0;

        JLabel bodyLabel = new JLabel("Body");
        bodyLabel.setHorizontalAlignment(JLabel.CENTER);
        bodyLabel.setFont(size30);

        JTextArea bodyField = new JTextArea(30, 70);
        bodyField.setLineWrap(true);
        bodyField.setWrapStyleWord(true);

        JScrollPane bodyScroll = new JScrollPane(bodyField);

        bodyScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
//        bodyScroll.setBounds(getX(), getY(), bodyField.getWidth(), bodyField.getHeight());

        setLayout(new GridBagLayout());
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL;

        add(userInfo, gbc);
        gbc.gridy++;
        add(msgInfo, gbc);
        gbc.gridy++;
        gbc.insets = new Insets(10, 10, 4, 10);
        add(bodyLabel, gbc);
        gbc.gridy++;
        gbc.insets = new Insets(4, 10, 10, 10);
        gbc.weighty = 1;
        gbc.fill = GridBagConstraints.BOTH;
        add(bodyScroll, gbc);

    }

}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

UIWebView cut off at the bottom by UITabBarController

Android Textview text cut off at bottom

Bottom of Table View (Cell) cut off

ChartJS Line chart cut off at the top and bottom

Button aligned to bottom of RelativeLayout is cut off

Android Studio Project: bottom of UI is cut off

CSS | Dropdown at bottom of page is cut off

react native - screen cut off at bottom

Android - ExpandableListView content cut off at bottom of screen

Italic characters' bottom cut off in textBox

Android AlertDialog text is cut off at bottom

Cut off bottom of element with dynamic height

Youtube video is cut off at the bottom in swift application

Android glide: crop - cut off X pixels from image bottom

BottomSheetDialogFragment has bottom part of it cut off when created

Part of fragment inside ViewPager getting cut off at bottom of screen(Android)

Element's border bottom is cut off inside of a horizontal container

Plain text being cut off at bottom in iOS, how to fix in xCode?

React Native ScrollView is cut off from the bottom in iOS

TextView inside NestedScrollView is cut off by one bottom line

custom css select menu gets cut off at the bottom in chrome

airodump-ng info at screen bottom is cut-off

Android Compose - Why is drawText in Canvas being cut off at bottom but not at top?

Scroll JScrollPane to bottom

Android L ActionBarActivity using Feinstein SldingMenu and AppCompat v21 is cut off at bottom of the screen

UILabel subclass - text cut off in bottom despite label being correct height

React Native screen display cut off at top and bottom, and app looks zoomed in

iOS 7 - Fixed status bar, now bottom of PhoneGap app is cut off by 20px

I want a UITableView inside of a UIScrollView to extend to the bottom of the UIScrollView and not get cut off by initial screen