Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-30-2008, 09:53 PM
Member
 
Join Date: Aug 2008
Posts: 39
hungleon88 is on a distinguished road
About JList
By Default, JList only display the value that already exist when create JList.

Now I have an empty JList, how can i add value to the Jlist by checkBox, textField ? and how can i show the value to the jList in Frame??
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-30-2008, 10:04 PM
Fubarable's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 1,289
Fubarable is on a distinguished road
You should set your JList's model to be an object of DefaultListModel. Then since you have a reference to this DefaultListModel (let's call "model") you can simply add objects to it via the addElement(Object obj) method.

You would do well to study the Sun Swing tutorials, List section where this is all explained with examples.

Good luck!
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 08-30-2008, 10:43 PM
Member
 
Join Date: Aug 2008
Posts: 39
hungleon88 is on a distinguished road
oh yes,i alredy use addElement, but i can't display the Item(value) in a jList that added
can you hint me a method to display that?, i try .show(), .list() ... but not work, forgive me, i'm so noob T_T
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 08-30-2008, 10:53 PM
Fubarable's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 1,289
Fubarable is on a distinguished road
If you add an element to the currently displayed JList's model, it will display; nothing else need be done. For instance:
Code:
import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.DefaultListModel; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JList; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; public class JListAddElement { private static DefaultListModel listmodel; // here's our DefaultListModel private static void createAndShowUI() { final JFrame frame = new JFrame("JList AddElement"); final JPanel mainPane = new JPanel(); String[] items = {"Fe", "Fi", "Fo", "Fum", "What the F*ck?!?"}; // create the list model and fill with Strings listmodel = new DefaultListModel(); for (int i = 0; i < items.length; i++) { listmodel.addElement(items[i]); } // create JList and give it our defaultlistmodel JList myJList = new JList(listmodel); // button that allows us to add an item to the list model JButton addItem = new JButton("Add Item"); addItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // call a joptionpane to get a string String text = JOptionPane.showInputDialog(frame, "Add Item to be Entered into List"); if (text != null && !text.isEmpty()) { // then add the string to the list's model here // note that nothing else needs to be done. When the model changes // the list automatically shows these changes listmodel.addElement(text); } } }); mainPane.setPreferredSize(new Dimension(400, 300)); mainPane.add(new JScrollPane(myJList)); mainPane.add(addItem); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(mainPane); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { createAndShowUI(); } }); } }

Last edited by Fubarable : 08-30-2008 at 11:04 PM. Reason: show an example
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 08-30-2008, 11:12 PM
Member
 
Join Date: Aug 2008
Posts: 39
hungleon88 is on a distinguished road
No no,i means, i create an empty JList, doesn't have any value in that.
Then, i want to get value from ComboBox, CheckBox and add to the JList.Thus, when i click on button Create , the value after get from ComboBox , CheckBox and add to JList will display on JList interface.
And the display is what i want
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 08-30-2008, 11:24 PM
Fubarable's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 1,289
Fubarable is on a distinguished road
It doesn't matter if the DefaultListModel starts out with data or not. The method for getting stuff into it is the same. If you need more help, then create an SSCCE like I did above. Please read the link. It will tell you that this program should be small, compilable and runnable, and should have no code that doesn't relate to your problem, but the code should demonstrate your problem.

And remember: Do not post your whole program as no one will read it. Instead (again) post an SSCCE. Good luck!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
JList and JPanels JetsYanks New To Java 3 05-12-2008 06:56 AM
JList problem zizou147 Advanced Java 1 04-17-2008 10:50 AM
searching within a JList newtojava7 New To Java 1 03-10-2008 02:12 AM
Help with JList Albert NetBeans 1 07-13-2007 05:42 PM
add a jlist column Alan JCreator 1 05-28-2007 06:51 AM


All times are GMT +3. The time now is 09:33 AM.


VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org