0

I am trying to place a JTextArea inside of JFrame using GridBagLayout. I cannot resize the text area nor is it being placed according to my gridx and gridy coordinates. Having a bit of trouble figuring out whats missing or what I am doing wrong.

I am trying to build an ATM with a GUI interface. The text area will be my screen. Kinda new to GUI's so any help would be greatly appreciated.

ATM.java

Represents an automated teller machine

    import java.awt.Color;
    import javax.swing.JFrame;
    import java.awt.FlowLayout;
    import java.awt.BorderLayout;
    import java.awt.GridBagLayout;
    import java.awt.GridBagConstraints;
    import java.awt.Component;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.JLabel;
    import javax.swing.JTextArea;
    import javax.swing.JButton;
    import java.awt.Container;

    //import java.awt.BorderLayout;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    public class ATM  extends JFrame 
    {  
        private JFrame atmContainer;
        private GridBagConstraints constraints;
        private GridBagLayout layout;
        private JTextArea atmScreen;
        private Container container;
        // constants corresponding to main menu options
        /*private static final int BALANCE_INQUIRY = 1;
        private static final int WITHDRAWAL = 2;
        private static final int DEPOSIT = 3;
        private static final int EXIT = 4;*/

        // no-argument ATM constructor initializes instance variables
        public ATM() 
        {
          atmContainer = new JFrame("ATM");
          atmScreen = new JTextArea(5,15);
          atmContainer.setLayout(new GridBagLayout());
          constraints = new GridBagConstraints();
          atmContainer.setSize(600, 400);
          atmContainer.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //completely closes the      program
       
       
          atmScreen.setText("Welcome user");
          atmScreen.setEnabled(false);
          // atmScreen.setSize(100,100);
          constraints.gridx = 1;
          constraints.gridy = 0;
          atmContainer.add(atmScreen, constraints);
        
          atmContainer.setVisible(true); //makes atm visible
          atmContainer.setLocationRelativeTo(null);
       } 
   }
Community
  • 1
  • 1
qb1234
  • 155
  • 2
  • 14
  • 1
    you will save yourself a world of hurt by not using GridBagLayout. – MeBigFatGuy Nov 22 '14 at 16:57
  • 3
    `The jTextArea will be my screen.` - then just use the BorderLayout, which is the default layout manager for the frame. Then just add the text area to the frame and it will take up all the available space of the frame. – camickr Nov 22 '14 at 16:58
  • The reason why is because you do not understand how Layout Managers work. The best thing to do is study what Layout Managers are and how they work by reading the [Java Layout Manager Tutorial](https://docs.oracle.com/javase/tutorial/uiswing/layout/index.html), and then choose the appropriate layout manager(s) for what you need to do. After that, if you think no layout manager is appropriate for what you have to do, you have to choices: 1) Create your own Layout Manager, or 2) Use [Absolute Positioning](https://docs.oracle.com/javase/tutorial/uiswing/layout/none.html). – hfontanez Nov 22 '14 at 19:47
  • 1
    @hfontanez Absolute Positioning is strongly disencouraged. There is always a better LayoutManager than doing it manually. In some very rare cases, a custom LayoutManager may be implemented. – Guillaume Polet Nov 22 '14 at 20:13
  • @GuillaumePolet there is a reason why I listed it as the last alternative. However, I am not sure I subscribe to the idea that the use of absolute positioning is **strongly** discouraged. Absolute positioning gives you more control to place components EXACTLY where you want them. But I know there are drawbacks to this, just like there are drawbacks to Layout Managers. The best thing is to be informed of ALL supported methodologies and choose the best one. – hfontanez Nov 22 '14 at 20:18
  • @hfontanez You can achieve the same effect as absolute positioning with a custom LayoutManager. The advantage of doing it that way is that you keep the layout logic properly separated from the rest of the code. So yes, you can perfectyly live without absolute positioning and only using LayoutManager – Guillaume Polet Nov 22 '14 at 20:59
  • @GuillaumePolet did you read my whole comment or are you arguing for the sake of arguing. Answer something for me? What was my FIRST recommendation if no suitable layout manager can be found? – hfontanez Nov 22 '14 at 21:04
  • @hfontanez *"However, I am not sure I subscribe to the idea that the use of absolute positioning is **strongly** discouraged"* I sure would, and so would most developers who appear in the first page of [this list](http://stackoverflow.com/tags/swing/topusers).. I disagree with it to the extent where I'd never mention it and give any answer that does, a -1. – Andrew Thompson Nov 23 '14 at 00:35
  • @AndrewThompson everyone is entitled to their own opinions. Apparently you know everyone's opinion here in SO. However, I go by the official documentation provided in the Oracle Java website. Yes, in there is states that Layout Managers are a better option, but they don't **strongly** discourage their use. Second, I added that as a last option for a reason. For the record, I use Layout Managers all the time, and I do encourage others to use them as well. But, I recognize that there might be cases where Absolute Positioning might be the way to go. – hfontanez Nov 23 '14 at 00:44
  • I'm having trouble understanding what your desired behavior actually is. You've set a size for the JFrame (`atmContainer.setSize(600, 400)`) and you've set a size for the JTextArea (`new JTextArea(5,15)`). GridBagLayout is dealing with this in a way that seems reasonable to me. – Radiodef Nov 23 '14 at 00:49
  • @hfontanez *"official documentation provided in the Oracle Java website."* The tutorial is not *always* the best advice. However it devotes an entire trail to layouts, and ..one paragraph (from memory) to absolute layouts. They might have added another **two** paragraphs to describe the down-side of such nonsense, but that would have swamped the entry. – Andrew Thompson Nov 23 '14 at 00:51
  • 1
    Provide ASCII art, or a simple drawing, of the layout of the GUI. – Andrew Thompson Nov 23 '14 at 00:51
  • Furthermore, it would be helpful if you explained why you're trying to use GridBagLayout in the first place. (Are there actually more components than the JTextArea?) – Radiodef Nov 23 '14 at 00:55
  • @hfontanez I did read your entire comment and I actually pointed who easy it is to circumvent absolute positioning by using a custom LayoutManager. Only a LayoutManager should call setBounds/setSize/setPosition and implement getPreferredSize(), even if the positioning is based on asbolute values provided by any kinf of model. This allows a clean separation of the components and their layout. So my point remains this: don't use absolute positioning, ever. – Guillaume Polet Nov 23 '14 at 19:06

1 Answers1

-3

Seeing you are new to GUIs, I advise utilizing a GUI builder; they make designing a GUI considerably easier and more productive. This question names a few options:

Best GUI designer for eclipse?

Also, is there a reason that you are using the GridBagLayout? For beginners, it may be a difficult to understand and to use. As pointed out by camickr, using the BorderLayout will do the job just fine.

From my personal experience, it was easier for me to first learn about the FlowLayout. It is the simplest of all layouts. You should gain a good understanding by experimenting with it a little.

The Java Tutorials are rich of lessons as well; this is a visual overview of possible layout managers:

https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html#gridbag

And this demonstrates how to use a FlowLayout:

https://docs.oracle.com/javase/tutorial/uiswing/layout/flow.html

Community
  • 1
  • 1
univise
  • 509
  • 2
  • 11
  • 1
    _Seeing you are new to GUIs, I advise utilizing a GUI builder_ Worst advice ever. Best way to create ugliest code (and UI) and not learn how GUI actually work. – Guillaume Polet Nov 22 '14 at 20:10