ListModel: getElementAt(int index)

ListModel: getElementAt(int index)

ListModel: getElementAt(int index)
ListModel: getElementAt(int index)  method is used to get a specific element at a specified Index x.
Other JList methods and Listener you should check out are ListSelectionListener , Java JList:getModel

ListModel: getElementAt(int index) Example

[
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
public class JavaJListExample extends JFrame  {
    public JList list;

    JavaJListExample()
    {
        super("JList");

        String language []={"Java","C++","PHP","Python","C#"};
        list=new JList();
        list.setListData(language);
        list.setVisibleRowCount(3);

        JScrollPane pane=new JScrollPane(list);
        pane.setBounds(50, 20, 100, 50);
        add(pane);

        list.addMouseListener(new MouseAdapter()
        {
            public void mouseClicked(MouseEvent event)
            {
                list=(JList)event.getSource();
                if(event.getClickCount()==2)
                {
                    int i=list.locationToIndex(event.getPoint());
                    if(i>=0)
                    {
                        Object object=list.getModel().getElementAt(i);
                        String s=object.toString();
                        JOptionPane.showMessageDialog(null,"You Double clicked on "+ s,
                                "JList getElement",JOptionPane.INFORMATION_MESSAGE);

                    }
                }

            }
        });



        setSize(400,400);
        setLayout(null);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }

    public static void main(String args[])
    {
        new JavaJListExample();

    }

}
]

ListModel: getElementAt(int index) Code Explanation

When user double clicks on a item in JList ..this code is executed[ list.addMouseListener(new MouseAdapter()
        {
            public void mouseClicked(MouseEvent event)
            {
                list=(JList)event.getSource();
                if(event.getClickCount()==2)
                {
                    int i=list.locationToIndex(event.getPoint());
                    if(i>=0)
                    {
                        Object object=list.getModel().getElementAt(i);
                        String s=object.toString();
                        JOptionPane.showMessageDialog(null,"You Double clicked on "+ s,
                                "JList getElement",JOptionPane.INFORMATION_MESSAGE);

                    }
                }

            }
        });
]

A JOptionPane showMessage Dialog pops up displaying the item that has been double-clicked.

Other methods used in this program 


See also:


Java JList:getModel() method

JList addJListSelectionListener


COMMENTS

Name

android android Apps android emulators antivirus blogger bluestack Break Statement in Java constructors download downloader drivers emulators java java control statements java do-while loop java for loop java infinitive do-while loop java system methods java tutorials Java While Loop java.awt java.swing javalookandfeel javascript JButton JCheckBox JColorChooser JComboBox JDialog JFileChooser JFrame JLayeredPane JList JMenuBar joomla JOptionPane JPanel JPasswordField JPopupMenu JProgressBar JRadioButton JScrollBar JSlider JSpinner JTabbedPane JTable JTextArea JTextField JToolBar mysql php plugins Polymorphism setToolTiptext skype for windows software swing class themes Visual Studio Code WeChat whatsapp wordpress
false
ltr
item
Java Codes: ListModel: getElementAt(int index)
ListModel: getElementAt(int index)
ListModel: getElementAt(int index)
https://2.bp.blogspot.com/-q8lEQ6an-Jw/WMbGLJa9DjI/AAAAAAAAHKg/CiQ9ZtKu0CUGSO1y4GXzOzQDYdNaHS8ZQCLcB/s200/ListModel-getElementAt-example-min.PNG
https://2.bp.blogspot.com/-q8lEQ6an-Jw/WMbGLJa9DjI/AAAAAAAAHKg/CiQ9ZtKu0CUGSO1y4GXzOzQDYdNaHS8ZQCLcB/s72-c/ListModel-getElementAt-example-min.PNG
Java Codes
http://www.download-all.net/2017/03/listmodel-getelementatint-index.html
http://www.download-all.net/
http://www.download-all.net/
http://www.download-all.net/2017/03/listmodel-getelementatint-index.html
true
7826648292785071009
UTF-8
Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy