How to make a scrollable JList to add details got from a JOptionPane
Asked Answered
M

3

5

I want to repeatedly take input from the user(probably using a button) via a JOptionPane(already done) and store the details in something(how about a dynamic object array) and display this information as a list in a scrollable JList.


MY CODE


import java.awt.GridLayout;
import javax.swing.*;

class Flight {



public static void main(String[] args) {

    //Panel
    JPanel panel = new JPanel(new GridLayout(7, 2,20, 20));

    //Add textfields here
    JTextField txtflightno = new JTextField(8);
    JTextField txtmechanicalstatus = new JTextField(8);
    JTextField txtmedicalstatus = new JTextField(8);
    JTextField txtfuellevel = new JTextField(8);
    JTextField txtweathercondition = new JTextField(8);
    JTextField txtfrequency = new JTextField(8);
    JTextField txtflightpath = new JTextField(8);






    //Add labels here
    JLabel lblflightno = new JLabel("Flight No : ");
    JLabel lblmechanicalstatus = new JLabel("Mechanical Status:");
    JLabel lblmedicalstatus = new JLabel("Medical Status:");
    JLabel lblfuellevel = new JLabel("Fuel Level:");
    JLabel lblweathercondition = new JLabel("Weather Condition:");
    JLabel lblfrequency = new JLabel("Frequency:");
    JLabel lblflightpath = new JLabel("Flight Path:");





    //Adding flightno to panel
    panel.add(lblflightno);
    panel.add(txtflightno);

    //Adding mechanicalstatus to the panel
    panel.add(lblmechanicalstatus);
    panel.add(txtmechanicalstatus);

    //Adding medicalstatus to the panel
    panel.add(lblmedicalstatus);
    panel.add(txtmedicalstatus);

    //Adding fuellevel to the panel
    panel.add(lblfuellevel);
    panel.add(txtfuellevel);

    //Adding weathercondition to the panel
    panel.add(lblweathercondition);
    panel.add(txtweathercondition);

    //Adding frequency to the panel
    panel.add(lblfrequency);
    panel.add(txtfrequency);

    //Adding flightpath to the panel
    panel.add(lblflightpath);
    panel.add(txtflightpath);


    panel.setBounds(0, 0, 800, 600);




   int result = JOptionPane.showConfirmDialog(null, panel, "Flight Details",
        JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
    if (result == JOptionPane.OK_OPTION) {

    }
}
}

How must I do the storing of the plane details ? How must I implement a scrollable JList ? Any suggestions.

Many Thanks

Maharajah answered 8/7, 2010 at 5:22 Comment(1)
You've got some good answers here, so you should mark one of them as accepted.Hydrostatic
A
11

As discussed in How to Use Lists, JList uses a list model as the source of data it displays. Just add your data to a DefaultListModel, and use it to construct your list.

DefaultListModel dlm = new DefaultListModel();
// add data
JList list = new JList(dlm);
panel.add(new JScrollPane(list));
Argyll answered 8/7, 2010 at 5:55 Comment(0)
T
4

To make the JList scrollable, simply embed it in a JScrollPane. Instead of

add(myList, constraints);

do

add(new JScrollPane(myList), constraints);

To extend the list, just get the JList's ListModel (using getListModel) and use add to add objects.

More on using ListModels in Sun's tutorial.

More on ScrollPane in the Tutorial, too.

Tatianatatianas answered 8/7, 2010 at 5:58 Comment(1)
you mean JScrollPane?Leodora
I
0

You've to use the ActionListener of Button, that you've missed in the code snippet.

In the OK Option :

JList jlist = ...;

jlist.add(txtflightno.getText());
jlist.add(txtmechanicalstatus.getText());
jlist.add(txtmedicalstatus.getText());
....
....

& 

add(new JScrollPanel(myList), constraints);

After this use validate method of Component to update the list with this new item. But one thing you should remember is that list displays each item row-wise. I suggest you to use JTable with which you can display your items in a meaningful way...

Intoxicative answered 8/7, 2010 at 5:58 Comment(1)
coder: I don't see where @Maharajah needs an ActionListener; the JOptionPane's OK button should do. Also, JList can't accept data directly; a ListModel should be used instead.Argyll

© 2022 - 2024 — McMap. All rights reserved.