Convert the following program so that it uses JList instead

Convert the following program so that it uses JList instead of JComboBox.

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import javax.swing.*;


public class ShoppingList extends JFrame {

   private static final long myShoppingList = 1L;

   // Constants for window width and height

   private final int WINDOW_WIDTH = 700;

   private final int WINDOW_HEIGHT = 250;

   private JTextField shoppingList;

   // Buttons for Save, Add, Remove, and Load features.

   private JButton Save;

   private JButton Add;

   private JButton Remove;

   private JButton Load;

   // Panels for the button groups

   private JPanel textfieldPanel;

   private JPanel buttonPanel;

   private JComboBox<String> itemList;

   /**

   * CONSTRUCTOR

   */

   public ShoppingList() {

       // Window title

       setTitle(\"Shopping List\");

       // Set size

       setSize(WINDOW_WIDTH, WINDOW_HEIGHT);

       // Default close operation

       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       // Calling buildPanel method

       buildPanels();

       // Grid Layout

       setLayout(new FlowLayout());

       // Adding the panel to JFrame

       add(textfieldPanel);

       add(buttonPanel);

       // Visibility

       setVisible(true);

   }

   // Building the Panels,

   private void buildPanels() {

       // Initializing the Textfield

       shoppingList = new JTextField(20);

       // Initializing the Buttons

       Save = new JButton(\"Save\");

       Add = new JButton(\"Add\");

       Remove = new JButton(\"Remove\");

       Load = new JButton(\"Load\");

       // Associating action listener

       Add.addActionListener(new AddButtonListener());

       Save.addActionListener(new SaveButtonListener());

       Remove.addActionListener(new RemoveButtonListener());

       Load.addActionListener(new LoadButtonListener());

       try {

           itemList = new JComboBox<>(fileToArray());

        

       } catch (IOException e) {

           itemList = new JComboBox<>();

       }

    

       itemList.setPreferredSize(new Dimension(10,20));

       // Initializing the panels

       textfieldPanel = new JPanel();

       buttonPanel = new JPanel();

       // Seting borders of the panels

       textfieldPanel.setBorder(BorderFactory

               .createTitledBorder(\"Shopping List\"));

       buttonPanel.setBorder(BorderFactory.createTitledBorder(\"Process\"));

       // Adding the objecs to the respective panels

       textfieldPanel.setLayout(new BoxLayout(textfieldPanel, BoxLayout.Y_AXIS));

       textfieldPanel.add(shoppingList);

       textfieldPanel.add(itemList);

       buttonPanel.add(Save);

       buttonPanel.add(Add);

       buttonPanel.add(Remove);

       buttonPanel.add(Load);

   }// End buildPanels

   private class AddButtonListener implements ActionListener {

       public void actionPerformed(ActionEvent addaction) {

           if (!shoppingList.getText().trim().equals(\"\"))

               itemList.addItem(shoppingList.getText().trim().toLowerCase());

       }

   }//end listener

   private class RemoveButtonListener implements ActionListener {

       public void actionPerformed(ActionEvent removeaction) {

           if (itemList.getItemCount() > 0)

               itemList.removeItemAt(itemList.getSelectedIndex());
         
           else {
               JOptionPane.showMessageDialog(null, \"Please select an item to delete!\");
           }

       }

   }//end listener

   private class SaveButtonListener implements ActionListener {

       public void actionPerformed(ActionEvent saveaction) {

           try {

               writeToFile();

           }

           catch (Exception n) {

               System.out.println(\"File not created!\");

           }

       }

   }//end listener

   private void writeToFile() throws FileNotFoundException, IOException {

       PrintWriter inFile = new PrintWriter(new File(\"list.txt\"));

       FileWriter writer = (new FileWriter(\"list.txt\", true));

       writer.write(shoppingList.getText().trim().toLowerCase());

       writer.write(\"\ \");

       writer.close();

       inFile.close();

   }

   private class LoadButtonListener implements ActionListener {

       public void actionPerformed(ActionEvent loadaction) {

           try {

               for (String item : fileToArray()) {

                   itemList.addItem(item);

               }

           }

           catch (Exception o)

           {

               JOptionPane.showMessageDialog(null, \"Error opening data file\");

           }

       }

   }//end listener

   private String[] fileToArray() throws IOException {

       Path filePath = new File(\"list.txt\").toPath();

       Charset charset = Charset.defaultCharset();

       List<String> stringList = Files.readAllLines(filePath, charset);

       return stringList.toArray(new String[] {});

   }

   public static void main(String[] args) throws IOException {

       new ShoppingList();

   }

}// end main

Solution

import java.awt.*;

import java.awt.event.*;

import java.util.*;

import javax.swing.*;

public class ComboBoxTwo extends JPanel implements ActionListener

{

private JComboBox<String> mainComboBox;

private JComboBox<String> subComboBox;

private Hashtable<String, String[]> subItems = new Hashtable<String, String[]>();

public ComboBoxTwo()

{

String[] items = { \"Select Item\", \"Color\", \"Shape\", \"Fruit\" };

mainComboBox = new JComboBox<String>( items );

mainComboBox.addActionListener( this );

// prevent action events from being fired when the up/down arrow keys are used

mainComboBox.putClientProperty(\"JComboBox.isTableCellEditor\", Boolean.TRUE);

add( mainComboBox );

// Create sub combo box with multiple models

subComboBox = new JComboBox<String>();
subComboBox.setPrototypeDisplayValue(\"XXXXXXXXXX\"); // JDK1.4

add( subComboBox );

String[] subItems1 = { \"Select Color\", \"Red\", \"Blue\", \"Green\" };

subItems.put(items[1], subItems1);

String[] subItems2 = { \"Select Shape\", \"Circle\", \"Square\", \"Triangle\" };

subItems.put(items[2], subItems2);

String[] subItems3 = { \"Select Fruit\", \"Apple\", \"Orange\", \"Banana\" };

subItems.put(items[3], subItems3);

}

public void actionPerformed(ActionEvent e)

{

String item = (String)mainComboBox.getSelectedItem();

Object o = subItems.get( item );

if (o == null)

{

subComboBox.setModel( new DefaultComboBoxModel() );

}

else

{

subComboBox.setModel( new DefaultComboBoxModel( (String[])o ) );

}

}

private static void createAndShowUI()

{

JFrame frame = new JFrame(\"SSCCE\");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.add( new ComboBoxTwo() );

frame.setLocationByPlatform( true );

frame.pack();

frame.setVisible( true );

}

\\

public static void main(String[] args)

{

EventQueue.invokeLater(new Runnable()

{

public void run()

{

createAndShowUI();

}

});
}

}

Convert the following program so that it uses JList instead of JComboBox. import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionList
Convert the following program so that it uses JList instead of JComboBox. import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionList
Convert the following program so that it uses JList instead of JComboBox. import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionList
Convert the following program so that it uses JList instead of JComboBox. import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionList
Convert the following program so that it uses JList instead of JComboBox. import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionList
Convert the following program so that it uses JList instead of JComboBox. import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionList

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site