Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Program - GUI help - Create an interface for the user to add, remove, sort,

ID: 3817887 • Letter: J

Question

Java Program - GUI help

- Create an interface for the user to add, remove, sort, filter, save and load information from our flower pack.
-You should USE ALL of the following at least once in your interface:
       JFrame
       JButton
       JLabel
       JTextField
       JCheckBox / JRadioButton
       Layout (Flow, grid,or border layouts)
       JMenu
       JMenuItem

-The flower pack should hold flowers, weeds, fungus, and now herbs. All should be a subclass of a plant class.
-Each subclass shares certain qualities (ID, Name, and Color)
-Flower traits include (Thorns, and Smell)
-Fungus traits include (Poisonous)
-Weed traits include (Poisonous, Edible and Medicinal)
-Herb traits include (Flavor, Medicinal, Seasonal)

Use the code below:

package flowerpack;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Scanner;

public class FlowerPack {
   public static void main(String[] args) {
       try{
           ArrayList flowerPack= new ArrayList<>();
           Scanner input=new Scanner(System.in);
           int choice;
           String name, color, ID;
           boolean poisonous,edible,medicinal,smell,thorns;
           while(true)
           {
               System.out.println(" 1. Add plants");
               System.out.println("2. Remove plants");
               System.out.println("3. Search plants");
               System.out.println("4. Filter plants");
               System.out.println("5. Save to file");
               System.out.println("6. Load from file");
               System.out.println("7. Quit");
               System.out.print("Enter your choice: ");
               choice=input.nextInt();
               switch(choice)
               {
               case 1:
                   input.nextLine();
                   System.out.print("You want to add Flower, Fungus or Weed? ");
                   String type=input.nextLine();
                   if(type.equalsIgnoreCase("Flower"))
                   {
                       System.out.print("Enter name : ");
                       name=input.nextLine();
                       System.out.print("Enter ID : ");
                       ID=input.nextLine();
                       System.out.print("Enter color : ");
                       color=input.nextLine();
                       System.out.print("Has thorns? ");
                       thorns=input.hasNextBoolean();
                       input.nextLine();
                       System.out.print("Has smell? ");
                       smell=input.hasNextBoolean();
                       input.nextLine();
                       flowerPack.add(new Flower(name,ID,color,thorns,smell));
                   }
                   else if(type.equalsIgnoreCase("Fungus"))
                   {
                       System.out.print("Enter name : ");
                       name=input.nextLine();
                       System.out.print("Enter ID : ");
                       ID=input.nextLine();
                       System.out.print("Enter color : ");
                       color=input.nextLine();
                       System.out.print("Is it poisonous? ");
                       poisonous=input.hasNextBoolean();
                       input.nextLine();
                       flowerPack.add(new Fungus(name,ID,color,poisonous));
                   }
                   else if(type.equalsIgnoreCase("Weed"))
                   {
                       System.out.print("Enter name : ");
                       name=input.nextLine();
                       System.out.print("Enter ID : ");
                       ID=input.nextLine();
                       System.out.print("Enter color : ");
                       color=input.nextLine();
                       System.out.print("Is it Poisonous? ");
                       poisonous=input.hasNextBoolean();
                       input.nextLine();
                       System.out.print("Is it Edible? ");
                       edible=input.hasNextBoolean();
                       input.nextLine();
                       System.out.print("Is it Medicinal? ");
                       medicinal=input.hasNextBoolean();
                       input.nextLine();
                       flowerPack.add(new Weed(name,ID,color,poisonous,edible,medicinal));
                   }
                   break;
               case 2:input.nextLine();
               System.out.print("Enter the name of the plant you want to remove : ");
               name=input.nextLine();
               int flag=0;
               for(Plant plant:flowerPack)
               {
                   if(plant.getName().equalsIgnoreCase(name))
                   {
                       System.out.println("plant removed sucessfully") ;
                       flag=1;
                       break;
                   }
               }
               if(flag==0)
               {
                   System.out.println("plant not found") ;
               }
               break;
               case 3:
                   input.nextLine();
                   System.out.print("Enter the name of the plant you want to search : ");
                   name=input.nextLine();
                   int f=0;
                   for(Plant plant:flowerPack)
                   {
                       if(plant.getName().equalsIgnoreCase(name))
                       {
                           System.out.println("plant found sucessfully") ;
                           f=1;
                           break;
                       }
                   }
                   if(f==0)
                   {
                       System.out.println("plant not found") ;
                   }
                   break;
               case 4:
                   input.nextLine();
                   System.out.print("Enter the name of the plant you want to filter: ");
                   name=input.nextLine();
                   f=0;
                   for(Plant plant:flowerPack)
                   {
                       if(plant.getName().equalsIgnoreCase(name))
                       {
                           System.out.println("Name: " + plant.getName() + " ID: " + plant.getID()) ;
                           f=1;
                       }
                   }
                   if(f==0)
                   {
                       System.out.println("NO plant of this name in List") ;
                   }
                   break;
               case 5:
                   File file = new File("Plants.txt");
                   FileOutputStream fStream = new FileOutputStream(file);
                   ObjectOutputStream stream = new ObjectOutputStream(fStream);
                   stream.writeObject(flowerPack);
                   stream.close();
                   break;
                
               case 6:
                   File file2 = new File("Plants.txt");
                   FileInputStream fileStream = new FileInputStream(file2);
                   ObjectInputStream oStream = new ObjectInputStream(fileStream);
                   ArrayList readObject = ((ArrayList)oStream.readObject());
                   flowerPack.addAll(readObject);
                   oStream.close();
                   break;
               case 7: System.exit(0);
               }
           }
       }catch(Exception e)
       {
           System.out.println(e);
       }
   }

}

*******
package flowerpack;

import java.io.Serializable;

public class Plant implements Serializable{
String name;
String ID;
Plant(String name,String ID)
{
this.name=name;
this.ID=ID;
}
public String getName()
{
return name;
}
public String getID()
{
return ID;
}

********
package flowerpack;

class Fungus extends Plant
{
String Color;
boolean Poisonous;
public Fungus(String name, String ID,String Color,boolean Poisonous) {
super(name, ID);
this.Color=Color;
this.Poisonous=Poisonous;
}
}
*******
package flowerpack;

class Flower extends Plant
{
String color;
boolean thorns;
boolean smell;
public Flower(String name, String ID,String color,boolean thorns,boolean smell) {
super(name, ID);
this.color=color;
this.thorns=thorns;
this.smell=smell;
}
}
*********
package flowerpack;

class Weed extends Plant
{
String Color;
boolean Poisonous;
boolean Edible;
boolean Medicinal;
public Weed(String name, String ID,String Color,boolean Poisonous,boolean Edible, boolean Medicinal) {
super(name, ID);
this.Color=Color;
this.Poisonous=Poisonous;
this.Edible=Edible;
this.Medicinal=Medicinal;
}
}

Explanation / Answer

public class ExampleIs {
private EventList<Persons> eventList = new BasicEventList<Person>();

public JTable createTable(...) { ... code to generate the table ...}

public void manipulateTable() {
// add to the table (via the eventList)
eventList.add(new Persons("Steve Jobs"));
// remove first object in the table (and the list)
eventList.remove(0);
// update a row
Persons p = eventList.get(0);
p.setName("A N Other");
eventList.set(0,p); // overwrite the old object in the list

}
}

public class MyFrame extends javax.swing.JFrame {

private EventList<Persons> eventList = new BasicEventList<Persons>();
private EventSelectionModel<Persons> selectionModel;

/**
* Creates new form MyFrame
*/
public MyFrame() {
initComponents();
loadData();
configureTable();
}

private void loadData() {

eventList.add(new Persons("Richard", "Dawkins"));
eventList.add(new Persons("Sam", "Harris"));
eventList.add(new Persons("Christopher", "Hitchens"));
eventList.add(new Persons("Daniel", "Dennett"));

}

private void configureTable() {
String[] headers = new String[]{"Firstname", "Lastname"};
String[] properties = new String[]{"firstname", "lastname"};


TextFilterator<Persons> personTextFilterator = new TextFilterator<Persons>() {

@Override
public void getFilterStrings(List list, Persons p) {
list.add(p.getFirstname());
list.add(p.getLastname());
}
};

MatcherEditor<Persons> textMatcherEditor = new TextComponentMatcherEditor<Persons>(txtFilter, personTextFilterator);

FilterList<Persons> filterList = new FilterList<Persons>(eventList, textMatcherEditor);

TableFormat tf = GlazedLists.tableFormat(properties, headers);
EventTableModel<Persons> model = new EventTableModel<Persons>(filterList, tf);

selectionModel = new EventSelectionModel<Persons>(filterList);
tblNames.setSelectionModel(selectionModel);

tblNames.setModel(model);
}

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jLabel1 = new javax.swing.JLabel();
txtFilter = new javax.swing.JTextField();
jScrollPane1 = new javax.swing.JScrollPane();
tblNames = new javax.swing.JTable();
btnDelete = new javax.swing.JButton();
btnReload = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("GlazedLists test");

jLabel1.setText("Filter");

tblNames.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
jScrollPane1.setViewportView(tblNames);

btnDelete.setText("Delete");
btnDelete.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnDeleteActionPerformed(evt);
}
});

btnReload.setText("Reload data");
btnReload.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnReloadActionPerformed(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(jLabel1)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(txtFilter))
.add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 388, Short.MAX_VALUE)
.add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
.add(btnReload)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.add(btnDelete)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(jLabel1)
.add(txtFilter, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
.add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 240, Short.MAX_VALUE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(btnDelete)
.add(btnReload))
.addContainerGap())
);

pack();
}// </editor-fold>

private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {
if (!selectionModel.isSelectionEmpty()) {
eventList.removeAll(selectionModel.getSelected());
}
}   

private void btnReloadActionPerformed(java.awt.event.ActionEvent evt) {
eventList.clear();
loadData();
}   

/**
* @param args the command line arguments
*/
public static void main(String args[]) {

/*
* Create and display the form
*/
java.awt.EventQueue.invokeLater(new Runnable() {

@Override
public void run() {
new MyFrame().setVisible(true);
}
});
}

private javax.swing.JButton btnDelete;
private javax.swing.JButton btnReload;
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable tblNames;
private javax.swing.JTextField txtFilter;
  
}

public class Persons {

private String firstname;
private String lastname;

public Persons() {
}

public Persons(String firstname, String lastname) {
this.firstname = firstname;
this.lastname = lastname;
}

public String getFirstname() {
return firstname;
}

public void setFirstname(String firstname) {
this.firstname = firstname;
}

public String getLastname() {
return lastname;
}

public void setLastname(String lastname) {
this.lastname = lastname;
}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote