CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15
  1. #1
    Join Date
    Feb 2009
    Posts
    7

    how to get data from my jList (which is populated from oracle database) to textfields

    bonjour,
    i've been stuck on this for a couple of days.

    i'm trying to get the data from my jlist, (which is populated from an oracle database through a defaultlistmodel) and get it into my 7 jtextfields when i click on each item... i'm not sure how to go about doing this.

    here's my jlist
    Code:
    public void loadList(){
    		String ll;
    		String toAdd;
    			
    		try
    		{
    			Statement stmt = conn.createStatement();
    			ll = "select * from Packages";
    			ResultSet rs =  stmt.executeQuery(ll);
    			
    			//while the ResultSet has a next(){
    			while(rs.next()){
    			//get all the bits of data out of the ResultSet row
    			toAdd = rs.getString("PKGNAME") + rs.getString("PACKAGEID") + 
    			rs.getString("PKGSTARTDATE") + rs.getString("PKGENDDATE") + 
    			rs.getString("PKGDESC") + rs.getString("PKGBASEPRICE") +
    			rs.getString("PKGAGENCYCOMMISSION");
    			//add() them to the DefaultListModel
    			dlm.addElement(toAdd);
    			}
    		}
    			
    		 catch (SQLException e2) {
    			// TODO Auto-generated catch block
    			e2.printStackTrace();
    		}

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: how to get data from my jList (which is populated from oracle database) to textfi

    If you want to get a selected item out of the JList when the selection changes, add a ListSelectionListener to the JList, and get the item index from the ListSelection event. You can then get the item at that index from the ListModel.

    Simplicity is the soul of efficiency...
    A. Freeman
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  3. #3
    Join Date
    Feb 2009
    Posts
    7

    Re: how to get data from my jList (which is populated from oracle database) to textfi

    still kinda lost..
    am i on the right path with this?

    Code:
    jListPList.addListSelectionListener(new ListSelectionListener()
    		{
    			
    			public void valueChanged(ListSelectionEvent e)
    			{
    				if(e.getValueIsAdjusting() == false)
    				{
    					if(jListPList.getSelectedIndex() == -1)
    					{
    						//no selection, disable edit button
    						jButtonEdit.setEnabled(false);
    					}
    					else
    					{
    						//selection, enable edit button
    						jButtonEdit.setEnabled(true);
    						
    						
    					}
    				}
    			}   
    		});
    i'm thinking what i need will go in the else statement after the edit button is enabled.. but i'm not sure how to code it

  4. #4
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: how to get data from my jList (which is populated from oracle database) to textfi

    In your OP you said you wanted to get an item from a JList into a JTextField when you click on the item. I explained how to do that with a selection listener.

    Now you've added a selection listener, but instead of extracting the item and putting it in a text field, you've introduced an edit button for some reason...

    What is it you actually want to achieve?

    Simplicity is the soul of efficiency...
    A. Freeman
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  5. #5
    Join Date
    Feb 2009
    Posts
    7

    Re: how to get data from my jList (which is populated from oracle database) to textfi

    ya i still want to extract an item from my Jlist into my 7 textfields. i was just asking if i could extract the item in my else statement underneath the enable edit button.

    sorry, my programming skills are really weak

    how would i go about doing this?...
    "and get the item index from the ListSelection event. You can then get the item at that index from the ListModel."

  6. #6
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: how to get data from my jList (which is populated from oracle database) to textfi

    Quote Originally Posted by mcpubes View Post
    how would i go about doing this?...
    You've added items from the database to the DefaultListModel, so you know about the list model. You know how to access the index of the selected item, because you do it in the selection listener, so you could use that index to get the item at that index from the list model - the method to use can be found in the list model API docs.

    However, you don't actually need to do this - you can just get the selected item directly from the JList with a single call. Check the JList API for the method to call - it's hard to miss

    It is really important when using classes you haven't written yourself (e.g. library classes) to familiarise yourself with the class API docs. They are the primary source of information about what facilities the class supplies and how to use them. If you want to get the selected value from a JList, the first thing to do is look for a JList method that does that. If you don't find one, you'll have to find an indirect way to do it, but in this particular case you will find a method.

    One can think effectively only when one is willing to endure suspense and to undergo the trouble of searching...
    J. Dewey
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  7. #7
    Join Date
    Feb 2009
    Posts
    7

    Re: how to get data from my jList (which is populated from oracle database) to textfi

    ah thanks.. i think i've found the single call.. getSelectedValue?

    i can't get it to output into the textfields tho.
    thanks for the help.. even though i'm still stuck, i'm learning

    Code:
    jListPList.addListSelectionListener(new ListSelectionListener()
    		{
    			
    			public void valueChanged(ListSelectionEvent e)
    			{
    				if(e.getValueIsAdjusting() == false)
    				{
    					if(jListPList.getSelectedIndex() == -1)
    					{
    						//no selection, disable edit button
    						jButtonEdit.setEnabled(false);
    					}
    					else
    					{
    						//selection, enable edit button
    						jButtonEdit.setEnabled(true);
    						jtfID.setText(jListPList.getSelectedValue().toString());
    						jtfName.setText(jListPList.getSelectedValue().toString());
    
    					}
    				}
    			}   
    		});

  8. #8
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: how to get data from my jList (which is populated from oracle database) to textfi

    The code you posted works for me, with trivial data, e.g.
    Code:
    DefaultListModel dlm = new DefaultListModel();
    
    dlm.addElement("one");
    dlm.addElement("two");
    dlm.addElement("three");
    dlm.addElement("four");
    
    jListPList.setModel(dlm);
    
    // your code here
    ...
    The most likely way for the world to be destroyed, most experts agree, is by accident. That's where we come in; we're computer professionals. We cause accidents...
    N. Borenstein
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  9. #9
    Join Date
    Feb 2009
    Posts
    7

    Re: how to get data from my jList (which is populated from oracle database) to textfi

    oops nevermind, i'm getting output! i just put the wrong textfield names.
    thanks!

    would i be able to use getSelectedValue to output seperate data from the jlist/database to seperate textfields?..
    ie. grab the PACKAGEID from database to package ID textfield

    right now it just adds everything to every textfield

  10. #10
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: how to get data from my jList (which is populated from oracle database) to textfi

    It's up to you to decide what you want to make it do. Look at the JList API and see if it has a method to do what you want. If you can identify which item number you want to display in which text field, you can use the getSelectedIndex method to check which index is the selected item and use the list model elementAt method to retrieve it and put it into the appropriate text field.

    I have no idea what PACKAGEID is or how it is stored in the database, so I can't help with that. If you can explain what the criteria are for a particular item to go in a particular field, I might be able to suggest something.


    If you cannot describe what you are doing as a process, you don't know what you're doing...
    W. E. Deming
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  11. #11
    Join Date
    Feb 2009
    Posts
    7

    Re: how to get data from my jList (which is populated from oracle database) to textfi

    hm i have this bit of code that kind of gives me what i want.. but it's initiated by typing an ID in a textfield
    would it be possible to modify this somehow without having to type input in a textfield?

    Code:
    Statement stmt =  conn.createStatement();
    							s1 = "select * from Packages where PACKAGEID=" + jTextFieldID.getText();
    							ResultSet rs =  stmt.executeQuery(s1);
    							if(rs.next()){
    								jTextFieldName.setText(rs.getString(2));
    								jTextFieldStartDate.setValue(rs.getDate(3));
    								jTextFieldEndDate.setValue(rs.getDate(4));
    								jTextFieldDesc.setText(rs.getString(5));
    								jTextFieldBasePrice.setText(rs.getString(6));
    								jTextFieldAgencyComm.setText(rs.getString(7));
    here's the info for my database

    PACKAGEID NUMBER 10 (pk)
    PKGNAME VARCHAR2 100
    PKGSTARTDATE DATE
    PKGENDDATE DATE
    PKGDESC VARCHAR2 100
    PKGBASEPRICE NUMBER 19 4 (0)
    PKGAGENCYCOMMISSION NUMBER

  12. #12
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: how to get data from my jList (which is populated from oracle database) to textfi

    Quote Originally Posted by mcpubes View Post
    ... it's initiated by typing an ID in a textfield
    would it be possible to modify this somehow without having to type input in a textfield?
    Certainly it's possible - so where do you want the ID to come from?

    If you can't explain it simply, you don't understand it well enough...
    A. Einstein
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  13. #13
    Join Date
    Feb 2009
    Posts
    7

    Re: how to get data from my jList (which is populated from oracle database) to textfi

    I want to somehow get the ID by clicking a package from the jList.

  14. #14
    Join Date
    May 2002
    Location
    Lindenhurst, NY
    Posts
    867

    Re: how to get data from my jList (which is populated from oracle database) to textfi

    Quote Originally Posted by mcpubes View Post
    I want to somehow get the ID by clicking a package from the jList.
    When you're learning something for the first time, like list boxes, write a small test program. Then use this knowledge to implement list boxes into your real program. This way, if you have a problem with list boxes, you can post your small test program instead of your real program that has list box code somewhere in it. Break the problem into smaller problems. Walk first, then run. Use SSCCE's. Instead of asking a question like 'how do I popluate a list box with the results of an SQL query' ask 'how do I popluate a list box with strings', then 'how do I do SQL queries', then on your own, combine the knowledge to implement your solution.

  15. #15
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: how to get data from my jList (which is populated from oracle database) to textfi

    Quote Originally Posted by mcpubes View Post
    I want to somehow get the ID by clicking a package from the jList.
    OK, assuming a 'package' is one of those strings you pulled out of the result set, and the package doesn't contain its ID in the string, you have to be able to associate each package string with its ID in advance, so that when a package is selected, you can use it as a 'key' to look up its ID.

    The way I'd suggest is to store the package and ID as two columns in the same database table, and extract them from the result set together. Use the paired package/ID items to create a new object (a class called 'Package'?) containing both package and ID. Now if you give this Package class a toString() method that returns the package string, you can put it into the list box, and the package string will be displayed. So when you retrieve the selected item, it will already contain the ID you want.

    The art of programming is the arto of organizaing complexity, of mastering multitude and avoiding its bastard chaos...
    E. Dijkstra
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured