Search code examples
javaswingjtextfieldgrid-layout

can JTextField resizable in a gridlayout?


Below is my code for testing BorderLayout and GridLayout, but I found that the JTextField in the JPanel using GridLayout is not resizable, no matter how many times I change the JTextField.setPreferredSize, it still remains the same.

public class Boderlayout implements ActionListener {
    JButton bCalculate;
    JLabel lPrinAmount,lInterestRate,lPayPerYear,lResult;
    JTextField tPrinAmount,tInterestRate,tPayPerYear,tResult;
    
    public static void main (String[] args) {
        new Boderlayout();
    }

    public Boderlayout() {
        JFrame frame = new JFrame();
        frame.setLayout(new BorderLayout());
        
        JPanel p2 = new JPanel();
        p2.setBackground(Color.BLUE);
        p2.setPreferredSize(new Dimension(600,100));
        frame.add(p2,BorderLayout.SOUTH);
        
        bCalculate = new JButton("Calculate");
        bCalculate.setPreferredSize(new Dimension(550,85));
        p2.add(bCalculate);
        bCalculate.addActionListener(this);
        
        JPanel p3 = new JPanel();
        p3.setBackground(Color.GREEN);
        p3.setLayout(new GridLayout(6,1));
        p3.setPreferredSize(new Dimension(300,300));
        frame.add(p3,BorderLayout.CENTER);
            
        lPrinAmount = new JLabel("Principle Amount: ");
        lPrinAmount.setPreferredSize(new Dimension(200,40));
        p3.add(lPrinAmount);
            
        tPrinAmount = new JTextField(20);
        tPrinAmount.setPreferredSize(new Dimension(170,40));
        p3.add(tPrinAmount);
            
        tPrinAmount.addActionListener(this);
            
        lInterestRate = new JLabel("Interest Rate: ");
        lInterestRate.setPreferredSize(new Dimension(200,40));
        p3.add(lInterestRate);
            
        tInterestRate = new JTextField(20);
        tInterestRate.setPreferredSize(new Dimension(100,40));
        p3.add(tInterestRate);
            
        tInterestRate.addActionListener(this);
            
        lPayPerYear = new JLabel("Payment Period (Year): ");
        lPayPerYear.setPreferredSize(new Dimension(200,40));
        p3.add(lPayPerYear);
            
        tPayPerYear = new JTextField(20);
        tPayPerYear.setPreferredSize(new Dimension(170,40));
        p3.add(tPayPerYear);
            
        tPayPerYear.addActionListener(this);
        
        JPanel p5 = new JPanel();
        p5.setLayout(new GridLayout(2,1));
        p5.setBackground(Color.WHITE);
        p5.setPreferredSize(new Dimension(300,300));
        frame.add(p5,BorderLayout.EAST);
        
        lResult = new JLabel("Result");
        lResult.setPreferredSize(new Dimension(170,40));
        p5.add(lResult);
            
        tResult = new JTextField(50);
        tResult.setPreferredSize(new Dimension(170,200));
        tResult.setEditable(false);
        p5.add(tResult);
        
        frame.pack();
        frame.setVisible(true);
    }
    
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        if (e.getSource() == bCalculate) {
            double prinAmount = Double.parseDouble(tPrinAmount.getText());
            double interestRate = Integer.parseInt(tInterestRate.getText());
            double paymentYear = Integer.parseInt(tPayPerYear.getText());
            
            double interestRatePercentage = interestRate / 100;
            double loanInterest = prinAmount * interestRatePercentage;
            double year = 12 * paymentYear;
            double mortgageResult = (loanInterest * (Math.pow((1 + (interestRatePercentage / 12)), year)) / (Math.pow((1 + (interestRatePercentage) / 12), year) - 1)) / 12;
            
            String stringmortgageResult = Double.toString(mortgageResult);
            String newline = System.getProperty("line.separator");
            String finalResult = "Principle Amount: " + tPrinAmount.getText() + newline + "Interest Rate: " + tInterestRate.getText() + "%" + newline + "Payment Period: " + tPayPerYear.getText()
            + newline + "Mortgage: " + stringmortgageResult;
            tResult.setText(finalResult);
        }
    }
}

Solution

  • Refer to How to Use GridLayout.
    Here is a quote from that Web page:

    A GridLayout object places components in a grid of cells. Each component takes all the available space within its cell, and each cell is exactly the same size.

    In other words, GridLayout does not consider the preferred size of the components it contains.

    A layout manager that does consider its contained components' preferred size is GridBagLayout.

    BorderLayout only respects part of its contained components' preferred size. For the EAST component, BorderLayout uses its preferred width but not its preferred height. BorderLayout will initially use its CENTER component's preferred width and height.

    This is the window I get when I run your code.

    OP code screen capture

    Here is your modified code using GridBagLayout instead of GridLayout.

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.BorderFactory;
    import javax.swing.BoxLayout;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    
    public class Boderlayout implements ActionListener {
        JButton bCalculate;
        JLabel lPrinAmount, lInterestRate, lPayPerYear, lResult;
        JTextArea tResult;
        JTextField tPrinAmount, tInterestRate, tPayPerYear;
    
        public static void main(String[] args) {
            new Boderlayout();
        }
    
        public Boderlayout() {
            JFrame frame = new JFrame();
    
            JPanel p2 = new JPanel();
            p2.setBackground(Color.BLUE);
            p2.setPreferredSize(new Dimension(600, 100));
            frame.add(p2, BorderLayout.SOUTH);
    
            bCalculate = new JButton("Calculate");
            bCalculate.setPreferredSize(new Dimension(550, 85));
            p2.add(bCalculate);
            bCalculate.addActionListener(this);
    
            JPanel p3 = new JPanel();
            p3.setBackground(Color.GREEN);
            p3.setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            p3.setPreferredSize(new Dimension(300, 300));
            frame.add(p3, BorderLayout.CENTER);
    
            lPrinAmount = new JLabel("Principle Amount: ");
            lPrinAmount.setPreferredSize(new Dimension(200, 40));
            gbc.gridx = 0;
            gbc.gridy = 0;
            p3.add(lPrinAmount, gbc);
    
            tPrinAmount = new JTextField(20);
            tPrinAmount.setPreferredSize(new Dimension(170, 40));
            gbc.gridy = 1;
            p3.add(tPrinAmount, gbc);
    
            tPrinAmount.addActionListener(this);
    
            lInterestRate = new JLabel("Interest Rate: ");
            lInterestRate.setPreferredSize(new Dimension(200, 40));
            gbc.gridy = 2;
            p3.add(lInterestRate, gbc);
    
            tInterestRate = new JTextField(20);
            tInterestRate.setPreferredSize(new Dimension(100, 40));
            gbc.gridy = 3;
            p3.add(tInterestRate, gbc);
    
            tInterestRate.addActionListener(this);
    
            lPayPerYear = new JLabel("Payment Period (Year): ");
            lPayPerYear.setPreferredSize(new Dimension(200, 40));
            gbc.gridy = 4;
            p3.add(lPayPerYear, gbc);
    
            tPayPerYear = new JTextField(20);
            tPayPerYear.setPreferredSize(new Dimension(170, 40));
            gbc.gridy = 5;
            p3.add(tPayPerYear, gbc);
    
            tPayPerYear.addActionListener(this);
    
            JPanel p5 = new JPanel();
            p5.setLayout(new BoxLayout(p5, BoxLayout.PAGE_AXIS));
            p5.setBackground(Color.WHITE);
            p5.setPreferredSize(new Dimension(300, 300));
            frame.add(p5, BorderLayout.EAST);
    
            lResult = new JLabel("Result");
            lResult.setPreferredSize(new Dimension(170, 40));
            p5.add(lResult);
    
            tResult = new JTextArea();
            tResult.setPreferredSize(new Dimension(170, 200));
            tResult.setEditable(false);
            tResult.setBorder(BorderFactory.createLineBorder(Color.darkGray));
            p5.add(tResult);
    
            frame.pack();
            frame.setVisible(true);
        }
    
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == bCalculate) {
                double prinAmount = Double.parseDouble(tPrinAmount.getText());
                double interestRate = Integer.parseInt(tInterestRate.getText());
                double paymentYear = Integer.parseInt(tPayPerYear.getText());
    
                double interestRatePercentage = interestRate / 100;
                double loanInterest = prinAmount * interestRatePercentage;
                double year = 12 * paymentYear;
                double mortgageResult = (loanInterest
                        * (Math.pow((1 + (interestRatePercentage / 12)), year))
                        / (Math.pow((1 + (interestRatePercentage) / 12), year) - 1)) / 12;
    
                String stringmortgageResult = Double.toString(mortgageResult);
                String newline = System.getProperty("line.separator");
                String finalResult = "Principle Amount: " + tPrinAmount.getText() + newline
                        + "Interest Rate: " + tInterestRate.getText() + "%" + newline
                        + "Payment Period: " + tPayPerYear.getText() + newline + "Mortgage: "
                        + stringmortgageResult;
                tResult.setText(finalResult);
            }
        }
    }
    

    This is the window I get when I run the above code.

    enter image description here

    Note that I changed tResult to JTextArea rather than JTextField.