Results 1 to 2 of 2
- 12-11-2011, 10:48 AM #1
Member
- Join Date
- Oct 2011
- Posts
- 16
- Rep Power
- 0
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); } }
-
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:
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:class GUIx extends JFrame { // ..... code deleted for brevity //!! added private DefaultComboBoxModel comboModel = new DefaultComboBoxModel(); private JComboBox combo = new JComboBox(comboModel); //!! changed
Then I would have to change the GUIx constructor a little since the combo box has already been constructed.:Java Code://!! added public void addElement(Object element) { comboModel.addElement(element); } //!! added public int getComboSize() { return comboModel.getSize(); }
Then in the main program, I'd use the methods just created:Java Code://!! combo = new JComboBox(AddArray.al.toArray()); add(combo);
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); } } });
Similar Threads
-
Combobox dynamic load using String[]
By fm3c2007 in forum New To JavaReplies: 8Last Post: 04-07-2011, 03:34 AM -
how to add Arraylist filter for a jsp page showing results from a servlet-Arraylist
By alok_sharma in forum Java ServletReplies: 7Last Post: 11-22-2010, 01:26 PM -
String array from file to ComboBox
By cselic in forum AWT / SwingReplies: 3Last Post: 05-06-2010, 05:29 PM -
Problem Showing A Big String?
By AJArmstron@aol.com in forum New To JavaReplies: 1Last Post: 05-05-2010, 02:56 AM -
[SOLVED] Adding ComboBox
By impact in forum New To JavaReplies: 2Last Post: 05-02-2008, 08:43 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks