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

I need a bit of help with the following Java questions: 1.What is the purpose of

ID: 3809439 • Letter: I

Question

I need a bit of help with the following Java questions:

1.What is the purpose of an event listener?

2. Can you display GUI elements in a window?

3. How do you place a content pane in a container?

.4. Complete the code and then execute.

import javax.swing.*;

import java.awt._____

import java.awt.event.*;

public class Demo {

public static void main(String[] ____)

     {

         Window win = new Window();

     }

}

class Window extends ______ implements ItemListener {

JTextArea ta = new ________________(5,25);

    JComboBox combo1 = new JComboBox();

public Window()

{

    super("Hello Professor!");

    setSize(400,200);

    setDefaultClose________(JFrame.EXIT_____________);

    setVisible(______);

    Container ca = getContentPane();

    ca.setBackground(Color._______); //enter color in caps!

    FlowLayout flm = new ______________;

    ca.setLayout(flm);

   String[] styles = { "IIT Rules", "Love Java", "Happy Thanksgiving"};

    JComboBox<String> combo1 = new JComboBox<String>( _______ ) ;

    combo1.addItemListener(this);

    ca.add(combo1);

    ca.add(ta);

    setContentPane(ca);

}

public void itemStateChanged(ItemEvent ___________) {

    String pick = event.get_____().toString();

  ta.setText(pick);

}

}

Explanation / Answer

1.

For an event to have any effect, a program must detect the event and react to it. In order to detect an event, the program must "listen" for it. Listening for events is something that is done by an object called an event listener. An event listener object must contain instance methods for handling the events for which it listens. For example, if an object is to serve as a listener for events of type MouseEvent, then it must contain the following method (among several others):

2. yes like we are doing in following program.

we get contentpane of this window as an object of type container and then add to it combobox(styles) and jtextarea.(ta)

3.

Container ca = getContentPane();

4

import javax.swing.*;


import java.awt.*;
import java.awt.event.*;

public class Demo {
   public static void main(String[]args)
   {
       Window win = new Window();
   }
}

class Window extends JFrame implements ItemListener {
   JTextArea ta = new JTextArea(5,25);
   JComboBox combo1 = new JComboBox();

   public Window()
   {
       super("Hello Professor!");
       setSize(400,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       setVisible(true);
       Container ca = getContentPane();
       ca.setBackground(Color.BLACK); //enter color in caps!
       FlowLayout flm = new FlowLayout();
       ca.setLayout(flm);
       String[] styles = { "IIT Rules", "Love Java", "Happy Thanksgiving"};
       JComboBox<String> combo1 = new JComboBox<String>(styles) ;
       combo1.addItemListener(this);
       ca.add(combo1);
       ca.add(ta);
       setContentPane(ca);


   }

   public void itemStateChanged(ItemEvent event) {
       String pick = event.getItem().toString();
       ta.setText(pick);
   }
}

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