JTextField only shows as a slit Using GridBagLayout, need help
Asked Answered
D

4

16

Hi thank you in advance for any help, I'm trying to build a simple program to learn GUI's but when I run the code below my JTextFields all show as a slit thats not large enough for even one character.

cant post an image but it would look similar to: Label [|

where [| is what the text field actually looks like

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


import javax.swing.*;



public class lab6start implements ActionListener
{
    JTextField custNameTxt;
    JTextField acctNumTxt;
    JTextField dateCreatedTxt;
    JButton checkingBtn;
    JButton savingsBtn;
    JTextField witAmountTxt;
    JButton withDrawBtn;
    JTextField depAmountTxt;
    JButton depositBtn;

    lab6start()
    {
        JFrame bankTeller = new JFrame("Welcome to Suchnsuch Bank");
        bankTeller.setSize(500, 280);
        bankTeller.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        bankTeller.setResizable(false);
        bankTeller.setLayout(new GridBagLayout());

        bankTeller.setBackground(Color.gray);

        //bankTeller.getContentPane().add(everything, BorderLayout.CENTER);

        GridBagConstraints c = new GridBagConstraints();

        JPanel acctInfo = new JPanel(new GridBagLayout());
        c.gridx = 0;
        c.gridy = 0;
        c.gridwidth = 2;
        c.gridheight = 1;
        c.insets = new Insets(5,5,5,5);
        bankTeller.add(acctInfo, c);
        c.gridwidth = 1;

        //labels
        //name acct# balance interestRate dateCreated
        JLabel custNameLbl = new JLabel("Name");
        c.gridx = 0;
        c.gridy = 0;
        c.insets = new Insets(0,0,0,0);
        acctInfo.add(custNameLbl, c);

        custNameTxt = new JTextField("customer name",50);
        c.gridx = 1;
        c.gridy = 0;
        c.insets = new Insets(5,5,5,5);
        acctInfo.add(custNameTxt,c);
        custNameTxt.requestFocusInWindow();

        JLabel acctNumLbl = new JLabel("Account Number");
        c.gridx = 0;
        c.gridy = 1;
        c.insets = new Insets(5,5,5,5);
        acctInfo.add(acctNumLbl,c);

        acctNumTxt = new JTextField("account number");
        c.gridx = 1;
        c.gridy = 1;
        c.insets = new Insets(5,5,5,5);
        acctInfo.add(acctNumTxt,c);

        JLabel dateCreatedLbl = new JLabel("Date Created");
        c.gridx = 0;
        c.gridy = 2;
        c.insets = new Insets(5,5,5,5);
        acctInfo.add(dateCreatedLbl,c);

        dateCreatedTxt = new JTextField("date created");
        c.gridx = 1;
        c.gridy = 2;
        c.insets = new Insets(5,5,5,5);
        acctInfo.add(dateCreatedTxt,c);

        //buttons
        checkingBtn = new JButton("Checking");
        c.gridx = 0;
        c.gridy = 3;
        c.insets = new Insets(5,5,5,5);
        acctInfo.add(checkingBtn,c);

        savingsBtn = new JButton("Savings");
        c.gridx = 1;
        c.gridy = 3;
        c.insets = new Insets(5,5,5,5);
        acctInfo.add(savingsBtn,c);

//end of info panel

        JPanel withDraw = new JPanel(new GridBagLayout());
        c.gridx = 0;
        c.gridy = 1;
        c.insets = new Insets(5,5,5,5);
        bankTeller.add(withDraw, c);

        witAmountTxt = new JTextField("Amount to Withdraw:");
        c.gridx = 0;
        c.gridy = 0;
        c.insets = new Insets(5,5,5,5);
        withDraw.add(witAmountTxt,c);

        withDrawBtn = new JButton("Withdraw");
        c.gridx = 1;
        c.gridy = 0;
        c.insets = new Insets(5,5,5,5);
        withDraw.add(withDrawBtn,c);

        //add check balance

//end of withdraw panel

        JPanel deposit = new JPanel(new GridBagLayout());
        c.gridx = 1;
        c.gridy = 1;
        c.insets = new Insets(5,5,5,5);
        bankTeller.add(deposit, c);

        depAmountTxt = new JTextField("Amount to Deposit");
        c.gridx = 0;
        c.gridy = 0;
        c.insets = new Insets(5,5,5,5);
        deposit.add(depAmountTxt,c);

        depositBtn = new JButton("Deposit");
        c.gridx = 1;
        c.gridy = 0;
        c.insets = new Insets(5,5,5,5);
        deposit.add(depositBtn,c);      

        bankTeller.setVisible(true);

        // action/event 
        checkingBtn.addActionListener(this);
        savingsBtn.addActionListener(this);
        withDrawBtn.addActionListener(this);
        depositBtn.addActionListener(this);

    }

    public void actionPerformed(ActionEvent e) 
    {
        if (e.getSource()== checkingBtn)
        {
            witAmountTxt.requestFocusInWindow();
            //checking newcheck = new checking();
        }

    }
}


/*
        String accountType = null;
        accountType = JOptionPane.showInputDialog(null, "Checking or Savings?");

        if (accountType.equalsIgnoreCase("checking"))
        {
            checking c_Account = new checking();
        }
        else if (accountType.equalsIgnoreCase("savings"))
        {
        //  savings s_Account = new savings();
        }
        else
        {
            JOptionPane.showMessageDialog(null, "Invalid Selection");
        }

    */
Docilla answered 24/12, 2010 at 17:44 Comment(0)
O
10

Try calling pack() on your JFrame after adding everything and before setVisible(true)

Also, you'll not want to forget to set the GridBagConstraints weightx and weighty fields. At least give them a non-0 value such as 1.0 for most fields and 0 for fields whose size you don't want changed if the GUI changes size.

Orlandoorlanta answered 24/12, 2010 at 17:56 Comment(2)
awesome! ty! didnt know about pack() and thats exactly what is needed. i knew something was wrong in the sizing when it displayed i just didnt know how to fix it.Docilla
Setting weightx to 1 fixed it for me. Thanks!Detainer
A
13

Adding those works for me:

    c.weightx=1.;
    c.fill=GridBagConstraints.HORIZONTAL;
Almeria answered 3/9, 2012 at 18:35 Comment(1)
+1 works great, a lot more efficient than a repack() or some hackTracee
O
10

Try calling pack() on your JFrame after adding everything and before setVisible(true)

Also, you'll not want to forget to set the GridBagConstraints weightx and weighty fields. At least give them a non-0 value such as 1.0 for most fields and 0 for fields whose size you don't want changed if the GUI changes size.

Orlandoorlanta answered 24/12, 2010 at 17:56 Comment(2)
awesome! ty! didnt know about pack() and thats exactly what is needed. i knew something was wrong in the sizing when it displayed i just didnt know how to fix it.Docilla
Setting weightx to 1 fixed it for me. Thanks!Detainer
H
9

There is also a bug in Swing that could result in a JTextArea showing up as a slit, although Sun/Oracle says "its not a bug, its a feature":

https://bugs.java.com/bugdatabase/view_bug?bug_id=4247013

One potential solution that someone suggested on that thread is to set the minimum size of the JTextField... something like this:

textField.setMinimumSize(textField.getPreferredSize());
Halftone answered 16/5, 2011 at 14:18 Comment(1)
This allowed me to fix a program that I wrote years ago that broke very quickly. I'm not supposed to just say thank you, so I won't, but you get the idea....Bessie
A
1

I'm guessing from the name lab6 that you may not have used GridBagLayout before. It is one of the most difficult and feared of the Swing Layout tools. If you haven't used it I'd suggest working through tutorials such as: http://download.oracle.com/javase/tutorial/uiswing/layout/gridbag.html and building up to your example

Airily answered 24/12, 2010 at 18:0 Comment(3)
1+ for great suggestion. Also he should consider nesting JPanels each using one of the other easier to use simpler layout managers. Other options include miglayout.Orlandoorlanta
and +1 to you for the pack() and setVisible(). I would never use GBL by choice and unless the OP is required to do it for the Lab I would also go for nested JPanelsAirily
i have actually but thank you. i just tried something a little more adventurous this time.Docilla

© 2022 - 2024 — McMap. All rights reserved.