Edit a JList

hiya,
if u click on a JTable Editable Cell, u can Edit it and change the value.
can i use the same Technic with JList?
i mean double click on a row and Edit it?
u can do that in JTree also for ex.
if yes? what Method or Property do i need?
thx

i did a google searc on the internet and finally got to palce:
http://java.sun.com/docs/books/tutorial/uiswing/components/list.html
it's tutorial on JList...
i don't know the exact Y7N answer to your question, ut i'm sure that you may create a JList that has such feature, that when you (double)click on a item, then you may edit it....
you can achieve it by seting some custom Renderer for Cell item
you should implement for that CellListRenderer
http://java.sun.com/j2se/1.4.1/docs/api/javax/swing/ListCellRenderer.html
basically JTable has also different renderers for these cells, so it should be quite similar.
HTH

Similar Messages

  • How to edit Integer numbers in a JList?

    I need to edit Integer numbers in a JList when double clicked.
    Anyone has any example how to do that?
    It will be always Integer numbers (they are used to calculate future dates based on today date).
    Appreciate any help

    JList by default is not capable of handling editting of content like JTree and JTable. But with some customization, that is possible, see this site for more info: http://www.jroller.com/santhosh/date/20050607
    ICE

  • Editable components in JList cell?

    I want to create a JList of objects, where the list entries contain a JCheckBox that corresponds to a boolean property.
    It's easy to add the checkbox by writing a new ListCellRenderer. BUT, the checkbox can't be edited-- mouse-clicks only select/deselect the item.
    Can someone tell me if and how the checkbox can be made editable? Or should I give up and use a JTable instead?

    You can also use a JTable rather than a JList, just configure it so the line-cell-separators will not be displayed, and also do not use the table header. JTable already take care of editors. Here is a ref where you can found some source code.
    http://www2.gol.com/users/tame/swing/examples/JTableExamples3.html

  • Changing JList non-editable Font color of the selected items

    Hi All,
    I want to change the font color of non-editable JList's selected items which is not clearly visible to user. And I need to change the font color only the selected item in this scenerio.
    Could you please clarify me?
    <img src="file:///C:/DOCUME~1/sgnanasi/LOCALS~1/Temp/moz-screenshot-4.png" alt="" />

    [email protected] wrote:
    ..I want to change the font color of non-editable JList's selected items which is not clearly visible to user. And I need to change the font color only the selected item in this scenerio.Set a custom [cell renderer|http://java.sun.com/javase/6/docs/api/javax/swing/ListCellRenderer.html] *(<- link)* for the JList.

  • Editing Jlist Item Contents....

    hi,
    i want to edit the contents of a Jlist Item.
    any help???
    Manikandan

    in your ListSelectionListener you must check if the event is one of multiple change events. In your case you get one event when the mouse first selects an item and possible more when you move the cursor over the other entries. If you are only interested in the final selection check if e.getValueIsAdjusting() is false.
    // not interested in events if they are not final
    if (e.getValueIsAdjusting())
        return;
    // do what ever you want
    System.out.println(colorList.getSelectedValue());
    ...Hope this helps!

  • How to make JList Editable

    How can i enter things in the JList? Is that possible be done? Pls advice.

    Its right, JList is not editable but You can catch events
    I for example use this
    public void jList1_KeyPressed(java.awt.event.KeyEvent keyEvent) {
         if (keyEvent.getKeyCode() == keyEvent.VK_ENTER) {
              selectDetailWindows(ivjJList1.getSelectedIndex());
         return;
    }where selectDetailWindows knows the Listindex, get data edit them and writes them back.
    Hop this helps
    Hanns

  • How to  move items from one JList to other

    Can u pls help me out to implement this(I m using Netbeans 5.5):
    I want to move items from one JList to other thru a ADD button placed between JLists, I am able to add element on Right side JList but as soon as compiler encounter removeElementAt() it throws Array Index Out of Bound Exception
    and if I use
    removeElement() it removes all items from left side JList and returns value false.
    Pls have a look at this code:
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
    // TODO add your handling code here:
    Object selItem = jList1.getSelectedValue();
    int selIndex = jList1.getSelectedIndex();
    DefaultListModel model = new DefaultListModel();
    jList2.setModel(model);
    model.addElement(selItem);
    DefaultListModel modelr = new DefaultListModel();
    jList1.setModel(modelr);
    flag = modelr.removeElement(selItem);
    //modelr.removeElementAt(selIndex);
    System.out.println(flag);
    }

    hi Rodney_McKay,
    Thanks for valuable time but my problem is as it is, pls have a look what I have done and what more can b done in this direction.
    Here is the code:
    import javax.swing.DefaultListModel;
    import javax.swing.JList;
    public class twoList extends javax.swing.JFrame {
    /** Creates new form twoList */
    public twoList() {
    initComponents();
    //The code shown below is automatically generated and we can�t edit this code
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">
    private void initComponents() {
    jScrollPane1 = new javax.swing.JScrollPane();
    jList1 = new javax.swing.JList();
    jButton1 = new javax.swing.JButton();
    jScrollPane2 = new javax.swing.JScrollPane();
    jList2 = new javax.swing.JList();
    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    jList1.setModel(new javax.swing.AbstractListModel() {
    String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
    public int getSize() { return strings.length; }
    public Object getElementAt(int i) { return strings[i]; }
    jList1.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
    public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
    jList1ValueChanged(evt);
    jScrollPane1.setViewportView(jList1);
    jButton1.setText("ADD>>");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jButton1ActionPerformed(evt);
    jScrollPane2.setViewportView(jList2);
    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGap(31, 31, 31)
    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
    .addComponent(jButton1)
    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addContainerGap(78, Short.MAX_VALUE))
    layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jScrollPane1, jScrollPane2});
    layout.setVerticalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGap(62, 62, 62)
    .addComponent(jButton1))
    .addGroup(layout.createSequentialGroup()
    .addContainerGap()
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
    .addContainerGap(159, Short.MAX_VALUE))
    layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jScrollPane1, jScrollPane2});
    pack();
    }// </editor-fold>
    //automatic code ends here
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
    // TODO add your handling code here:
            jList1 = new JList(new DefaultListModel());
            jList2 = new JList(new DefaultListModel());
             Object selItem = jList1.getSelectedValue();
             System.out.println(selItem);
            ((DefaultListModel) jList1.getModel()).removeElement(selItem);
            ((DefaultListModel) jList2.getModel()).addElement(selItem);
    //Now trying with this code it is neither adding or removing and the value �null� is coming in �selItem� .It may be bcoz JList and Jlist are already instantiated in automatic code. So, I tried this:
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
    // TODO add your handling code here:
             Object selItem = jList1.getSelectedValue();
             System.out.println(selItem);
            ((DefaultListModel) jList1.getModel()).removeElement(selItem);
            ((DefaultListModel) jList2.getModel()).addElement(selItem);
    //Now with this as soon as I click on �jButton1�, it is throwing this error:
    Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: twoList$1 cannot be cast to javax.swing.DefaultListModel
            at twoList.jButton1ActionPerformed(twoList.java:105)
            at twoList.access$100(twoList.java:13)
            at twoList$3.actionPerformed(twoList.java:50)
            at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
            at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
            at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
            at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
            at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
            at java.awt.Component.processMouseEvent(Component.java:6038)
            at javax.swing.JComponent.processMouseEvent(JComponent.java:3260)
            at java.awt.Component.processEvent(Component.java:5803)
            at java.awt.Container.processEvent(Container.java:2058)
            at java.awt.Component.dispatchEventImpl(Component.java:4410)
            at java.awt.Container.dispatchEventImpl(Container.java:2116)
            at java.awt.Component.dispatchEvent(Component.java:4240)
            at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
            at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
            at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
            at java.awt.Container.dispatchEventImpl(Container.java:2102)
            at java.awt.Window.dispatchEventImpl(Window.java:2429)
            at java.awt.Component.dispatchEvent(Component.java:4240)
            at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
            at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
            at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
            at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
            at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)

  • How to add words from a JTextField to a JList

    Im working on my final project for Java programming class using JApplet.
    Im trying to create a spelling test/wordfind
    I want to be able to have the user type a word in the JtextField press the "Add" button (Jbutton) and have the word be added to the JList.
    then click the "Start" Button and have the all the words from the JList be put in random stops in a panel and fill around all the words with random letters.
    then when the user clicks on the letter of the words they are finding it will change colors if they click on it again it will change back to its original color.
    So far I have my layout set up its adding the proper Listeners and events to achieve the desired results.
    I know this is way more advanced than what My Instructor is even teacher but I am very interested in Java and want to learn a lot more about it.
    If anyone has suggestions or know where i can find the information Im looking for please let me know.
    Thanks
    Code I have so Far:
    * @(#)SpellingTest.java
    * SpellingTest Applet application
    * @author
    * @version 1.00 2010/11/10
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class SpellingTest extends JApplet implements ActionListener {
         Image img;
         ImageIcon icon;
         private Image title;
         JTextField word;
         JButton addWord;
         JLabel label;
         JList wordList;
         public void init() {//opening init
                   getContentPane().setLayout(null);
                   getContentPane().setBackground(Color.WHITE);
                   title = getImage(getDocumentBase(), "title.jpg");
                   //The text field
                   word = new JTextField();
                   word.setLocation(190,100);
                   word.setSize(85,30);
                   getContentPane().add(word);
                   word.addActionListener(this);
                   //The addWord button
                   addWord = new JButton("ADD");
                   addWord.setLocation(295, 100);
                   addWord.setSize(90,30);
                   getContentPane().add(addWord);
                   addWord.setBackground(new Color(146, 205, 220));
                   addWord.addActionListener(this);
                   //The label
                   label = new JLabel("Type your spelling words:");
                   label.setLocation(25, 65);
                   label.setSize(250,100);
                   getContentPane().add(label);
                   label.setForeground(new Color(146, 205, 220));
                   //The List
                   wordList = new JList();
                   JScrollPane scrollPane = new JScrollPane(wordList,
                   ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
                   ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
                   scrollPane.setLocation(25,150);
                   scrollPane.setSize(150,300);
                   getContentPane().add(scrollPane);
         }//closing init
         public void actionPerformed (ActionEvent ae){
              Object obj = ae.getSource();
              String text = word.getText();
              if (obj == addWord){
                   if (text.length()>0);
                   wordList.addString("");
         public void paint(Graphics g) {
              super.paint(g);
    }

    Read the first posting titled "Welcome to the new home" to learn how to use code tags. Then you can edit your posting so the code is formatted and readable.
    I want to be able to have the user type a word in the JtextField press the "Add" button (Jbutton) and have the word be added to the JList.Read the JList API and follow the link to the Swing tutoral on "How to Use Lists" where you will find a working example.
    So far I have my layout Actually you haven't. You should learn how to use layout managers. Again the Swing tutorial explains what layout managers are and provides working example of using them.
    I am very interested in Java and want to learn a lot more about it.A great place to start is by read tutorials because they always contain working example. Here are some [url http://download.oracle.com/javase/tutorial/]Java tutorrial.

  • Need help in storing data from JList into a vector

    need help in doing the following.-
    alright i click a skill on industryskills Jlist and press the add button and it'll be added to the applicantskills Jlist. how do i further store this data that i added onto the applicantskills JList into a vector.
    here are the codes:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import java.text.*;
    import java.util.*;
    import java.util.Vector;
    import javax.swing.JScrollPane.*;
    //import javax.swing.event.ListSelectionListener;
    public class Employment extends JFrame
            //declare class variables
            private JPanel jpApplicant, jpEverything,jpWEST, jpCENTRE, jpEAST, jpAddEditDelete,
                                       jpCentreTOP, jpCentreBOT, jpEastTOP, jpEastCENTRE, jpEastBOT,
                                       jpBlank1, panel1, panel2, panel3, panel4,jpBottomArea,
                                       jpEmptyPanelForDisplayPurposes;
            private JLabel jlblApplicantForm, jlblAppList, jlblName, jlblPhone,
                                       jlblCurrentSalary, jlblPassword, jlblDesiredSalary,
                                       jlblNotes, jlblApplicantSkills, jlblIndustrySkills,
                                       jlblBlank1, jlblBlank2, ApplicantListLabel,
                                       NotesListLabel, ApplicantSkillsLabel,
                                       IndustrySkillsLabel,jlblEmptyLabelForDisplayPurposes;  
            private JButton jbtnAdd1, jbtnEdit, jbtnDelete, jbtnSave, jbtnCancel,
                                            jbtnAdd2, jbtnRemove;
            private JTextField jtfName, jtfPhone, jtfCurrentSalary, jtfPassword,
                                               jtfDesiredSalary;
              private JTabbedPane tabbedPane;
            private DefaultListModel /*listModel,*/listModel2;
              String name,password,phone,currentsalary,desiredsalary,textareastuff,NotesText;
              String selectedname;
            final JTextArea Noteslist= new JTextArea();;
            DefaultListModel listModel = new DefaultListModel();
            JList ApplicantSkillsList = new JList(listModel);
           private ListSelectionModel listSelectionModel;
            JList ApplicantList, /*ApplicantSkillsList,*/ IndustrySkillsList;
            //protected JTextArea NotesList;    
                    //Vector details = new Vector();
                  Vector<StoringData> details = new Vector<StoringData>();             
                public static void main(String []args)
                    Employment f = new Employment();
                    f.setVisible(true);
                    f.setDefaultCloseOperation(EXIT_ON_CLOSE);
                    f.setResizable(false);
                }//end of main
                    public Employment()
                            setSize(800,470);
                            setTitle("E-commerce Placement Agency");
                                  Font listfonts = new Font("TimesRoman", Font.BOLD, 12);
                            JPanel topPanel = new JPanel();
                            topPanel.setLayout( new BorderLayout() );
                            getContentPane().add( topPanel );
                            createPage1();
                            createPage2();
                            createPage3();
                            createPage4();
                            tabbedPane = new JTabbedPane();
                            tabbedPane.addTab( "Applicant", panel1 );
                            tabbedPane.addTab( "Job Order", panel2 );
                            tabbedPane.addTab( "Skill", panel3 );
                            tabbedPane.addTab( "Company", panel4 );
                            topPanel.add( tabbedPane, BorderLayout.CENTER );
            public void createPage1()//PAGE 1
                 /*******************TOP PART********************/
                            panel1 = new JPanel();
                            panel1.setLayout( new BorderLayout());
                                  jpBottomArea = new JPanel();
                                  jpBottomArea.setLayout(new BorderLayout());
                            jpApplicant= new JPanel();
                            jpApplicant.setLayout(new BorderLayout());
                            Font bigFont = new Font("TimesRoman", Font.BOLD,24);
                            jpApplicant.setBackground(Color.lightGray);
                            jlblApplicantForm = new JLabel("\t\t\t\tAPPLICANT FORM  ");
                            jlblApplicantForm.setFont(bigFont);
                            jpApplicant.add(jlblApplicantForm,BorderLayout.EAST);
                            panel1.add(jpApplicant,BorderLayout.NORTH);
                            panel1.add(jpBottomArea,BorderLayout.CENTER);
           /********************************EMPTY PANEL FOR DISPLAY PURPOSES*************************/
                               jpEmptyPanelForDisplayPurposes = new JPanel();
                               jlblEmptyLabelForDisplayPurposes = new JLabel(" ");
                               jpEmptyPanelForDisplayPurposes.add(jlblEmptyLabelForDisplayPurposes);
                               jpBottomArea.add(jpEmptyPanelForDisplayPurposes,BorderLayout.NORTH);
           /*****************************************WEST*********************************/             
                            jpWEST = new JPanel();                 
                            jpWEST.setLayout( new BorderLayout());
                            //Applicant List
                                  listModel2=new DefaultListModel();
                                  ApplicantList = new JList(listModel2);
                                listSelectionModel = ApplicantList.getSelectionModel();
                                listSelectionModel.addListSelectionListener(new SharedListSelectionHandler());
                                JScrollPane scrollPane3 = new JScrollPane(ApplicantList);
                                  ApplicantList.setPreferredSize(new Dimension(20,40));
                                 scrollPane3.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);                                             
                                scrollPane3.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
                            ApplicantListLabel = new JLabel( "Applicant List:");
                            jpWEST.add(ApplicantListLabel,"North"); 
                            jpWEST.add(scrollPane3,"Center");
                            jpBottomArea.add(jpWEST,BorderLayout.WEST);
                            /*********CENTRE*********/
                            jpCENTRE = new JPanel();
                            jpCENTRE.setLayout(new GridLayout(2,1));
                            jpCentreTOP = new JPanel();
                            jpBottomArea.add(jpCENTRE,BorderLayout.CENTER);
                            jpCENTRE.add(jpCentreTOP);
                            jpCentreTOP.setLayout(new GridLayout(6,2));
                              //Creating labels and textfields
                            jlblName = new JLabel( "Name:");
                            jlblBlank1 = new JLabel ("");
                            jtfName = new JTextField(18);
                            jlblBlank2 = new JLabel("");
                            jlblPhone = new JLabel("Phone:");
                            jlblCurrentSalary = new JLabel("Current Salary:");
                            jtfPhone = new JTextField(13);
                            jtfCurrentSalary = new JTextField(7);
                            jlblPassword = new JLabel("Password:");
                            jlblDesiredSalary = new JLabel("Desired Salary:");
                            jtfPassword = new JTextField(13);
                            jtfDesiredSalary = new JTextField(6);
                              //Add labels and textfields to panel
                            jpCentreTOP.add(jlblName);
                            jpCentreTOP.add(jlblBlank1);
                            jpCentreTOP.add(jtfName);
                            jpCentreTOP.add(jlblBlank2);
                            jpCentreTOP.add(jlblPhone);
                            jpCentreTOP.add(jlblCurrentSalary);
                            jpCentreTOP.add(jtfPhone);
                            jpCentreTOP.add(jtfCurrentSalary);
                            jpCentreTOP.add(jlblPassword);
                            jpCentreTOP.add(jlblDesiredSalary);
                            jpCentreTOP.add(jtfPassword);
                            jpCentreTOP.add(jtfDesiredSalary);
                            //Noteslist
                            jpCentreBOT = new JPanel();
                            jpCentreBOT.setLayout( new BorderLayout());
                            jpCENTRE.add(jpCentreBOT);
                            jpBlank1 = new JPanel();
                             //     Noteslist = new JTextArea(/*Document doc*/);
                            JScrollPane scroll3=new JScrollPane(Noteslist);
                                scroll3.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);                                             
                                scroll3.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
                            NotesListLabel = new JLabel( "Notes:");
                            jpCentreBOT.add(NotesListLabel,"North"); 
                            jpCentreBOT.add(scroll3,"Center");
                            jpCentreBOT.add(jpBlank1,"South");
                            jpBottomArea.add(jpCENTRE,BorderLayout.CENTER);
                            /**********EAST**********/
                            //Applicant Skills Panel
                            //EAST ==> TOP
                            jpEAST = new JPanel();
                            jpEAST.setLayout( new BorderLayout());
                            jpEastTOP = new JPanel();
                            jpEastTOP.setLayout( new BorderLayout());
                            ApplicantSkillsLabel = new JLabel( "Applicant Skills");
                            JScrollPane scrollPane1 = new JScrollPane(ApplicantSkillsList);
                               scrollPane1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);                                             
                              scrollPane1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);                                             
                            ApplicantSkillsList.setVisibleRowCount(6);
                            jpEastTOP.add(ApplicantSkillsLabel,"North"); 
                            jpEastTOP.add(scrollPane1,"Center");
                            jpEAST.add(jpEastTOP,BorderLayout.NORTH);
                            jpBottomArea.add(jpEAST,BorderLayout.EAST);
                            //Add & Remove Buttons
                            //EAST ==> CENTRE
                            jpEastCENTRE = new JPanel();
                            jpEAST.add(jpEastCENTRE,BorderLayout.CENTER);
                            jbtnAdd2 = new JButton("Add");
                            jbtnRemove = new JButton("Remove");
                            //add buttons to panel
                            jpEastCENTRE.add(jbtnAdd2);
                            jpEastCENTRE.add(jbtnRemove);
                            //add listener to button
                           jbtnAdd2.addActionListener(new Add2Listener());
                           jbtnRemove.addActionListener(new RemoveListener());
                            //Industry Skills Panel
                            //EAST ==> BOTTOM
                            jpEastBOT = new JPanel();
                            jpEastBOT.setLayout( new BorderLayout());
                           String[] data = {"Access97", "Basic Programming",
                           "C++ Programming", "COBOL Programming",
                           "DB Design", "Fortran programming"};
                           IndustrySkillsList = new JList(data);
                           JScrollPane scrollPane = new JScrollPane(IndustrySkillsList);
                           scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);                                             
                           scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
                           IndustrySkillsLabel = new JLabel( "Industry Skills:");
                           jpEastBOT.add(IndustrySkillsLabel,"North"); 
                           jpEastBOT.add(scrollPane,"Center");
                           jpEAST.add(jpEastBOT,BorderLayout.SOUTH);
                            //BOTTOM
                            jpAddEditDelete= new JPanel();
                            jbtnAdd1=       new JButton("Add");
                            jbtnEdit=       new JButton("Edit");
                            jbtnDelete=     new JButton("Delete");
                            jbtnSave=       new JButton("Save");
                            jbtnCancel=     new JButton("Cancel");
                            jpAddEditDelete.add(jbtnAdd1);
                            jpAddEditDelete.add(jbtnEdit);
                            jpAddEditDelete.add(jbtnDelete);
                            jpAddEditDelete.add(jbtnSave);
                            jpAddEditDelete.add(jbtnCancel);
                               jbtnEdit.addActionListener(new EditListener());
                               jbtnDelete.addActionListener(new DeleteListener());
                                jbtnEdit.addActionListener(new EditListener());
                            jbtnAdd1.addActionListener(new Add1Listener());
                            jbtnCancel.addActionListener(new CancelListener());
                            jpBottomArea.add(jpAddEditDelete,BorderLayout.SOUTH);
            public void createPage2()//PAGE 2
                    panel2 = new JPanel();
                    panel2.setLayout( new GridLayout(1,1) );
                    panel2.add( new JLabel( "Sorry,under construction" ) );
            public void createPage3()//PAGE 3
                    panel3 = new JPanel();
                    panel3.setLayout( new GridLayout( 1, 1 ) );
                    panel3.add( new JLabel( "Sorry,under construction" ) );
            public void createPage4()//PAGE 4
                    panel4 = new JPanel();
                    panel4.setLayout( new GridLayout( 1, 1 ) );
                    panel4.add( new JLabel( "Sorry,under construction" ) );
            public class Add1Listener implements ActionListener
            public void actionPerformed(ActionEvent e)
                    name = jtfName.getText();
                    password = jtfPassword.getText();
                    phone = jtfPhone.getText();
                    currentsalary = jtfCurrentSalary.getText();
                    int i= Integer.parseInt(currentsalary);
                    desiredsalary = jtfDesiredSalary.getText();
                    int j= Integer.parseInt(desiredsalary);
                       StoringData person = new StoringData(name,password,phone,i,j);
                   //     StoringData AppSkillsList = new StoringData(listModel);
                    details.add(person);
                 //     details.add(AppSkillsList);
                  listModel2.addElement(name);
                    jtfName.setText("");
                    jtfPassword.setText("");
                    jtfPhone.setText("");
                    jtfCurrentSalary.setText("");
                    jtfDesiredSalary.setText("");
    //                NotesList.setText("");
            public class Add2Listener implements ActionListener
                    public void actionPerformed(ActionEvent e)
                           String temp1;
                           temp1 = (String)IndustrySkillsList.getSelectedValue();
                           listModel.addElement(temp1);
            public class RemoveListener implements ActionListener
                    public void actionPerformed(ActionEvent e)
                            int index = ApplicantSkillsList.getSelectedIndex();
                                listModel.remove(index);
            public class EditListener implements ActionListener
                    public void actionPerformed(ActionEvent e)
                            jtfName.setEditable(true);
                            jtfPassword.setEditable(true);
                            jtfPhone.setEditable(true);
                            jtfCurrentSalary.setEditable(true);
                            jtfDesiredSalary.setEditable(true);
                            Noteslist.setEditable(true);
                            jbtnAdd2.setEnabled(true);               
                            jbtnRemove.setEnabled(true);
                            jbtnSave.setEnabled(true);
                            jbtnCancel.setEnabled(true);                     
            public class DeleteListener implements ActionListener
                    public void actionPerformed(ActionEvent e)
                        int index1 = ApplicantList.getSelectedIndex();
                            listModel2.remove(index1);
            public class SaveListener implements ActionListener
                    public void actionPerformed(ActionEvent e)
            public class CancelListener implements ActionListener
                    public void actionPerformed(ActionEvent e)
                    jtfName.setText("");
                    jtfPassword.setText("");
                    jtfPhone.setText("");
                    jtfCurrentSalary.setText("");
                    jtfDesiredSalary.setText("");                         
            public class SharedListSelectionHandler implements ListSelectionListener
            public void valueChanged(ListSelectionEvent e)
             selectedname =ApplicantList.getSelectedValue().toString();
             StoringData selectedPerson = null;
             jtfName.setEditable(false);
             jtfPassword.setEditable(false);
             jtfPhone.setEditable(false);
             jtfCurrentSalary.setEditable(false);
             jtfDesiredSalary.setEditable(false);                                 
             Noteslist.setEditable(false);
             jbtnAdd2.setEnabled(false);               
             jbtnRemove.setEnabled(false);
             jbtnSave.setEnabled(false);
             jbtnCancel.setEnabled(false);
                   for (StoringData person : details)
                          if (person.getName1().equals(selectedname))
                                 selectedPerson = person;
                                 jtfName.setText(person.getName1());
                                 jtfPassword.setText(person.getPassword1());
                              jtfPhone.setText(person.getPhone1());
                              //String sal1 = Integer.parseString(currentsalary);
                             // String sal2 = Integer.parseString(desiredsalary);
                             // jtfCurrentSalary.setText(sal1);
                             // jtfDesiredSalary.setText(sal2);
                                 break;
                   //     if (selectedPerson != null)
    }

    Quit posting 300 line programs to ask a question. We don't care about your entire application. We only care about code that demonstrates your current problem. We don't want to read through 300 lines to try and find the line of code that is causing the problem.
    If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.
    And don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags so the code retains its original formatting.
    Here is a simple SSCCE. Now make your changes and if you still have problems you have something simple to post. If it works then you add it to your real application.
    Learn to simplify your problem by simplifying the code.
    import java.awt.*;
    import javax.swing.*;
    public class ListTest2 extends JFrame
         JList list;
         public ListTest2()
              String[] numbers = { "one", "two", "three", "four", "five", "six", "seven" };
              list = new JList( numbers );
              JScrollPane scrollPane = new JScrollPane( list );
              getContentPane().add( scrollPane );
         public static void main(String[] args)
              ListTest2 frame = new ListTest2();
              frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
              frame.setSize(200, 200);
              frame.setLocationRelativeTo( null );
              frame.setVisible( true );
    }

  • Populating Swing JList using SQL Database

    Sup peeps, im trying to to populate a JList using the entries in a table in a database i created.
    package networks;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.PrintWriter;
    import java.sql.*;
    public class Contestantlist extends javax.swing.JFrame {
        /** Creates new form contestantList */
        public Contestantlist() {
            initComponents();
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
             try
               // Load driver's class, inilialize, register with DriverManager
               //Class.forName("oracle.jdbc.driver.OracleDriver");
               Class.forName("org.gjt.mm.mysql.Driver");
            catch (ClassNotFoundException e)
               System.out.println("Unable to load driver class");
               return;
            ResultSet rs = null;
            try
               // Call DriverManager's methods (all are static)
               // To print log on sysout
               //DriverManager.setLogStream(System.out); //JDBC 1.x driver
              // DriverManager.setLogWriter(new PrintWriter(System.out) );
               // Connect to database.  DriverManager loads each registered driver
               // in turn until one can handle the database URL format
               //Connection con = DriverManager.getConnection(
               //                     "jdbc:oracle:thin:@host.domain.com:1521:db_name"
               //                     ,"scott","tiger");
              // Connection con = DriverManager.getConnection("jdbc:mySql://localhost:3306/Contest","root","blink182");
               // Get the DatabaseMetaData object to display database info
            //   DatabaseMetaData dmd = con.getMetaData();
             //  Statement stmt = con.createStatement();
           //     String sql;
             //  sql = "SELECT Username FROM Contestant WHERE Username IS NOT NULL";
              // rs = stmt.executeQuery(sql); //throws SQLExecption if fails
              // printResultSet(rs);
               DriverManager.setLogWriter(new PrintWriter(System.out) );
               Connection con = DriverManager.getConnection("jdbc:mySql://localhost:3306/Contest","root","blink182");
               // Get the DatabaseMetaData object to display database info
               DatabaseMetaData dmd = con.getMetaData();
               String sql;
                 Statement stmt = con.createStatement();
             sql = "SELECT Username FROM Contestant WHERE Username IS NOT NULL";
             rs = stmt.executeQuery(sql); //throws SQLExecption if fails
            ResultSetMetaData rsmd = rs.getMetaData();
            int numCols = rsmd.getColumnCount();*
            // Display data, fetching until end of the result set
            // Calling next moves to first or next row and returns true if success
            while(rs.next() )
               // Each rs after next() contains next rows data
               for(final int i=1; i<=numCols; i++)
                  if(i > 1) System.out.print(",");
                  // Almost all SQL types can be cast to a string by JDBC
                 // System.out.print(rs.getString(i));
                  theList.setModel(new javax.swing.AbstractListModel() {
                      String[] strings = { rs.getString(i) };
                      public int getSize() { return strings.length; }
                      public Object getElementAt(int i) { return rs.getString(i); }
               System.out.println("");
            catch(Exception f)
            titleLabel = new javax.swing.JLabel();
            jScrollPane1 = new javax.swing.JScrollPane();
           // theList.setVisibleRowCount(6);
            theList = new javax.swing.JList();
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            titleLabel.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
            titleLabel.setText("The Scrupulous Contestants!");
           /* theList.setModel(new javax.swing.AbstractListModel() {
                String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "Item 3", "Item 4", "Item 5", "Item 4", "Item 5"  };
                public int getSize() { return strings.length; }
                public Object getElementAt(int i) { return strings; }
    jScrollPane1.setViewportView(theList);
    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGap(70, 70, 70)
    .addComponent(titleLabel))
    .addGroup(layout.createSequentialGroup()
    .addContainerGap()
    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 215, javax.swing.GroupLayout.PREFERRED_SIZE)))
    .addContainerGap(71, Short.MAX_VALUE))
    layout.setVerticalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addContainerGap()
    .addComponent(titleLabel)
    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addContainerGap(124, Short.MAX_VALUE))
    pack();
    }// </editor-fold>
    /* private static void printResultSet(ResultSet rs) throws SQLException
         DriverManager.setLogWriter(new PrintWriter(System.out) );
    Connection con = DriverManager.getConnection("jdbc:mySql://localhost:3306/Contest","root","blink182");
    // Get the DatabaseMetaData object to display database info
    DatabaseMetaData dmd = con.getMetaData();
    String sql;
         Statement stmt = con.createStatement();
    sql = "SELECT Username FROM Contestant WHERE Username IS NOT NULL";
    rs = stmt.executeQuery(sql); //throws SQLExecption if fails
    ResultSetMetaData rsmd = rs.getMetaData();
    int numCols = rsmd.getColumnCount();
    // Display data, fetching until end of the result set
    // Calling next moves to first or next row and returns true if success
    while(rs.next() )
    // Each rs after next() contains next rows data
    for(int i=1; i<=numCols; i++)
    if(i > 1) System.out.print(",");
    // Almost all SQL types can be cast to a string by JDBC
    System.out.print(rs.getString(i));
    System.out.println("");
    public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    new Contestantlist().setVisible(true);
    // Variables declaration - do not modify
    private javax.swing.JList theList;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel titleLabel;
    // End of variables declaration
    I can tell that im not using something correctly. Can you guys just point out how i should proceed through this? I encapsulated the piece of code that has got me confused.
    Edited by: 860597 on May 29, 2011 6:29 AM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

    package networks;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.PrintWriter;
    import java.sql.*;
    public class Contestantlist extends javax.swing.JFrame {
        public Contestantlist() {
            initComponents();
        @SuppressWarnings("unchecked")
        private void initComponents() {
             try
               Class.forName("org.gjt.mm.mysql.Driver");
            catch (ClassNotFoundException e)
               System.out.println("Unable to load driver class");
               return;
            try
                 ResultSet rs = null;
               DriverManager.setLogWriter(new PrintWriter(System.out) );
               Connection con = DriverManager.getConnection("jdbc:mySql://localhost:3306/Contest","root","blink182");
               DatabaseMetaData dmd = con.getMetaData();
               String sql;
                 Statement stmt = con.createStatement();
             sql = "SELECT Username FROM Contestant WHERE Username IS NOT NULL";
             rs = stmt.executeQuery(sql);
             printResultSet(rs);
            ResultSetMetaData rsmd = rs.getMetaData();
            int numCols = rsmd.getColumnCount();
            catch(Exception f)
            titleLabel = new javax.swing.JLabel();
            jScrollPane2 = new javax.swing.JScrollPane();
            jTable1 = new javax.swing.JTable();
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            titleLabel.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
            titleLabel.setText("The Scrupulous Contestants!");
            jTable1.setModel(new javax.swing.table.DefaultTableModel(
                new Object [][] {
                new String [] {
                    "Title 1", "Title 2", "Title 3", "Title 4"
            jScrollPane2.setViewportView(jTable1);
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(0, 0, 0)
                            .addComponent(titleLabel))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(0, 0, 0)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 454, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addContainerGap(33, Short.MAX_VALUE))
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(titleLabel)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(263, Short.MAX_VALUE))
            pack();
        }// </editor-fold>/ </editor-fold>
        private static void printResultSet(ResultSet rs) throws SQLException
               DriverManager.setLogWriter(new PrintWriter(System.out) );
              Connection con = DriverManager.getConnection("jdbc:mySql://localhost:3306/Contest","root","blink182");
              DatabaseMetaData dmd = con.getMetaData();
              String sql;
                Statement stmt = con.createStatement();
            sql = "SELECT Username FROM Contestant WHERE Username IS NOT NULL";
            rs = stmt.executeQuery(sql); //throws SQLExecption if fails
           ResultSetMetaData rsmd = rs.getMetaData();
           int numCols = rsmd.getColumnCount();
           while(rs.next() )
              for(int i=1; i<=numCols; i++)
                 if(i > 1) System.out.print(",");
                 System.out.print(rs.getString(i));
              System.out.println("");
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new Contestantlist().setVisible(true);
        private javax.swing.JList theList;
        private javax.swing.JScrollPane jScrollPane1;
        private javax.swing.JLabel titleLabel;
        private javax.swing.JScrollPane jScrollPane2;
        private javax.swing.JTable jTable1;
    }I have amended the code and now it does print out the specified contents of my table.
    I have added a a JTable, but excuse my ignorance because i have no idea as to how to proceed from here. I can't figure out how call database entries from the array in the JTable.
    Edited by: 860597 on May 29, 2011 7:27 AM

  • Refreshing JLIst

    I'm having a bit of trouble refreshing JList
    I have a file that stores an arraylist of objects. What happens when the program starts up is the arraylist is deserialized, converted to an array, and then displayed in the JList. I also have 3 other classes which are accessed via buttons, and those 3 deserialize the arraylist, modify it, and save it back to the file. One class adds an object, one edits(replaces) and object, and one deletes. How do I get my JList to update after a change is made to the file?

    You update the model of the JList.

  • Can JTable cell acts as JList?

    I was planning to create an Event Calendar view in Monthly. Currently my jTable cell extends JTextArea. My problem is that, for example, today I got 2 events display in one cell, when a user double-click the "today" cell, an edit form will pop out, if there are 2 events in one cell, how I know which events to be edit? In this case I was thinking to use JList instaed of jTextArea. so i can choose which event to be edit. Is there any example showing how to implement jTable with jList cell? or is there any other method to overcome it?

    I haven't used a JList in a JTable so far, but for a JComboBox you find a working example in the tutorial. That may give you some ideas.
    What I did use is a (read only) JTable in a JTable. So if you don't achieve your goal with JList there are alternatives.
    But wait for our table specialists to come along.

  • How can i apply different color for each cell in jlist component?

    hi guys i need your help and sorry if my english is very bad..guys how can i apply different colors for each cell in jlist component? for example for the first cell i want to apply color blue, for the second cell i want red, etc... i need your answer guys tnx..
    Edited by: ryrene on Mar 21, 2010 12:13 AM

    ryrene wrote:
    hi guys i need your help and sorry if my english is very bad.
    ..guys how can i apply different colors for each cell in jlist component? for example for the first cell i want to apply color blue, for the second cell i want red, etc... i need your answer guys tnx..I'll see your tnx, and raise you dve bmbrs & nplm.

  • How to set mutable cell width and height in a jlist?

    Hello,
    i wrote a jlist cell renderer object to able to render a jlabel for the unselected items and a wide and high jpanel with images when an item is selected.
    The jlist render all items at the same dimension.
    How can i change this built-in behavior?
    i printed the items preferedsize:
    java.awt.Dimension[width=340,height=140]
    java.awt.Dimension[width=275,height=75]
    java.awt.Dimension[width=275,height=75]
    java.awt.Dimension[width=275,height=75]
    java.awt.Dimension[width=275,height=75]
    java.awt.Dimension[width=275,height=75]
    java.awt.Dimension[width=275,height=75]
    Message was edited by:
    fazekaim
    ok, i give for every index pos a fix jpanel object, and it is working.
    What i would like is a mouse selection and the selected item/cell should be rendered by a component with different dimension (containing more information). but this doesn't work.
    Is it possible, that the JList cashes the cells dimensions???
    Message was edited by:
    fazekaim
    Message was edited by:
    fazekaim

    i just want to change a cell for another when i click on it.
    there are names in the jlist. after having click on one of them, a description panel could be appears in the list instead of the short name. it' a simple ui effect, i think.
    is there any other way than write a custom jlist implementation?

  • Drag a JLabel, drop in a JList

    I'm trying to implement a drop-and-drag feature in my program.
    I have a class Book, which extends a JLabel with and ID field (as a string) and a get and set method for the ID in it, as well as a toString method which returns the ID.
    I need to drag the label over a JList and add the label's ID to the list, directly below the selected index over which the mouse dropped.
    So far i have tried to override the transferhandler with example String and List handlers i found on the site (at http://java.sun.com/docs/books/tutorial/uiswing/misc/example-1dot4/ListTransferHandler.java and http://java.sun.com/docs/books/tutorial/uiswing/misc/example-1dot4/StringTransferHandler.java ) however, it seems the problem keeps getting bigger the more i try to edit them.
    Also, i have tried to implement Serializable and Transferable Book's, but I'm not entirely familiar with how to use the dataflavors, so that could also be causing the problem.
    I have tried a few possible scenarios with the transfer handler and so far this is getting me the most results:
    public class dragtest extends JPanel {
    public dragtest() {
    super(new GridLayout(3,1));
    add(createArea());
    add(createList());
    add(createBookcase(17));
    private JPanel createList() {
    DefaultListModel listModel = new DefaultListModel();
    listModel.addElement("List one");
    listModel.addElement("List two");
    listModel.addElement("List three");
    JList list = new JList(listModel);
    list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
    JScrollPane scrollPane = new JScrollPane(list);
    scrollPane.setPreferredSize(new Dimension(400,100));
    // do not allow list items to be dragged
    list.setDragEnabled(false);
    // list.setDropTarget();
    // DropTarget dt = new DropTarget();
    // list.setDropTarget(dt);
    list.setTransferHandler(new ListTransferHandler());
    JPanel panel = new JPanel(new BorderLayout());
    panel.add(scrollPane, BorderLayout.CENTER);
    panel.setBorder(BorderFactory.createTitledBorder("List"));
    return panel;
    private JPanel createBookcase(int amount) {
    JPanel bookCase = new JPanel();
    int X1 = 30; // height from top
    int X2 = 20; // height to bottom
    int Y1 = 20; // distance from sides
    int Y2 = 10; // distance between images
    int Z1 = 60; // width of image
    int Z2 = 80; // height of image
    int numLabelsInCol = 5;
    int numDone = 0;
    int x = 0;
    for (int y = 0; y < numLabelsInCol; y++) {
    if (numDone < amount) {
    Book lbl = new Book(numDone, "address");
    lbl.setBounds( (Y1 + y * (Z1 + Y2)), (X1 + x * (Z2 + X2 + X1)), Z1, Z2);
    bookCase.add(lbl);
    String text = lbl.ID;
    lbl.setText(text);
    numDone++;
    lbl.setTransferHandler(new TransferHandler("text"));
    MouseListener listener = new DragMouseAdapter();
    lbl.addMouseListener(listener);
    bookCase.setPreferredSize(new Dimension(400, 100));
    return bookCase;
    private class DragMouseAdapter extends MouseAdapter { public void mousePressed(MouseEvent e) {
    JComponent c = (JComponent) e.getSource();
    TransferHandler handler = c.getTransferHandler();
    handler.exportAsDrag(c, e, TransferHandler.COPY);
    I realise it might be a little unconventional, the way i implement the transfer handler, but this actually prints out the ID on a JTextArea, however a list does not even react when i drag or drop the label over it. It does not even show a shaded area under which the mouse is.
    I have tried a multitude of examples but none of them have dealt with this. The only one which deals with JLabels dropped on a JList does not care for the order, however my program focuses on it.
    Please could anyone help?

    1) Swing related questions should be posted in the Swing forum
    2) When you poste code use the "code" formatting tags so the code retains its original formatting
    Using the ExtendedDnDDemo, from the Swing tutorial you referenced above, as the test program I created the following method:
        private JPanel createLabel() {
             JLabel label = new JLabel("Dnd Label");
            label.setTransferHandler(new LabelTransferHandler());
            label.addMouseListener( new MouseAdapter()
                 public void mousePressed(MouseEvent e)
                     JComponent c = (JComponent)e.getSource();
                     TransferHandler handler = c.getTransferHandler();
                     handler.exportAsDrag(c, e, TransferHandler.COPY);
            JPanel panel = new JPanel(new BorderLayout());
            panel.add(label, BorderLayout.CENTER);
            panel.setBorder(BorderFactory.createTitledBorder("Label"));
            return panel;
        }And the following class:
    * LabelTransferHandler.java is used by the 1.4
    * ExtendedDnDDemo.java example.
    import javax.swing.*;
    import java.awt.*;
    import java.awt.datatransfer.*;
    public class LabelTransferHandler extends StringTransferHandler {
        // Get the text for export.
        protected String exportString(JComponent c) {
            JLabel label = (JLabel)c;
            return label.getText();
        //Take the incoming string and set the text of the label
        protected void importString(JComponent c, String str) {
            JLabel label = (JLabel)c;
            label.setText(str);
        protected void cleanup(JComponent c, boolean remove) {
    }

Maybe you are looking for