Results 1 to 2 of 2
Like Tree1Likes
  • 1 Post By Fubarable

Thread: Adding String to ArrayList from textfield and showing it on ComboBox

  1. #1
    Renxx is offline Member
    Join Date
    Oct 2011
    Posts
    16
    Rep Power
    0

    Default Adding String to ArrayList from textfield and showing it on ComboBox

    How can i make so that when i add String to ArrayList from textfield, it adds it automaticlyto JComboBox as one of the options which to choose.

    Java Code:
    package Leiutis;
    
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
    import java.util.ArrayList;
    
    
    import javax.swing.*;
    
    public class AddArray {
    
       static ArrayList<String> al = new ArrayList<String>();
       static int count =-1;
       static String sum ="";
       
       public static void main(String[] args) {
          final GUIx s = new GUIx();
          s.setVisible(true);
          s.getButton().addActionListener(new ActionListener() {
             
             public void actionPerformed(ActionEvent e) {
              al.add(s.getTField().getText());
              sum += s.getTField().getText()+" ";
             }
             
          });
          s.getButtonx().addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                if(al.size()<1)
                   s.getLabel().setText("Nimekiri tühi");
                else {
                      s.getLabel().setText(sum) ;
                   
                }
             
             }
             
          });
          s.getCombo().addItemListener(new ItemListener() {
    
             @Override
             public void itemStateChanged(ItemEvent e) {
                // TODO Auto-generated method stub
                
             }
             
          });
       }
       
    
    }
    
    class GUIx extends JFrame {
          
       private FlowLayout layout;
       private Button button,buttonx;
       private Label label;
       private TextField tfield;
       private JComboBox combo;
       
       public Button getButtonx() {
          return buttonx;
       }
       
       public JComboBox getCombo() {
          return combo;
       }
       
       public Button getButton() {
          return button;
       }
       public TextField getTField() {
          return tfield;
       }
       public Label getLabel() {
          return label;
       }
       
          
       
       GUIx() {
          super("thetitle");
          setSize(300,300);
          layout = new FlowLayout();
          getContentPane().setBackground(new Color(255,0,0));
          setLayout(layout);
          setDefaultCloseOperation(EXIT_ON_CLOSE);
       
          button = new Button("Add");
          button.setBackground(new Color(0,255,0));
          add(button);
          
          buttonx = new Button("Display");
          buttonx.setBackground(new Color(0,200,0));
          add(buttonx);
          
          tfield = new TextField(10);
          tfield.setFont(new Font("Serif",Font.BOLD,14));
          add(tfield);
          
          label = new Label();
          add(label);
          
          combo = new JComboBox(AddArray.al.toArray());
          add(combo);
          
          
       }
       
       
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Adding String to ArrayList from textfield and showing it on ComboBox

    I wouldn't use an ArrayList but rather to keep it simple, use a DefaultComboBoxModel like so:

    Java Code:
    class GUIx extends JFrame {
       // ..... code deleted for brevity
    
       //!! added
       private DefaultComboBoxModel comboModel = new DefaultComboBoxModel();
       private JComboBox combo = new JComboBox(comboModel); //!! changed
    Then I'd give GUIx two public methods, one to add and element to the comboModel and one to get the comboModel's size:
    Java Code:
       //!! added
       public void addElement(Object element) {
          comboModel.addElement(element);
       }
       
       //!! added
       public int getComboSize() {
          return comboModel.getSize();
       }
    Then I would have to change the GUIx constructor a little since the combo box has already been constructed.:
    Java Code:
          //!! combo = new JComboBox(AddArray.al.toArray());
          add(combo);
    Then in the main program, I'd use the methods just created:
    Java Code:
          s.getButton().addActionListener(new ActionListener() {
    
             public void actionPerformed(ActionEvent e) {
                //!!
                String text = s.getTField().getText(); //!!
                s.addElement(text); //!!
                
                //!! al.add(s.getTField().getText());
                sum += s.getTField().getText() + " ";
             }
    
          });
          s.getButtonx().addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                //!! if (al.size() < 1)
                if (s.getComboSize() < 1) //!!
                   s.getLabel().setText("Nimekiri tühi");
                else {
                   s.getLabel().setText(sum);
    
                }
    
             }
    
          });
    DarrylBurke likes this.

Similar Threads

  1. Combobox dynamic load using String[]
    By fm3c2007 in forum New To Java
    Replies: 8
    Last Post: 04-07-2011, 04:34 AM
  2. Replies: 7
    Last Post: 11-22-2010, 02:26 PM
  3. String array from file to ComboBox
    By cselic in forum AWT / Swing
    Replies: 3
    Last Post: 05-06-2010, 06:29 PM
  4. Problem Showing A Big String?
    By AJArmstron@aol.com in forum New To Java
    Replies: 1
    Last Post: 05-05-2010, 03:56 AM
  5. [SOLVED] Adding ComboBox
    By impact in forum New To Java
    Replies: 2
    Last Post: 05-02-2008, 09:43 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
  •