Results 1 to 4 of 4
  1. #1
    freshoreo is offline Member
    Join Date
    Aug 2008
    Posts
    2
    Rep Power
    0

    Default Problem With Text Fields!

    I know that there a a few problems with this program, but the problem I want to fix first is that when I run this app, my text fields are not displayed. Instead, a very VERY small text field (less than 1 column) is displayed and I cannot type anything in. What am I doing wrong?

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.*;
    import java.text.*;
    import java.lang.String;
    
    public class ModifiedFutureValueApp
    {
        public static void main(String[] args)
        {
            JFrame frame = new FutureValueFrame();
            frame.setVisible(true);
        }
    }
    
    class FutureValueFrame extends JFrame
    {
        public FutureValueFrame()
        {
            setTitle("Future Value Calculator");
            centerWindow(this);
            setSize(267, 200);
            setResizable(false);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JPanel panel = new FutureValuePanel();
            this.add(panel);
        }
    
        private void centerWindow(Window w)
        {
            Toolkit tk = Toolkit.getDefaultToolkit();
            Dimension d = tk.getScreenSize();
            setLocation((d.width-w.getWidth())/2, (d.height-w.getHeight())/2);
        }
    }
    
    class FutureValuePanel extends JPanel implements ActionListener
    {
        private JTextField  paymentTextField,
                            rateTextField;
    	 private JTextArea   futureValueTextArea;
    	 private JScrollPane scrollPane;
        private JLabel      paymentLabel,
                            rateLabel,
                            yearsLabel,
                            futureValueLabel;
        private JButton     calculateButton,
                            exitButton;
    	 private JComboBox   yearsComboBox;
    
        public FutureValuePanel()
        {
            setLayout(new GridBagLayout());
    
            // payment label
            paymentLabel = new JLabel("Monthly Payment:");
            add(paymentLabel, getConstraints(0,0,1,1, GridBagConstraints.EAST));
    
            // payment text field
            paymentTextField = new JTextField(15);
    		  paymentTextField.setColumns(15);
            add(paymentTextField, getConstraints(1,0,1,1, GridBagConstraints.WEST));
    
            // rate label
            rateLabel = new JLabel("Yearly Interest Rate:");
            add(rateLabel, getConstraints(0,1,1,1, GridBagConstraints.EAST));
    
            // rate text field
            rateTextField = new JTextField(15);
    		  rateTextField.setColumns(15);
            add(rateTextField, getConstraints(1,1,1,1, GridBagConstraints.WEST));
    
            // years label
    		  yearsLabel = new JLabel("Number of Years:");
            add(yearsLabel, getConstraints(0,2,1,1, GridBagConstraints.EAST));
    		  
    		  //years combo box
    		  String[] years = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
    		  "11", "12", "13", "14", "15", "16", "17", "18", "19", "20" };
            yearsComboBox = new JComboBox(years);
    		  yearsComboBox.setSelectedIndex(0);
    		  yearsComboBox.addActionListener(this);
            add(yearsComboBox, getConstraints(1,2,1,1, GridBagConstraints.WEST));
    
    
            // future value label
            futureValueLabel = new JLabel("Future Value:");
            add(futureValueLabel, getConstraints(0,3,1,1, GridBagConstraints.EAST));
    
            // future value text field
    		  JTextArea futureValueTextArea = new JTextArea(5, 15);
    		  JScrollPane scrollPane = new JScrollPane(futureValueTextArea);
    		  futureValueTextArea.setEditable(false);
    		  futureValueTextArea.setColumns(15);
            add(scrollPane, getConstraints(1,3,1,1, GridBagConstraints.WEST));
    
            // calculate button
            calculateButton = new JButton("Calculate");
            calculateButton.addActionListener(this);
            add(calculateButton, getConstraints(1,4,1,1, GridBagConstraints.CENTER));
    
            // exit button
            exitButton = new JButton("Exit");
            exitButton.addActionListener(this);
            add(exitButton, getConstraints(2,4,1,1, GridBagConstraints.WEST));
    		  
        }
    
        public void actionPerformed(ActionEvent e)
        {
            Object source = e.getSource();
            if (source == exitButton)
                System.exit(0);
            else if (source == calculateButton)
            {
                double payment = Double.parseDouble(paymentTextField.getText());
                double rate = Double.parseDouble(rateTextField.getText());
    				int years = yearsComboBox.getSelectedIndex();
                double futureValue = FinancialCalculations.calculateFutureValue(
                    payment, rate, years);
                NumberFormat currency = NumberFormat.getCurrencyInstance();
                futureValueTextArea.setText(currency.format(futureValue));
            }
        }
    	 private GridBagConstraints getConstraints(int gridx, int gridy, int gridwidth
    	 , int gridheight, int anchor)
    	 {
    	 	GridBagConstraints c = new GridBagConstraints();
    		c.insets = new Insets(5, 5, 5, 5);
    		c.ipadx = 0;
    		c.ipady = 0;
    		c.gridx = gridx;
    		c.gridy = gridy;
    		c.gridwidth = gridwidth;
    		c.gridheight = gridheight;
    		c.anchor = anchor;
    		return c;
    	 }
    }
    Last edited by freshoreo; 08-04-2008 at 06:03 PM. Reason: Hit wrong button, forgot to enter code.

  2. #2
    newmember is offline Member
    Join Date
    Dec 2007
    Posts
    8
    Rep Power
    0

    Default

    i face the same problem, i dont know the reason.

    but i prefer this
    rateTextField.setPrefferedSize( new Dimension(100,20)) ;

    and for centering the form ,

    setLocationRelativeto( null ) ; after setting the size of the form
    Last edited by newmember; 08-04-2008 at 07:50 PM.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default

    I changed the setSize() to 400,400 and got the components to display. Then you need to adjust it and the constraints to what you want.

    Also the this.add() should be getContentPane().add()

  4. #4
    freshoreo is offline Member
    Join Date
    Aug 2008
    Posts
    2
    Rep Power
    0

    Default

    Wow, I knew it had to be something simple like that! Thank you so much!

Similar Threads

  1. Demonstration of text fields in SWT
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-25-2008, 02:20 PM
  2. Text Area problem
    By mcal in forum New To Java
    Replies: 0
    Last Post: 02-11-2008, 09:42 PM
  3. Show Text Fields on combo Box selected value
    By smajidali26 in forum AWT / Swing
    Replies: 0
    Last Post: 11-29-2007, 09:28 AM
  4. Help with text fields in Java
    By romina in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:29 AM
  5. Special characters in text fields
    By Felissa in forum Web Frameworks
    Replies: 0
    Last Post: 06-27-2007, 04:47 PM

Posting Permissions

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