JList selection problems

I'm having problems with JList selection - it's really wierd.
I have a Jlist and on different selections I want to do stuff - well, I do see the GUI line get highlighted but everytime my ListSelectionListener is called the selected index stays 0.
also, it always calls the ListSelectionListener twice for every selection I make.
here's how my code looks:
(typesList is my JList)
typesList.setSelectedIndex(0);
typesList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
typesList.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
      public void valueChanged(ListSelectionEvent e) {       
        int newSelection = typesList.getSelectedIndex(); //this returns always 0!        ...
      }

Look at the line getValueIsAdjusting... (only one selection is processed. And, this code works and provides proper index.
    * Method to load all Tables from Database user
    public void loadTables() {
        // run Database request
        tableListModel = getListModelData("select object_name from user_objects where object_type = 'TABLE'");
        // now plug List with Model
        tableList = new JList(tableListModel);
        tableList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        tableList.setSelectedIndex(0);
        //tableList.addListSelectionListener(this);
        tableList.addListSelectionListener(
            new ListSelectionListener() {
                public void valueChanged( ListSelectionEvent e )
                    if(e.getValueIsAdjusting()) return;
                    String tablename = tableList.getSelectedValue().toString();
                    showColumnData(tablename);
                    displayAllTableData(tablename); //intensive (reworking)
        jScrollPane1.add(tableList);
        jScrollPane1.setViewportView(tableList);
        showColumnData(tableList.getSelectedValue().toString());
    }PiratePete
http://www.piratepetesoftware.com

Similar Messages

  • How to 'veto' a jList selection change

    Hi All,
    I have a JList which I'm using as a record selector - so whenever my user selects an item on the list, the associated record is loaded up into the other controls on this form for editing. If the user edits any of the data items, I want to ask whether to save changes or not if another item is selected from the JList. So, I've added code in my 'selection changed' function to check for changes and show a 'yes/no/cancel' JOptionPane. The code for my 'yes' and 'no' respones work fine - either save the changes or not, then show the next record. My problem is handling if a user clicks cancel...
    If the user selects 'Cancel', I dont want any of my "show new record" code to execute (this is easy, I can just 'return' out of the function) but also I don't want the JList selection to change. I've tried calling setSelectedIndex() back to the originally selected item, but this in turn triggers my 'selection changed' function to be called again, which causes the user to be asked twice whether they want to save changes!
    So, what I'm after is a kind of beforeSelectionChanged event, which allows the possibility of denying the selection change - but it doesn't look like this exists! I vaguely remember another language (possibly C++/MFC) having this - the user's action could be ignored depending on the return value of the function. Can anyone offer a way of achieving this in Java?
    (It's been a while since I last touched Java, and I'm a complete n00b with Swing. Using NetBeans as my IDE.)
    Thanks in advance for any suggestions!
    Andy

    The way I do it, is to implement a VetoableSelectionModel similar to a bean with a vetoable property: on selection change it queries registered VetoableChangeListeners if they don't object and backs out if one of them barks.
    HTH
    Jeanette

  • Selection Problem with JTable

    Hello,
    i have a selection problem with JTable. I want to allow only single cell selection and additionally limit the selection to the first column.
    I preffered the style from MS Outlook Express where you can select the email accounts to edit.
    It is a table like this:
    Account name  |   Type  |   ...
    --------------|---------|---------------------
    Hotmail       |   POP3  |
    GMX           |   IMAP  |The selection should be only avaibable at 'Hotmail' or 'GMX' - not at 'POP3', 'IMAP' or as complete row selection.
    Please help me!
    Thanks.
    Warlock

    Maybe this will helpimport java.awt.*;
    import javax.swing.*;
    public class Test3 extends JFrame {
      public Test3() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Container content = getContentPane();
        String[] head = {"One", "Two"};
        String[][] data = {{"R1-C1", "R1-C2"}, {"R2-C1", "R2-C2"}};
        JTable jt = new JTable(data, head);
        jt.getColumnModel().setSelectionModel(new MyTableSelectionModel());
        content.add(new JScrollPane(jt), BorderLayout.CENTER);
        jt.setCellSelectionEnabled(true);
        jt.setRowSelectionAllowed(false);
        jt.setColumnSelectionAllowed(false);
        setSize(300, 300);
        setVisible(true);
      public static void main(String[] arghs) { new Test3(); }
    class MyTableSelectionModel extends DefaultListSelectionModel {
      public void setSelectionInterval(int index0, int index1) {
        super.setSelectionInterval(0, 0);
    }

  • Select Problem For 'Back Menus' - Zen V P

    Just got the Zen V Plus and tried to set the time/date. Followed the 'guide' and after the time/date screen pressed the 'back button'. Got the Set Alarm/Date/Time (etc) menu but when I selected an option (moved joystick down to desired option then pressed the joystick) the time/date display came up and I was NOT ABLE to set the date. Same thing occurred trying to set the date (tried it several times and it worked once .... then attempted again - several times (4) but it only worked once). Turns out I have the same problem with any option in ANY 'back' menu. Suggestion?
    Do I have a defecti've unit (can't be the 'operator' who is defecti've! haha)? Should I return it and try another?
    Actually not that interested in playing music .... it's more for playing .wma(DRM) book files.

    Latest ... updated my firmware (to ZENVPlus_PCFW_P4S_L2___0.exe) and it fixed the select problem for SET DATE/TIME etc however
    the SELECT procedure doesn't always work for 'back button' menus. Example: tried to set a bookmark... following procedure in 'guide', pressed 'back button' and held it, from 'NOW PLAYING' screen - works 2 out of 5 times. Most of the time, pressing and holding takes you back to the previous menu - not to the 'back' (or in this case the SET BOOKMARK) menu. Sounds like something for the next version of firmware ... the code doesn't always set an internal timer correctly (am a programmer of 30 years ... part of that time pgmmng firmware).

  • Print selection problem

    when i select a page to be print from my pc the page select in

    Hello. I feel your pain and frustration with the print selection problem in safari, especially in Snow Leopard. I have spent hours trawling help pages and forums etc. BUT YEE HAA (sorry got a bit excited, but it really was hours) I found the answer. So here goes.
    Click on Safari
    Scroll down to services then slide to right
    Scroll down to services preferences and click
    Scroll down to the Text section
    If you Tick the Text box it will choose all options for you. Or if you don't want all options in the Text list, then untick Text box and tick options you do want.
    BUT for your print selection problem make sure you tick NEW TEXT EDIT WINDOW CONTAINING SELECTION.
    Then when you want to select something in a web page you want to print (including pictures etc) then highlight it, then Right click. And low and behold, in the list is NEW TEXT EDIT WINDOW CONTAINING SELECTION. Click that, Then press cmd+p and your printing.
    I really hope that helps with your problem
    I can now go to sleep.

  • At Line selection problem

    hi,
    iam stuck up with AT LINE-SELECTION problem ie i designed a screen where they need month list on clicking F4. i got the month list using month_names_get in the screen but while clicking the relevant month no.it's not entering into the required parameter. i have attached the codings,
    MODULE MONTH_DIS OUTPUT.
      SUPPRESS DIALOG.
      LEAVE TO LIST-PROCESSING AND RETURN TO SCREEN 0.
      SET PF-STATUS SPACE.
      NEW-PAGE NO-TITLE.
      WRITE:/ 'SELECT MONTH' COLOR COL_HEADING.
      ULINE.
      DATA: T_MNTH LIKE T247 OCCURS 12 WITH HEADER LINE.
      refresh t_mnth.
      CALL FUNCTION 'MONTH_NAMES_GET'
       EXPORTING
         LANGUAGE                    = SY-LANGU
    IMPORTING
      RETURN_CODE                 =
        TABLES
          MONTH_NAMES                 = T_MNTH
       EXCEPTIONS
         MONTH_NAMES_NOT_FOUND       = 1
         OTHERS                      = 2
      IF SY-SUBRC <> 0.
        MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
                WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
      ENDIF.
      loop at t_mnth.
        write:/ t_mnth-mnr, t_mnth-ltx.
      endloop.
      CLEAR T_MNTH-MNR.
    ENDMODULE.                 " MONTH_DIS  OUTPUT
    AT LINE-SELECTION.
      p_period = t_mnth-mnr.
      CHECK NOT p_period IS INITIAL.
      LEAVE TO SCREEN 0.
    here in the AT LINE-SELECTION, the selected month is not coming to the required parameter.
    pls help me in this issue,
    Thanks in advance,
    Premnath.

    You need to set a "Hide" after the write... I suggest you also set your own dialog PF-STATUS too so you can have just a PF2 "pick" button and a Cancel button ( this will look better to the user).
    loop at t_mnth.
      write:/ t_mnth-mnr, t_mnth-ltx.
      hide: t_mnth.  "needs this
    endloop.
    clear: t_mnth.

  • Weird BGP path selection problem

    Hi, all,
    I am seeing a weird BGP path selection problem on 4948 switch running cat4500-entservicesk9-mz.122-46.SG.bin code, this switch has two uplinks to the same ISP's different edge router, one circuit is primary the other one is strict backup, only default route is accepted from ISP. I am setting both local preference and weight to the default route advertised over backup link, however neither one is taking effect, BGP still thinks the backup link is better, what could be wrong?
    rtr#sh ip bgp 0.0.0.0/0
    BGP routing table entry for 0.0.0.0/0, version 105
    Paths: (3 available, best #2, table Default-IP-Routing-Table, not advertised to EBGP peer)
      Not advertised to any peer
      17675, (received & used)
        203.169.8.37 from 203.169.8.37 (61.211.160.150)
          Origin IGP, localpref 100, valid, external
          Community: 65001:0 no-export
      17675
        203.169.8.45 from 203.169.8.45 (61.211.160.151)
          Origin IGP, localpref 90, weight 90, valid, external, best <====
          Community: 65001:0 no-export
      17675, (received-only)
        203.169.8.45 from 203.169.8.45 (61.211.160.151)
          Origin IGP, localpref 100, valid, external
          Community: 65001:0 no-export
    Thanks

    Hi,
    On cisco routers , weight is having highest preference to decide best path. By default for received route, weight is 0 but you are setting weight 90 to backup path and that is why it is getting preferred (higher is better). Please remove weight and let local preference be 90 (lesser than route on primary path)
    --Pls dont forget to rate helpful posts--
    Regards,
    Akash

  • Certificate selection problem in Safari

    Hi ,
    I have certifcates A,B,C,D for the same site , whenever i use the Mozilla it is asking which one to select , but somehow i dont know why Safari is asking the same option.
    It is forcing me to accept Certifcate A to that paricular site. How to solve this problem in safari.
    Regards
    Vikranth

    Hello. I feel your pain and frustration with the print selection problem in safari, especially in Snow Leopard. I have spent hours trawling help pages and forums etc. BUT YEE HAA (sorry got a bit excited, but it really was hours) I found the answer. So here goes.
    Click on Safari
    Scroll down to services then slide to right
    Scroll down to services preferences and click
    Scroll down to the Text section
    If you Tick the Text box it will choose all options for you. Or if you don't want all options in the Text list, then untick Text box and tick options you do want.
    BUT for your print selection problem make sure you tick NEW TEXT EDIT WINDOW CONTAINING SELECTION.
    Then when you want to select something in a web page you want to print (including pictures etc) then highlight it, then Right click. And low and behold, in the list is NEW TEXT EDIT WINDOW CONTAINING SELECTION. Click that, Then press cmd+p and your printing.
    I really hope that helps with your problem
    I can now go to sleep.

  • JList selection is listened twice

    Hi
    How to make JList selection listener to catch selection events for only one mouse click (mouse pressed or mouse released)

    Hi,
    you are right!
    first time e.getValueIsAdjusting() is true second time
    it is false. It does not corrspond to mousePressed or mouseReleased. I think the first is an item loosing selection and the second is another item getting selection (just a guess).
    Phil

  • Selection problem in a JList !!!!

    I am using a JList and on the selection of an item in this JList - I enable and disable some buttons.
    I have currenlty added a ListSelectionListener that allows me to trap the valuechanged event and thus if any item in the list is selected / deselected, i can enable/ disable the buttons.
    Now the abuv works fine with click and Ctrl clicking (to deselect),
    However if i come into the JList from another control (a JCombo) by tabbing and then use the arrow keys once the control is in the JList, the up and down arrow keys allow me to change the selection in the list.
    But, the valueChanged event does not come in the above case and hence the buttons do not enable/disable !!!
    Can you tell me what can be the problem?????

    Add a key listener and handle the KeyEvent
    Sachin

  • Selection problem with my JList

    Ok the problem is when i write this:
    myJlist.setEnabled(false);
    i set now the list in light grey but i can still select it someone knows why? Or i'm crazy :P

    After thorough investigation, yes, you are crazy!!!

  • JList selection focus problem

    Hi,
    I'm trying to write a help viewer similar to a MS one. I would like to be able to type characters into a textfield,and as they are typed, the matching JList item is selected.
    This works, but the selected item in the JList is only painted as selected when it gains focus. Is there any way to make a JList item paint as if it is selected and has focus, even though another component actually has the focus?
    Thanks.

    Make a custom cellrenderer extending the DefaulListCellRenderer and let it do smething like the code below. The borders on cellHasFocus are my custom variants, but I guess it's easy to find the default ones if you like.
    Have a look in the tutorial about custom renderers and in the performande book at :
    http://java.sun.com/docs/books/performance/
    or more precisly at:
    http://java.sun.com/docs/books/performance/1st_edition/html/JPSwingModels.fm.html#1001816
    It's easy to make some errors in renderers... Never create any new objects and other things and you're halfway there.
      public Component getListCellRendererComponent(JList list, Object value,
           int index, boolean isSelected, boolean cellHasFocus)
              if (isSelected)
                 this.setBackground(list.getSelectionBackground());
                 this.setForeground(list.getSelectionForeground());
              else
                 this.setBackground(list.getBackground());
                 this.setForeground(list.getForeground());
              if (cellHasFocus)
                 this.setBorder(hasFocusBorder);
              else
                 this.setBorder(hasNotFocusBorder);
              return this;
       }

  • How do I get my JList selection to Display after changing

    Granted there are probably 1,000,000,000 ways to write this code. The intent is to have a list of items that are assigned into catagories and when an item is selected the catagories that the item are in are highlighted in the list. This part works OK, as I change items the catagories change to reflect the current item catagories. I have two buttons to add catagories to items and remove catagories from items. The catagory to add is selected from a JCombo drop down list and then the user clicks the addcat button to add the catagory to the item.
    My problem is that the catagories in the JList (that also reside in a JScrollPane) do not show as selected after the list is updated. If I click the addcat button twice then the new catagory is highlighted. I thought that the suggestion in other forum messages to add revalidate and repaint would work so I tried that both at the list and the scrollpane level with no effect.
    -------------- Button Listener code --------------------
    addcat.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    // get the curent catagory from the JCombo..
         String selectedcat = (String) fullcatlist.getSelectedItem();
    // get the curent Item from the JCombo..
         String selecteditem = (String) itemlist.getSelectedItem();
    // add item to catagory
         items.addItemCatagory(selecteditem,selectedcat);
    // debug
         product.setText(selectedcat +"::"+selecteditem);          
         int cnt = 0;
    // get a list of all catagories for this item
         String[] tgtList = catagories.getItemCatagories(selecteditem);
         // debug
         product.setText(selectedcat);          
    // get the JList list
         final ListModel lm = catlist.getModel();
    // get the list size
         int lsize = lm.getSize();
         String st ="";
         for (int j = 0; j < lsize;j++) {
    // get jth element of the list
         st = (String)lm.getElementAt(j);
    // compare it to the selected catagories list in if a match
    // set the index into the selected index array
         for (int k=0;k<tgtList.length;k++) {
         if ( st.compareTo(tgtList[k]) == 0 )
         lst[cnt++]=j;          
    //set up the selections
         int[] ilst = new int[cnt];
         for (int k = 0; k<cnt;k++)
         ilst[k] = lst[k];
    // enable teh selected indice
         catlist.setSelectedIndices(ilst);
    // paint the frame
         scrollPane.revalidate();
         scrollPane.repaint();
    --------------------- end code -----------------

    This might sound sarcastic, but it's not:
    I have no clue how to fix your code, but if your bored and want to try something, try copy-and-pasting that section right afterwards. You'd probably need to change some variables, but, again, I have no clue. This is all just a wild guess. Sorry I couldn't help you any more.

  • JList item problem

    Hello Coders
    I have question regarding to Jlist.
    To capturing event i use this code :
    class roomListener implements ListSelectionListener{
        public void valueChanged(ListSelectionEvent e) {
        int x=0;
        System.out.prinln("the roomListener invoked for=>"+ x++);
          if (e.getValueIsAdjusting() == false) {
            if (roomList.getSelectedIndex() == -1) { // if no selection
              p("roomList is no selection");
            }else {// selection made
              currRoom = roomList.getSelectedValue().toString();//get the nickFromList
              roomLabel.setText("Your currRoom is=>"+currRoom);
              // request charom
              ClientRequester.sendChanghRoom(shortName,currRoom);
      }the output is :
    the roomListener invoked for=>0
    (i want to response to every single click on the same item)
    The problem is :
    When I click on item I see that valueChanghed() invoked , but when I prees again and again on the same item valueChanghed() not invoked anymore , why?
    Thanks

    hi,
    pretty clear, isn't it? valueChanged() is only invoked when the selection value CHANGED. If you click on the selected item again and again, the selection doesn't change. I suggest attaching a mouse listener to the list instead.
    best regards

  • JList Selection

    Hello everyone,
    first of all, excuse me for my poor english.
    I have a JList contaning nearly 200 items and I would like the maximum selectable item count to be 5.
    I thought a good way to do that could be to add a listener to the list data and each time an event occurs on the list data run the getSelectedIndexes() method. Then if the result length is more than 5 the event could be canceled.
    But, I am not sure it's the best way to do that and most of all I don't know how to cancel and event.
    Could some help me please. Thank yopu very much.

    Thank you for your answer because the code you gave me is the one I use, so now I'm sure it's the good way to proceed.
    My problem is that I don't know what to do in remplacement of the comment. That's to say I don't know how to unselect the box just selected by the user witch is not necessary the one with the higher index.
    In this case: user selects items with following indexes : 1, 7, 8, 125, 130 and then a 6th: 34. The min index will be 1, the max index wille be 130, but how can I know that the last selected index is 34.
    That's why I wanted to "cancel" the event; in order to unselect the last selected item.
    Thank you

Maybe you are looking for

  • XSLT mapping sorting against an output field

    Hi, all I am testing my XSLT mapping. Here is the output message type defintion: I want to sort f2 in my xslt mapping. Here is my mapping: <?xml version="1.0" encoding="UTF-8" ?>   <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/T

  • EXCEPTION_ACCESS_VIOLATION error in SapJCo DLL'S

    Hi All,<br><br> I have been having EXCEPTION_ACCESS_VIOLATION errors crashing the JVM in our SapJCo Client.<br><br> The error appears intermitant at the moment and we are still unable to replicate it on demand. I have found several references to simi

  • Report output not coming correctly

    Hi All, my query is like select 'gggohpoopiokoljmkjjg@@@@@' record from dual; where record string's length is more than 2000 characters. I am using oracle report for this purpose as i have some other validations associated with it. i am printing the

  • Wordpress 403 permission denied

    Whenever I try to get to my newly installed wordpress I get a 403 permission denied error. What can I do about this? Last edited by kahrkunne (2014-04-11 13:40:52)

  • Not able to get file handle

    Hi, First scenario. I run the following piece of code locally File file = new File(fileName); It works fine. Second Scenario. This code is put in a jar file and put on a shared drive. The application is started by clicking a bat file on the same shar