0

How would I add gridbag layout to my code in order for output box to span the length of gui within the border parameters provided in the following code? I have two other classes that go along with this work fine. When the GUI populates it all works except the output text box doesn't span the length of gui so that the text in text box is cut off and I want to fix this, but I don't know how to do it as I never used gridbaglayout.

Below is the image showing how the GUI is supposed to look:

ATM GUI

The code in question is given below:

    public ATMGui() {
        checkingAcc = new Account(1000);
        savingAcc = new Account(2000);
        currentSelect = new Account(0);
        atmFrame = new JFrame("Automated Teller Machine");
        output = new JTextField();
        panel = new JPanel();

I believe that the problem may be because I didn't completely declare the size of the output box so it only spans one grid so-to-speak whereas I want it to span both. Can someone help me decide whether I declare the grid differently in the code above or if it goes below? Also, I've read about GridBagLayout and other methods but my problem is that I'm not sure how/where to implement it here.

        atmWithdraw = new JButton("Withdraw");
        atmDeposit = new JButton("Deposit");
        transfer = new JButton("Transfer to");
        balance = new JButton("Balance");
        atmWithdraw.addActionListener(this);

        panel.setLayout(new GridLayout(4, 2));
        panel.setBorder(BorderFactory.createEmptyBorder(15, 15, 0, 15));
        panel.add(atmWithdraw, 0);
        panel.add(atmDeposit, 1);
        panel.add(transfer, 2);
        panel.add(balance, 3);
        panel.add(checking, 4);
        panel.add(savings, 5);
        panel.add(output, BorderLayout.LINE_END);
        atmFrame.add(panel);

        atmFrame.setSize(300, 175);
        atmFrame.setVisible(true);

2 Answers2

2

i have never used gridbaglayout

Start by reading the section from the Swing tutorial on How to Use GridBagLayout for working examples.

The working example shows how to have a button span the entire width of the frame. You need to play with the "grid width" constraint. The tutorial explain how all the constraints are used.

The other option is to nest layout managers. So maybe you use the standard BorderLayout of the frame. Then you create a panel and add some components to the panel using your GridBagLayout and then add the panel to the CENTER. Then you can add your "output box" to the BorderLayout.PAGE_END. By default the component will fill the horizontal space.

The point is read the tutorials and learn the basics of each layout manager. Then use the appropriate combination of layout managers to get the job done.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • I have it up and I'm reading the problem is I am self taught mostly with online college that doesnt help at all, so reading the how to is a tad confusing but im trying to understand the concepts so that is all makes sense – 3monkeys1gorilla Apr 13 '16 at 19:27
  • `I am self taught` Me too, that is why I use the tutorial for working examples to understand the basics. Download the demo code and test it. Learn by example. If there is a specific question about the tutorial code then you ask it, but we guess what you may or may not understand about the working code. – camickr Apr 13 '16 at 20:12
  • so i have been messing around with the c.fill and what not and I've gotten it to run but the closest I've gotten was the output pane virtually disappeared and was next to the savings radio button. so i suppose at this point i understand the concept, I'm just not sure how to apply it to my specific problem set, if that makes sense? – 3monkeys1gorilla Apr 13 '16 at 20:34
  • `if that makes sense?` - Not really. I really can't figure out what you are trying to do. Your posted code doesn't even use a GridBagLayout. It was also suggested that you don't need to use a GridBagLayout. You use whatever layout manager is appropriate. What you need to do is `START OVER`. You start by creating a frame with the components only. The rest of your code is completely irrelevant to the stated problem. The code doesn't compile because we don't have access to the Account class. It is not needed for the layout anyway. The ActionListener code is not needed. – camickr Apr 13 '16 at 21:31
  • `so i have been messing around with the c.fill` - that doesn't help us. We can't see the code you are using. Post a proper [SSCCE](http://sscce.org/) that shows what you have done with just the components on the frame and your layout code. Then maybe someone will be able to help you. Again the easiest way to have a component fill the screen width is to just add it to the PAGE_START or PAGE_END. Learn the basics of the BorderLayout first before moving on to other layout managers that are more complicated. – camickr Apr 13 '16 at 21:32
  • I don't believe i need to start over, but I understand that I didn't provide my other classes to see what the issue is, I will edit my original question to show the other two classes so you can see the problem. – 3monkeys1gorilla Apr 13 '16 at 21:36
  • WE are NOT interested in your other classes, they are irrelevant to the problem. We are NOT here to debug your code. We only want to see a simplified version of the problem. We don't have time to look at hundreds of lines of code. The `SSCCE` you create should be bout 30-40 lines of code a couple of line to create the frame. A few more to create your component and add them to the frame. And then a few more for the layout constraints. The entire point of the SSCCE is to simplify the problem if you want people to help you. So you need to make the effort to simplify the code!!! – camickr Apr 13 '16 at 21:39
  • This question has an example of a proper SSCCE: http://stackoverflow.com/questions/36603866/change-text-visibility-of-buttons-according-to-the-frame-size/36608162#36608162. The code is small and simple to understand. Your code won't be much different except for your layout constraints. – camickr Apr 13 '16 at 21:42
1

My advice is not to use GridBagLayout but rather something a bit easier to use (but just as powerful) like TableLayout (http://www.oracle.com/technetwork/java/tablelayout-141489.html). To use include download the library and include in classpath or use the following maven include: -

<dependency>
    <groupId>info.clearthought</groupId>
    <artifactId>table-layout</artifactId>
    <version>4.3.0</version>
</dependency>

(See https://github.com/nerro/table-layout for more details)

Once included, then you can define your table layout as a simple 2 dimensional array of numbers e.g.

import info.clearthought.layout.TableLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

public class Test {

    public static void main(String [] args) {

        // Create JPanel using TableLayout as layout manager

        double PREF = TableLayout.PREFERRED;
        double BORDER = 10;
        double size[][] = { { BORDER, 0.50, 10, 0.50, 10 }, { 10, PREF, 10, PREF, 10, PREF, 10, PREF, 10 } };

        JPanel panel = new JPanel(new TableLayout(size));

        panel.add(new JButton("Withdraw"), "1,1");
        panel.add(new JButton("Deposit"), "3,1");
        panel.add(new JButton("Transfer to"), "1,3");
        panel.add(new JButton("Balance"), "3,3");

        panel.add(new JRadioButton("Checking"), "1,5");
        panel.add(new JRadioButton("Savings"), "3,5");

        panel.add(new JTextField(), "1,7,3,7");

        // Add to JFrame and Display

        JFrame frame = new JFrame("ATM Machine");
        frame.setSize(300, 200);
        frame.setContentPane(panel);
        frame.setVisible(true);
    }
}

The previous code produces the following: -

enter image description here

NOTE that the layout is defined as follows: -

double size[][] = { { WIDTH1, WIDTH2, ...  }, { HEIGHT1, HEIGHT2, ...  } };`

The 2 column widths are specified as 50% or 0.50. All real numbers in the range [0.0..1.0) represents percentages e.g. see the BORDER variable. The constant FILL is allocated 100% of the scalable space and PREFERRED fills to the preferred size of the component. This allows the window to resize nicely.

To add then to this frame it's miles easier than GridBagLayout i.e. simply the cell X/Y positions e.g.

panel.add(new JButton("Withdraw"), "1,1");
panel.add(new JButton("Deposit"), "3,1");

We can span several columns as follows: -

panel.add(new JTextField(), "1,7,3,7");

This spans the JTextField from column 1 to column 3 in row 7.

For more advanced uses see: -

http://www.clearthought.info/sun/products/jfc/tsc/articles/tablelayout/Simple.html

bobmarksie
  • 3,282
  • 1
  • 41
  • 54
  • I've read the tutorial, but i dont believe this will help, however i will experiment with this, just not on this project, im simply trying to find a way to make the output box on the initial gui pop up extend in the frame so the output wont be cut off when running the functions ofthis code thank you for your input though – 3monkeys1gorilla Apr 13 '16 at 21:25
  • Can you include a screenshot of what you are trying to achieve (I tried to run your code but it didn't work due to missing classes). There has yet to be a GUI screen in Java which I cannot layout using TableLaoyout – bobmarksie Apr 14 '16 at 08:48
  • Updated answer to show how TableLayout would implement this – bobmarksie Apr 26 '16 at 15:42