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

I can\'t get my Search JButton to work correctly. I want to be able to search on

ID: 3539426 • Letter: I

Question

I can't get my Search JButton to work correctly. I want to be able to search on partial information and have it be found in my list also.

Here is my code for everything else.

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;


public class ShowJList extends JFrame {

  

private DefaultListModel show;

private JList list;

  

public ShowJList()

{

super( "Favorite shows" );

  

// create a DefaultListModel to store different sitcoms

show = new DefaultListModel();

show.addElement( "Good Luck Charlie" );

  show.addElement( "Piony" );

   show.addElement( "Asta" );

   show.addElement( "Sauanias" );

  show.addElement( "Iris" );

  show.addElement( "Tulip" );

  show.addElement( "Daffodil" );

  

list = new JList( show );

  

  

list.setSelectionMode(

ListSelectionModel.SINGLE_SELECTION );

  

// create JButton for adding

JButton addButton = new JButton( "Add Sitcomr" );

addButton.addActionListener(

new ActionListener() {

  

public void actionPerformed( ActionEvent event )

{

String name = JOptionPane.showInputDialog(

ShowJList.this, "Enter Sitcom" );

  

  

show.addElement( name );

}

}

);

  

/* JButton searchButton= new JButton("Search Sitcom");

searchButton.addActionListener(

new ActionListener(){

public void actionPerformed(ActionEvent event)

{

for( int i = 0; i < NewFlowerJList.length; i++ ) // assume it is a 1-D array

{

if (recognizeItem( userInput, ShowJList[ i ] ))

System.out.println( ShowJList[ i ] );

}


}

  

);

*/   

// create JButton for removing s

JButton removeButton =

new JButton( "Remove Show" );

  

removeButton.addActionListener(

new ActionListener() {

  

public void actionPerformed( ActionEvent event )

{

// remove

show.removeElement(

list.getSelectedValue() );

}

}

);

  

// lay out GUI components

JPanel inputPanel = new JPanel();

inputPanel.add( addButton );

inputPanel.add( removeButton );

// inputPanel.add(searchButton);

  

Container container = getContentPane();

container.add( list, BorderLayout.CENTER );

container.add( inputPanel, BorderLayout.NORTH );

  

setDefaultCloseOperation( EXIT_ON_CLOSE );

setSize( 400, 300 );

setVisible( true );

  

}

  

// execute application

public static void main( String args[] )

{

new ShowJList();

}

}

Explanation / Answer

Hi,


I ran ur code.

Here is what I can offer :


I would make u a search bar that is dynamic e.g. if u press a all the elements starting with a would show up.


Please Email me to confirm, eyedagger@gmail.com

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