Fix Java GUI Problem: Textboxes Being Cut Off

  • Context: Java 
  • Thread starter Thread starter DarkAnt
  • Start date Start date
  • Tags Tags
    Java
Click For Summary
SUMMARY

The forum discussion addresses a Java GUI issue where textboxes are being cut off in a Swing application. The user reports that the problem occurs on Windows XP and Windows 2000 using JBuilder versions 10 and 3.5, respectively. A suggested solution is to call this.pack() before displaying the window, which adjusts the frame size to fit the components. However, the user notes that this did not resolve the issue, indicating potential platform-specific rendering problems.

PREREQUISITES
  • Understanding of Java Swing components and layout managers
  • Familiarity with JFrame and its methods
  • Knowledge of event handling in Java using ActionListener
  • Basic experience with debugging GUI issues in Java applications
NEXT STEPS
  • Research Java Swing layout managers, focusing on GridLayout and FlowLayout
  • Learn about the pack() method and its impact on component sizing
  • Explore cross-platform GUI behavior in Java applications
  • Investigate the differences between JDK versions and their effects on GUI rendering
USEFUL FOR

Java developers, particularly those working on GUI applications using Swing, and anyone troubleshooting layout issues across different operating systems.

DarkAnt
Messages
195
Reaction score
0
My program has a slight gui problem. The bottom of my gui (the textboxes) are being cut off. I have no idea why this is happening and I have very little experience with GUIs. Could someone tell me why this is happening and how to fix it/better way of doing it. Thanks

ok here is my code

Code:
package project_7_6;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

class Gui extends JFrame implements ActionListener{
  JLabel principalLabel;
  JLabel downpaymentLabel;
  JLabel monthlyPaymentLabel;
  JLabel interestLabel;
  JLabel yearsLabel;
  JTextField principalField;
  JTextField downpaymentField;
  JTextField monthlyPaymentField;
  JTextField interestField;
  JTextField yearsField;
  JTextArea monthTextArea;
  JTextArea totalBalanceOwedTextArea;
  JTextArea interestOwedTextArea;
  JTextArea principalOwedTextArea;
  JTextArea paymentTextArea;
  JTextArea amountOwedTextArea;
  JButton calculateButton;
  JScrollPane scroll1;
  JScrollPane scroll2;
  JScrollPane scroll3;
  JScrollPane scroll4;
  JScrollPane scroll5;
  JScrollPane scroll6;

  int month;
  public double ballanceOwed, amountOwed, principal, downPayment, monthlyPayment, annualInterest, interestOwed, principalOwed, payment;
  public String strPrincipal;
  public String strDownpayment;
  public String strMonthlyPayment;
  public String strInterest;
  public String strYears;
  public String strBallanceOwed;
  public String strPayment;
  public String strMonth;
  public String strAmountOwed;


  public Gui() {

    super("TidBit Computer Store Ripoffs");
    setSize(400,500);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container pane = getContentPane();
    FlowLayout flow = new FlowLayout(FlowLayout.LEFT);
    FlowLayout button = new FlowLayout(FlowLayout.CENTER);
    GridLayout grid = new GridLayout(7,2);
    pane.setLayout(grid);
    JPanel row1 = new JPanel();
    principalLabel = new JLabel("Principal: $", JLabel.RIGHT);
    principalField = new JTextField(8);
    JPanel row2 = new JPanel();
    downpaymentLabel = new JLabel("Down Payment: $", JLabel.RIGHT);
    downpaymentField = new JTextField(8);
    JPanel row3 = new JPanel();
    monthlyPaymentLabel = new JLabel("Monthly Payment:", JLabel.RIGHT);
    monthlyPaymentField = new JTextField(8);
    JPanel row4 = new JPanel();
    interestLabel = new JLabel("Interest:", JLabel.RIGHT);
    interestField = new JTextField(3);
    JPanel row6 = new JPanel();
    calculateButton = new JButton("Calculate");
    calculateButton.addActionListener(this);
    JPanel row7 = new JPanel();
    monthTextArea = new JTextArea();
    monthTextArea.setLineWrap(true);
    monthTextArea.setWrapStyleWord(true);
    monthTextArea.setRows(12);
    totalBalanceOwedTextArea = new JTextArea();
    totalBalanceOwedTextArea.setLineWrap(true);
    totalBalanceOwedTextArea.setWrapStyleWord(true);
    totalBalanceOwedTextArea.setRows(12);
    interestOwedTextArea = new JTextArea();
    interestOwedTextArea.setLineWrap(true);
    interestOwedTextArea.setWrapStyleWord(true);
    interestOwedTextArea.setRows(12);
    principalOwedTextArea = new JTextArea();
    principalOwedTextArea.setLineWrap(true);
    principalOwedTextArea.setWrapStyleWord(true);
    principalOwedTextArea.setRows(12);
    paymentTextArea = new JTextArea();
    paymentTextArea.setLineWrap(true);
    paymentTextArea.setWrapStyleWord(true);
    paymentTextArea.setRows(12);
    amountOwedTextArea = new JTextArea();
    amountOwedTextArea.setLineWrap(true);
    amountOwedTextArea.setWrapStyleWord(true);
    amountOwedTextArea.setRows(12);
    scroll1 = new JScrollPane(monthTextArea);
    scroll2 = new JScrollPane(totalBalanceOwedTextArea);
    scroll3 = new JScrollPane(interestOwedTextArea);
    scroll4 = new JScrollPane(principalOwedTextArea);
    scroll5 = new JScrollPane(paymentTextArea);
    scroll6 = new JScrollPane(amountOwedTextArea);


    row1.setLayout(flow);
    row2.setLayout(flow);
    row3.setLayout(flow);
    row4.setLayout(flow);
    row6.setLayout(button);
    row7.setLayout(button);

    row1.add(principalLabel);
    row1.add(principalField);
    row2.add(downpaymentLabel);
    row2.add(downpaymentField);
    row3.add(monthlyPaymentLabel);
    row3.add(monthlyPaymentField);
    row4.add(interestLabel);
    row4.add(interestField);
    row6.add(calculateButton);
    row7.add(scroll1);
    row7.add(scroll2);
    row7.add(scroll3);
    row7.add(scroll4);
    row7.add(scroll5);
    row7.add(scroll6);

    pane.add(row1);
    pane.add(row2);
    pane.add(row3);
    pane.add(row4);
    pane.add(row6);
    pane.add(row7);
    setContentPane(pane);
    setVisible(true);
  }


  public void actionPerformed(ActionEvent event)
  {
    String command = event.getActionCommand();
    if (command.equals("Calculate"))
    {
      strPrincipal = principalField.getText();
      strDownpayment = downpaymentField.getText();
      strMonthlyPayment = monthlyPaymentField.getText();
      strInterest = interestField.getText();

      principal = Integer.parseInt(strPrincipal);
      downPayment = Integer.parseInt(strDownpayment);
      monthlyPayment = Integer.parseInt(strMonthlyPayment);
      annualInterest = Integer.parseInt(strInterest);

      monthlyPayment = monthlyPayment * .01;
      annualInterest = annualInterest * .01;

      strMonth = "Month";
      strBallanceOwed = "Ballance Owed";
      strInterest = "Interest Owed";
      strPrincipal = "Principal Owed";
      strPayment = "Payment";
      strAmountOwed = "Amount Owed";

      monthTextArea.setText(strMonth);
      totalBalanceOwedTextArea.setText(strBallanceOwed);
      interestOwedTextArea.setText(strInterest);
      principalOwedTextArea.setText(strPrincipal);
      paymentTextArea.setText(strPayment);
      amountOwedTextArea.setText(strAmountOwed);

      amountOwed = principal - downPayment;
      principalOwed = (principal - downPayment) * monthlyPayment;
      month = 0;
      principalOwed = principalOwed * 100;
      principalOwed = Math.round(principalOwed);
      principalOwed = principalOwed / 100;

      while (amountOwed > 0)
      {
        month++;
        interestOwed = annualInterest * amountOwed / 12;
        interestOwed = interestOwed * 100;
        interestOwed = Math.round(interestOwed);
        interestOwed = interestOwed / 100;

        ballanceOwed = amountOwed;
        ballanceOwed = ballanceOwed * 100;
        ballanceOwed = Math.round(ballanceOwed);
        ballanceOwed = ballanceOwed / 100;

        payment = interestOwed + principalOwed;
        amountOwed = amountOwed - payment;
        amountOwed = amountOwed * 100;
        amountOwed = Math.round(amountOwed);
        amountOwed = amountOwed / 100;


        if(amountOwed <= 0)
        {
          principalOwed = ballanceOwed - interestOwed;
          payment = ballanceOwed;
          amountOwed = 0;
        }

        strMonth = strMonth + "\n" + month;
        monthTextArea.setText(strMonth);

        strBallanceOwed = strBallanceOwed + "\n" + ballanceOwed;
        totalBalanceOwedTextArea.setText(strBallanceOwed);

        strInterest = strInterest + "\n" + interestOwed;
        interestOwedTextArea.setText(strInterest);

        strPrincipal = strPrincipal + "\n" + principalOwed;
        principalOwedTextArea.setText(strPrincipal);

        strPayment = strPayment + "\n" + payment;
        paymentTextArea.setText(strPayment);

        strAmountOwed = strAmountOwed + "\n" + amountOwed;
        amountOwedTextArea.setText(strAmountOwed);
      }
    }
  }

}
 
Last edited:
Technology news on Phys.org
The textboxes look fine under JDK 1.4.2_04 on Windows.

Can you tell me more about your platform?

You should also call this.pack() before displaying the window.

- Warren
 
the textboxes on the bottom look fine to you.
the two computers I've used this on have had issues with them. they cut off the bottom of the textboxes so when i make a scroll bar to go with them the bottom of the scroll bar gets cut off. One computer was running windows xp with JBuilder 10 and the other was running windows 2000 with JBuilder 3.5
I added this.pack() and it didn't do anything noticable
what does this.pack() do?
 
Last edited:

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
Replies
3
Views
3K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 1 ·
Replies
1
Views
4K