4 Replies - 372 Views - Last Post: 09 May 2011 - 10:51 AM Rate Topic: -----

#1 Amb154  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 29-April 11

QUESTION=How do I make a button that add or removes textfields/labels?

Posted 03 May 2011 - 11:03 AM

I posted a little while ago with a program I came up with that i needed help with, and it seems I'll be needing it again. heres the program:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;


public class GUI extends 
JFrame
{
    private static final int sizeOne = 500;
    private static final int sizeTwo = 1000;


    
    
    private JLabel Onum, Tnum, Thnum, Fnum, Finum, Snum, Senum, Enum, Nnum, Tenum, Amount , AnswerO;
    private JTextField OnumT, TnumT, ThnumT, FnumT, FinumT, SnumT, SenumT, EnumT, NnumT, TenumT, AmountT, AnswerT;
    private JButton calculateB, exitB, addB;
    
    private CalculateButtonHandler cbHandler;
    private ExitButtonHandler ebHandler;
    private AddButtonHandler aHandler;
    
    public GUI()
    {
        Onum = new JLabel ("Enter Number One: ", SwingConstants.RIGHT);
        Tnum = new JLabel("Enter Number Two: " ,SwingConstants.RIGHT);    
        Thnum = new JLabel("Enter Number Three: " ,SwingConstants.RIGHT);
        Fnum = new JLabel("Enter Number Four: " ,SwingConstants.RIGHT);
        Finum = new JLabel("Enter Number Five: " ,SwingConstants.RIGHT);
        Snum = new JLabel("Enter Number Six: " ,SwingConstants.RIGHT);
        Senum = new JLabel("Enter Number Seven: " ,SwingConstants.RIGHT);
        Enum = new JLabel("Enter Number Eight: " ,SwingConstants.RIGHT);
        Nnum = new JLabel("Enter Number Nine: " ,SwingConstants.RIGHT);
        Tenum = new JLabel("Enter Number Ten: " ,SwingConstants.RIGHT);
        AnswerO = new JLabel("Average: " ,SwingConstants.RIGHT);
        Amount = new JLabel("How many numbers did you enter?" ,SwingConstants.RIGHT);
    
        OnumT = new JTextField("0");
        TnumT = new JTextField("0");
        ThnumT = new JTextField("0");
        FnumT = new JTextField("0");
        FinumT = new JTextField("0");
        SnumT = new JTextField("0");
        SenumT = new JTextField("0");
        EnumT = new JTextField("0");
        NnumT = new JTextField("0");
        TenumT = new JTextField("0");
        AnswerT = new JTextField("0");
        AmountT = new JTextField("0");
    
        calculateB = new JButton("Calculate");
        cbHandler = new CalculateButtonHandler();
        calculateB.addActionListener(cbHandler);
        exitB = new JButton("Exit");
        ebHandler = new ExitButtonHandler();
        exitB.addActionListener(ebHandler);
        addB = new JButton("Add Another TextField.");
        
        
        
        
        
        setTitle("AverageFinder V0.1 Made by Adrian Bates.");
        Container pane = getContentPane();
        pane.setLayout(new GridLayout(16, 8));
        pane.add(Onum);
        pane.add(OnumT);
        pane.add(Tnum);
        pane.add(TnumT);
        pane.add(Thnum);
        pane.add(ThnumT);
        pane.add(Fnum);
        pane.add(FnumT);
        pane.add(Finum);
        pane.add(FinumT);
        pane.add(Snum);
        pane.add(SnumT);
        pane.add(Senum);
        pane.add(SenumT);
        pane.add(Enum);
        pane.add(EnumT);
        pane.add(Nnum);
        pane.add(NnumT);
        pane.add(Tenum);
        pane.add(TenumT);
        pane.add(Amount);
        pane.add(AmountT);
        pane.add(AnswerO);
        pane.add(AnswerT);
        pane.add(calculateB);
        pane.add(exitB);
        pane.add(addB);
        
        setSize(sizeOne, sizeTwo);
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    
    }

    private class
            CalculateButtonHandler implements
            ActionListener
    {
        public void
                actionPerformed(ActionEvent e)
        {
           
            double Num = Double.parseDouble(OnumT.getText());
            double Numt = Double.parseDouble(TnumT.getText());        
            double Numth = Double.parseDouble(ThnumT.getText()); 
            double Numf = Double.parseDouble(FnumT.getText()); 
            double Numfi = Double.parseDouble(FinumT.getText()); 
            double Nums = Double.parseDouble(SnumT.getText()); 
            double Numse = Double.parseDouble(SenumT.getText()); 
            double Nume = Double.parseDouble(EnumT.getText()); 
            double Numn = Double.parseDouble(NnumT.getText()); 
            double Numte = Double.parseDouble(TenumT.getText()); 
            double Amo = Double.parseDouble(AmountT.getText());
            double Ans = (Num + Numt + Numth + Numf + Numfi + Nums + Numse + Nume + Numn + Numte) / Amo;
                    
                    AnswerT.setText("" + Ans);
                    
       }
    }

    public class ExitButtonHandler
    implements ActionListener
    {
        public void
                actionPerformed(ActionEvent e)
        {
            System.exit(0);
        }
}
    public static void main(String[] args)
    {
        GUI AverageFinder = new GUI();
        
    }
    private class
            AddButtonHandler implements
            ActionListener
    {
        public void
                actionPerformed(ActionEvent e)
        {
        }
        
    }
}


However, I'm not sure how to actually ADD the action of adding another column to the textfields and labels.
Help anyone?

Is This A Good Question/Topic? 0
  • +

Replies To: QUESTION=How do I make a button that add or removes textfields/labels?

#2 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 293
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: QUESTION=How do I make a button that add or removes textfields/labels?

Posted 03 May 2011 - 11:14 AM

use the .addActionListener(ActionListener e) method or .addButtonListener(ActionListener e). Those are the only two I see you using so that should work for you. It's better to just add all the labels you will eventually use then .setVisibility(false) until a button is clicked. Dynamically adding components after the GUI is created is poor practice
Was This Post Helpful? 0
  • +
  • -

#3 Amb154  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 29-April 11

Re: QUESTION=How do I make a button that add or removes textfields/labels?

Posted 09 May 2011 - 07:33 AM

I could do that, but isn't it kind of bad to have to create 25+ Textfields/Jlabels, or is that just normal?
Was This Post Helpful? 0
  • +
  • -

#4 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 293
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: QUESTION=How do I make a button that add or removes textfields/labels?

Posted 09 May 2011 - 10:22 AM

No, it's not really normal. You could have one textfield and a jlabel. Set up an action listener to store the text entered into array[i] then increment the label's number by i + 1. Only do this while i < 10, then calculate the average and display it in another textfield
Was This Post Helpful? 0
  • +
  • -

#5 Amb154  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 29-April 11

Re: QUESTION=How do I make a button that add or removes textfields/labels?

Posted 09 May 2011 - 10:51 AM

That sounds like a great idea. Thanks for the help! I'll post again if there's any problems.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1