Forum Stats

  • 3,768,479 Users
  • 2,252,798 Discussions
  • 7,874,596 Comments

Discussions

NetBeans: Adding Elements in a list Box

User_AYF65
User_AYF65 Member Posts: 135 Red Ribbon
edited Jul 10, 2016 2:56PM in New To Java

Hi,

I am using NetBeans 8.1. I have created a list using swing component. I have a text box (variable name :TF) & 3 buttons. button1 is variable corresponding to the add button. I want to write its handler. I dont know what is the method of list box which can add elements in the list box.

When I press the add button, the data in text field should store in the list box. My variables are:

private javax.swing.JButton button1;

    private javax.swing.JButton button2;

    private javax.swing.JButton button3;

    private javax.swing.JLabel jLabel1;

    private javax.swing.JPanel jPanel1;

    private javax.swing.JScrollBar jScrollBar1;

    private javax.swing.JScrollPane jScrollPane1;

    private javax.swing.JTextField jTF;

    private javax.swing.JList<String> list;

I cant find the addElement ( ) method to add the string from the textbox into the list.

private void button1ActionPerformed(java.awt.event.ActionEvent evt) {                                       

        // TODO add your handling code here:

        String str=jTF.getText();

        list.???

    }    

Some body please guide me about the method to addElement in list box.

Zulfi.

Best Answer

  • User_AYF65
    User_AYF65 Member Posts: 135 Red Ribbon
    edited Jul 10, 2016 2:56PM Accepted Answer

    Hi,

    I am able to solve this problem. I checked a video on You-tube & i got the solution. Thanks everybody. The solution lies in clearing the initially created list box & this is not mentioned in the tutorial. After creating the list box, i went to properties->model & erased the contents across this field. After that my program ran correctly. Kindly tune the documentation for NetBeans users.

    My code corresponding to the two Buttons are:

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        

            // TODO add your handling code here:

           

            for (int i=0;i<5; ++i)

                model.addElement("Zulfi item" +i);

            jList1.setModel(model);

        }                                       

        private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                        

            // TODO add your handling code here:

            String str=jTextField1.getText();

            model.addElement(str);

            jList1.setModel(model);

        }                           

    & the constructor is:

    public class JavaListBox1 extends javax.swing.JFrame {

    private DefaultListModel model;

        /**

         * Creates new form JavaListBox1

         */

        public JavaListBox1() {

           model = new DefaultListModel();

            initComponents();

        }

    Variables corresponding to the components are:

    private javax.swing.JButton jButton1;

        private javax.swing.JButton jButton2;

        private javax.swing.JList<String> jList1;

        private javax.swing.JScrollPane jScrollPane1;

        private javax.swing.JTextField jTextField1;

    Zulfi.

Answers

  • Jiri.Machotka-Oracle
    Jiri.Machotka-Oracle Member Posts: 5,078
    edited Jul 4, 2016 4:21AM

    Take a look at the API for JList: https://docs.oracle.com/javase/7/docs/api/javax/swing/JList.html

    There is a method named add. I believe it's what you are looking for.

  • User_AYF65
    User_AYF65 Member Posts: 135 Red Ribbon
    edited Jul 4, 2016 10:22AM

    Hi,

    Thanks. I cant find the add method. Please provide me its description.

    Zulfi.

  • Jiri.Machotka-Oracle
    Jiri.Machotka-Oracle Member Posts: 5,078
    edited Jul 4, 2016 10:34AM

    It's among methods inherited from class java.awt.Container or even java.awt.Component.


    https://docs.oracle.com/javase/7/docs/api/java/awt/Container.html

  • User_AYF65
    User_AYF65 Member Posts: 135 Red Ribbon
    edited Jul 10, 2016 2:56PM Accepted Answer

    Hi,

    I am able to solve this problem. I checked a video on You-tube & i got the solution. Thanks everybody. The solution lies in clearing the initially created list box & this is not mentioned in the tutorial. After creating the list box, i went to properties->model & erased the contents across this field. After that my program ran correctly. Kindly tune the documentation for NetBeans users.

    My code corresponding to the two Buttons are:

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        

            // TODO add your handling code here:

           

            for (int i=0;i<5; ++i)

                model.addElement("Zulfi item" +i);

            jList1.setModel(model);

        }                                       

        private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                        

            // TODO add your handling code here:

            String str=jTextField1.getText();

            model.addElement(str);

            jList1.setModel(model);

        }                           

    & the constructor is:

    public class JavaListBox1 extends javax.swing.JFrame {

    private DefaultListModel model;

        /**

         * Creates new form JavaListBox1

         */

        public JavaListBox1() {

           model = new DefaultListModel();

            initComponents();

        }

    Variables corresponding to the components are:

    private javax.swing.JButton jButton1;

        private javax.swing.JButton jButton2;

        private javax.swing.JList<String> jList1;

        private javax.swing.JScrollPane jScrollPane1;

        private javax.swing.JTextField jTextField1;

    Zulfi.

This discussion has been closed.