Results 1 to 5 of 5
Like Tree3Likes
  • 3 Post By mine0926

Thread: Need help for a jcombobox

  1. #1
    Alexis is offline Member
    Join Date
    Nov 2010
    Posts
    17
    Rep Power
    0

    Default Need help for a jcombobox

    Hello , i need a jcombobox witch gets values from a file and when i put a new value automatically update file with new value .

    Here is my code so far
    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Cursor;
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.awt.event.WindowListener;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.ObjectInput;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutput;
    import java.io.ObjectOutputStream;
    import java.net.URL;
    
    import javax.swing.Box;
    import javax.swing.BoxLayout;
    import javax.swing.ComboBoxModel;
    import javax.swing.JComboBox;
    import javax.swing.JEditorPane;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextField;
    import javax.swing.event.HyperlinkEvent;
    import javax.swing.event.HyperlinkListener;
    
    public class MemComboBoxDemo extends JFrame 
    {
    	protected MemComboBox urlComboBox = new MemComboBox();
    	public MemComboBoxDemo() 
    	{
    		super();
    		setSize(300, 100);
    		getContentPane().setLayout(new BorderLayout());
    
    		JPanel p = new JPanel();
    		p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
    		p.add(new JLabel("Address"));
    
    		urlComboBox.load("addresses.dat");
    		ComboBoxListener lst = new ComboBoxListener();
    		urlComboBox.addActionListener(lst);
    
    		MemComboAgent agent = new MemComboAgent(urlComboBox);
    
    		p.add(urlComboBox);
    		getContentPane().add(p, BorderLayout.NORTH);
    
    		WindowListener wndCloser = new WindowAdapter() 
    		{
    			public void windowClosing(WindowEvent e) 
    			{
    				urlComboBox.save("addresses.dat");
    				System.exit(0);
    			}
    		};
        
    		addWindowListener(wndCloser);
    
    		setVisible(true);
    		urlComboBox.grabFocus();
    	}
    
    	class ComboBoxListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent evt) 
    		{
    			System.out.println( urlComboBox.getSelectedItem());
    		}
    	}
    
    	public static void main(String argv[]) 
    	{
    		new MemComboBoxDemo();
    	}
    }
    
    class MemComboAgent extends KeyAdapter 
    {
    	protected JComboBox comboBox;
    
    	protected JTextField editor;
    
    	public MemComboAgent(JComboBox c) 
    	{
    		comboBox = c;
    		editor = (JTextField) c.getEditor().getEditorComponent();
    		editor.addKeyListener(this);
    	}
    
    	public void keyReleased(KeyEvent e) 
    	{
    		char ch = e.getKeyChar();
    		if (ch == KeyEvent.CHAR_UNDEFINED || Character.isISOControl(ch))
    			return;
    		int pos = editor.getCaretPosition();
    		String str = editor.getText();
    		if (str.length() == 0)
    			return;
    
    		for (int k = 0; k < comboBox.getItemCount(); k++) 
    		{
    			String item = comboBox.getItemAt(k).toString();
    			if (item.startsWith(str)) 
    			{
    				editor.setText(item);
    				editor.setCaretPosition(item.length());
    				editor.moveCaretPosition(pos);
    				break;
    			}
    		}
    	}
    }
    
    class MemComboBox extends JComboBox 
    {
    	public static final int MAX_MEM_LEN = 30;
    
    	public MemComboBox() 
    	{
    		super();
    		setEditable(true);
    	}
    
    	public void add(String item) 
    	{
    		removeItem(item);
    		insertItemAt(item, 0);
    		setSelectedItem(item);
    		if (getItemCount() > MAX_MEM_LEN)
    			removeItemAt(getItemCount() - 1);
    	}
    
    	public void load(String fName) 
    	{
    		try 
    		{
    			if (getItemCount() > 0)
    			removeAllItems();
    			File f = new File(fName);
    			if (!f.exists())
    				return;
    			FileInputStream fStream = new FileInputStream(f);
    			ObjectInput stream = new ObjectInputStream(fStream);
    
    			Object obj = stream.readObject();
    			if (obj instanceof ComboBoxModel)
    				setModel((ComboBoxModel) obj);
    
    			stream.close();
    			fStream.close();
    		} 
    		catch (Exception e) 
    		{
    			System.err.println("Serialization error: " + e.toString());
    		}
    	}
    
    	public void save(String fName) 
    	{
    		try 
    		{
    			FileOutputStream fStream = new FileOutputStream(fName);
    			ObjectOutput stream = new ObjectOutputStream(fStream);
    
    			stream.writeObject(getModel());
    
    			stream.flush();
    			stream.close();
    			fStream.close();
    		}
    		catch (Exception e) 
    		{
    			System.err.println("Serialization error: " + e.toString());
    		}
    	}
    }
    But displays only the last value

  2. #2
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default Re: Need help for a jcombobox

    Can you explain a bit what your classes do, I cannot find the event that adds your input.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default Re: Need help for a jcombobox

    Can you please the specific code segment where you add items to JComboBox?

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default Re: Need help for a jcombobox

    However, just looking at the following code segment, what was the requirement to remove the item and added at 0?

    Java Code:
    public void add(String item) 
    	{
    		removeItem(item);
    		insertItemAt(item, 0);
    		setSelectedItem(item);
    		if (getItemCount() > MAX_MEM_LEN)
    			removeItemAt(getItemCount() - 1);
    	}

  5. #5
    Alexis is offline Member
    Join Date
    Nov 2010
    Posts
    17
    Rep Power
    0

    Default Re: Need help for a jcombobox

    I am not sure . I am new in programming . The code above it's from various sites

  6. #6
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default Re: Need help for a jcombobox

    I suggest that you do your own code, it is hard to modify someones code if we dont understand it. Write down what you need to do, and try to code it. When you are stuck somewhere on your code then go back here and ask specifically.
    Fubarable, DarrylBurke and Eranga like this.

Similar Threads

  1. JComboBox
    By Java_25 in forum New To Java
    Replies: 10
    Last Post: 04-20-2011, 06:39 PM
  2. Need help with JCOMBOBOX
    By Slavon in forum AWT / Swing
    Replies: 1
    Last Post: 03-31-2011, 09:17 AM
  3. Replies: 2
    Last Post: 12-10-2009, 01:58 PM
  4. JComboBox
    By Fosters in forum AWT / Swing
    Replies: 0
    Last Post: 08-10-2008, 01:22 PM
  5. jcombobox
    By Freddie in forum AWT / Swing
    Replies: 4
    Last Post: 05-11-2007, 12:48 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
  •