Results 1 to 3 of 3
Like Tree3Likes
  • 1 Post By DarrylBurke
  • 2 Post By Dex

Thread: JTextField size - gridbaglayout

  1. #1
    Dex
    Dex is offline Member
    Join Date
    Mar 2012
    Posts
    29
    Rep Power
    0

    Default JTextField size - gridbaglayout

    How would i set the size of my JtextField so that it fits the width of my JFrame?

    So far I have tried:

    - changing the number columns when creating my JtextField, but this also sometimes changes the size of my Jframe
    - setPreferedSize(new Dimension(x,y)) , but I'm not sure how to get the exact width of my frame
    - a.gridwidth = GridBagConstraints.REMAINDER , doesn't seem to help me?

    Thankyou

    Java Code:
        a.insets = new Insets(2,2,2,2); 
        a.gridwidth = GridBagConstraints.REMAINDER;
        field = new JTextField("");
        field.setBackground(Color.lightGray);
        field.setOpaque(true);
        field.setHorizontalAlignment(JTextField.RIGHT);
       
        field.setEditable(false);
        
        a.gridx = 0; 
        a.gridy = 0;
        a.gridwidth = GridBagConstraints.REMAINDER;
        panel.add(field,a);
    full Code

    Java Code:
    import java.util.Stack;
    import java.io.*;  
    import javax.swing.*; 
    import java.util.Scanner;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.Component.*;
    
    public class Calculator2 {
      
      private JTextField field;
      private JButton bttn0;
      private JButton bttn1;
      private JButton bttn2;
      private JButton bttn3;
      private JButton bttn4;
      private JButton bttn5;
      private JButton bttn6;
      private JButton bttn7;
      private JButton bttn8;
      private JButton bttn9;
      private JButton btn1;
      private JButton btn2;
      private JButton btn3;
      private JButton btn4;
      private JButton btn5;
      private JButton equals;
      private int numb1;
      private int numb2;
      private char operator;
      private int total;
      
      Calc2Handler handler = new Calc2Handler();
      String str = "";
      
      public Calculator2(){
        total = 0;
      }
      
      
      public Integer calculate(String input){
        
        
        for(int i = 0; i < input.length(); i++){
          
          if(isOp((input.charAt(i))) == false){
            
            if( i == 0 ){
              numb1 = Character.getNumericValue(input.charAt(i));
            }else if(i == 2){
              numb2 = Character.getNumericValue(input.charAt(i));
            }
          }
          else if(isOp((input.charAt(i))) == true){
            operator = input.charAt(i);
            
          }
          
        }
        
        switch(operator){
          case '+':
            total = (Integer)(numb1 + numb2);
            break;
          case '-':
            total = numb1 - numb2;
            break;
          case '/':
            total = numb1 / numb2;
            break;
          case '*':
            total = numb1 * numb2;
            break;
          default:
            total = 100;
            
        }
        System.out.println(total);
        return total;
      }
      
      public boolean isOp(char c){
        
        switch(c){
          case '+':
            return true;
          case '-':
            return true;
          case '/':
            return true;
          case '*':
            return true;
          default:
            return false;
            
            
        }
        
      }
      
      public void Interface(){
        
        //frame 
        
        JFrame frame = new JFrame("Calc 2.0");
        
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        JMenuBar menuBar = new JMenuBar();
        JMenu menu = new JMenu("File");
        menuBar.add(menu);
        frame.setJMenuBar(menuBar);
        
        JMenu menu2 = new JMenu("Exit");
        menuBar.add(menu2);
        
        JPanel panel = new JPanel(new GridBagLayout());
        frame.getContentPane().add(panel, BorderLayout.NORTH);
        GridBagConstraints a = new GridBagConstraints();
        a.anchor = GridBagConstraints.NORTHWEST;
        
        
        //////////////////////////// THISS ( JTEXTFIELD )
        a.insets = new Insets(2,2,2,2); 
        a.gridwidth = GridBagConstraints.REMAINDER;
        field = new JTextField("",15);
        field.setBackground(Color.lightGray);
        field.setOpaque(true);
        field.setHorizontalAlignment(JTextField.RIGHT);
       
        field.setEditable(false);
        
        a.gridx = 0; 
        a.gridy = 0;
        a.gridwidth = GridBagConstraints.REMAINDER;
        panel.add(field,a);
        ///////////////////////////////////////////////////
        
        
        a.weightx = 0.5; 
        a.fill = GridBagConstraints.BOTH;
        a.gridwidth = 1;
        bttn7 = new JButton("7");
        a.gridx = 0;
        a.gridy = 1;
        
        panel.add(bttn7,a);
        
        bttn8 = new JButton("8");
        a.gridx = 1;
        a.gridy = 1;
        panel.add(bttn8,a);
        
        bttn9 = new JButton("9");
        a.gridx = 2;
        a.gridy = 1;
        panel.add(bttn9,a);
        
        bttn4 = new JButton("4");
        a.gridx = 0;
        a.gridy = 2;
        panel.add(bttn4,a);
        
        bttn5 = new JButton("5");
        a.gridx = 1;
        a.gridy = 2;
        panel.add(bttn5,a); 
        
        bttn6 = new JButton("6");
        a.gridx = 2;
        a.gridy = 2;
        panel.add(bttn6,a);
        
        bttn1 = new JButton("1");
        a.gridx = 0;
        a.gridy = 3;
        panel.add(bttn1,a);
        
        bttn2 = new JButton("2");
        a.gridx = 1;
        a.gridy = 3;
        panel.add(bttn2,a);
        
        bttn3 = new JButton("3");
        a.gridx = 2;
        a.gridy = 3;
        panel.add(bttn3,a);
        
        bttn0 = new JButton("0");
        a.gridx = 0;
        a.gridy = 4;
        a.gridwidth = GridBagConstraints.RELATIVE;
        bttn0.setPreferredSize(new Dimension(92,26));
        panel.add(bttn0,a);
        
        a.gridwidth = GridBagConstraints.REMAINDER;
        btn1 = new JButton("+");
        a.gridx = 3;
        a.gridy = 4;
        panel.add(btn1,a);
        
        a.gridwidth = 1;
        btn2 = new JButton("/");
        a.gridx = 3;
        a.gridy = 1;
        panel.add(btn2,a);
        
        btn3 = new JButton("*");
        a.gridx = 3;
        a.gridy = 2;
        panel.add(btn3,a);
        
        btn4 = new JButton("-");
        a.gridx = 3;
        a.gridy = 3;
        panel.add(btn4,a);
        
        btn5 = new JButton(".");
        a.gridx = 3;
        a.gridy = 5;
        a.gridwidth = GridBagConstraints.REMAINDER;
        panel.add(btn5,a);
        
        a.gridwidth = GridBagConstraints.RELATIVE;
        equals = new JButton("=");
        a.gridx = 0;
        a.gridy = 5;
        panel.add(equals,a);
        
        frame.pack();
        frame.setVisible(true);
        frame.setResizable(false);
        
        // adding action listeners to buttons
        
        bttn0.addActionListener(handler);
        bttn1.addActionListener(handler);
        bttn2.addActionListener(handler);
        bttn3.addActionListener(handler);
        bttn4.addActionListener(handler);
        bttn5.addActionListener(handler);
        bttn6.addActionListener(handler);
        bttn7.addActionListener(handler);
        bttn8.addActionListener(handler);
        bttn9.addActionListener(handler);
        btn1.addActionListener(handler);
        btn2.addActionListener(handler);
        btn3.addActionListener(handler);
        btn4.addActionListener(handler);
        btn5.addActionListener(handler);
        equals.addActionListener(handler);
        field.addActionListener(handler);
      }
      
      
    // MAIN  
      
      public static void main(String[] args){
        Calculator2 calc = new Calculator2();
        calc.Interface();
        
      }
      
    // ACTIONS PERFORMED
      private class Calc2Handler implements ActionListener{
        public void actionPerformed(ActionEvent e) { 
          
          
          if(e.getSource() == equals){
            System.out.println(str);
            field.setText(str);
            calculate(str);
          }
          else if(e.getSource() == bttn0){
            str+= "0";
          }else if (e.getSource() == bttn1){
            str+= "1";
          } else if (e.getSource() == bttn2){
            str+= "2";
          } else if (e.getSource() == bttn3){
            str+= "3";
          } else if (e.getSource() == bttn4){
            str+= "4";
          } else if (e.getSource() == bttn5){
            str+= "5";
          } else if (e.getSource() == bttn6){
            str+= "6";
          } else if (e.getSource() == bttn7){
            str+= "7";
          } else if (e.getSource() == bttn8){
            str+= "8";
          } else if (e.getSource() == bttn9){
            str+= "9";
          } else if (e.getSource() == btn1){
            str+= "+";
          } else if (e.getSource() == btn2){
            str+= "/";
          } else if (e.getSource() == btn3){
            str+= "*";
          } else if (e.getSource() == btn4){
            str+= "-";
          } else if (e.getSource() == equals){
            
          }
        }
      }
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,305
    Rep Power
    20

    Default Re: JTextField size - gridbaglayout

    Have you so quickly forgotten Fubarable's advice in your last thread?

    Also, have you ever read this webpage? -> SSCCE (Short, Self Contained, Correct (Compilable), Example)

    db
    Fubarable likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Dex
    Dex is offline Member
    Join Date
    Mar 2012
    Posts
    29
    Rep Power
    0

    Default Re: JTextField size - gridbaglayout

    Quote Originally Posted by DarrylBurke View Post
    Have you so quickly forgotten Fubarable's advice in your last thread?

    Also, have you ever read this webpage? -> SSCCE (Short, Self Contained, Correct (Compilable), Example)

    db
    Just re - looked at my code and realised that I added my TextField to the panel before I set the weight and fill by mistake (I was very tired).
    And yes I do remember what he said :) and i'll check out that webpage now


    Thanks db and Fubarable
    Last edited by Dex; 12-31-2012 at 05:08 AM.
    DarrylBurke and Fubarable like this.

Similar Threads

  1. Replies: 8
    Last Post: 11-29-2011, 02:07 AM
  2. Replies: 1
    Last Post: 07-19-2010, 12:31 AM
  3. JTextField not showing properly in GridBagLayout
    By aserothbw in forum AWT / Swing
    Replies: 13
    Last Post: 10-02-2009, 11:11 AM
  4. Applet JTextField Size Problem
    By a13w in forum New To Java
    Replies: 4
    Last Post: 09-19-2009, 07:52 AM
  5. gridbaglayout: increase/decrease size of components.
    By newtojava7 in forum New To Java
    Replies: 2
    Last Post: 01-28-2008, 07:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •