Question

There are 4 GUI components at the top (NORTH) of the ContentPane of the JFrame, label, two radio buttons, and a combo box. Th

import javax.swing. * import iavaawt.* import iava awt.event import javax. swing. border.* import javax. swing. event. * impo

I want the content of the combo box updates with the corresponding radiobutton, how can I write the two radioButton actionListeners? and mycomboBox addItemListener?

There are 4 GUI components at the top (NORTH) of the ContentPane of the JFrame, label, two radio buttons, and a combo box. The combo box is empty. When a user has clicked a radio button, the content of the combo box will be update You will need to use the setModel method and set the corresponding DefaultComboBoxModel to the combo box. See the screenshots below. A2 X A2 Testing ABC 123 1 Testing ABC 123 A 1 A 2 When the user has selected an item in the combo box, the JLabel will be updated by the selected item. X X A2 A2 123 C CO ABC 20 ABC O 123 2 Steps: BCOE
import javax.swing. * import iavaawt.* import iava awt.event import javax. swing. border.* import javax. swing. event. * import java.util. * public class A2Help01 extends JFrame private JComboBox my.camboBax DefaultComboBoxModel srcModel, destModel; private JRadioBut ton radioButtonABC, radioButton 123; /** main method for A2 */ public static void main (String [] args) i.avaxiswing Swinautilitiesinv.okelater.(new Runnable() public void run() new A2HelpQ1 ); } public A2HelpQ1 () ( super("A2"); setupRadioButtons (); getContentPane().add (setUpTo ol sPan el ( ), BorderLayout .NORTH); setDefaultClose0peration (JFrame. EXIT ON CLOSE); setSize(800, 500); setVisible(true); String labe ls1[]= "A", "B" s rcModel new DefaultComboBoxModel(labels1); String labels2[] = "1", "2" , "3", "4" , "5"; des tMode "C". "E" } ; "D" = new DefaultCombo BoxM ode l (labels2 ) ; private void setupRadio Buttons () radioButtonABC = new JRadioButton ( "ABC"): radioButton 123 = new JRadioButton("123" ) ; / add a ButtonGroup radioButtonABC. addAction Listener (new ActionListener ) ( public void actionPerformed (Action Event e) ( //Complete this radioButton123.addAction Listener(new ActionListener() public void actionPerformed (Action Event e) ( //complete this public JPansl setUpToolsPanel() nyCanboBax= new JComboBox (); nySamboBox.addItemkistener.(new ItemListener() ( 1/complete this JPanel toolsPanel toolsPanel. setLayout (new BoxLayout (tools Panel, BoxLayout.X_AXIS)); toolsPanel.add(radioButtonABC): toolsPanel.add(radioButton 12 3): toolsPanel.add(my.comboBax): return toolsPanel; =new Pans10;
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

// A2HelpQ1.java

import java.awt.BorderLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.ItemEvent;

import java.awt.event.ItemListener;

import javax.swing.BoxLayout;

import javax.swing.ButtonGroup;

import javax.swing.DefaultComboBoxModel;

import javax.swing.JComboBox;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JRadioButton;

public class A2HelpQ1 extends JFrame {

      private JComboBox<String> mycomboBox;

      DefaultComboBoxModel<String> srcModel, destModel;

      private JRadioButton radioButtonABC, radioButton123;

      // label for displaying selected value

      private JLabel label;

      public static void main(String[] args) {

            javax.swing.SwingUtilities.invokeLater(new Runnable() {

                  @Override

                  public void run() {

                        new A2HelpQ1();

                  }

            });

      }

      public A2HelpQ1() {

            super("A2");

            setupRadioButtons();

            getContentPane().add(setupToolsPanel(), BorderLayout.NORTH);

            setDefaultCloseOperation(EXIT_ON_CLOSE);

            setSize(800, 500);

            setVisible(true);

            String labels1[] = { "A", "B", "C", "D", "E" };

            srcModel = new DefaultComboBoxModel<String>(labels1);

            String labels2[] = { "1", "2", "3", "4", "5" };

            destModel = new DefaultComboBoxModel<String>(labels2);

      }

      private void setupRadioButtons() {

            radioButtonABC = new JRadioButton("ABC");

            radioButton123 = new JRadioButton("123");

            // creating a ButtonGroup, adding radio buttons to it, so that only one

            // can be selected at a time

            ButtonGroup group = new ButtonGroup();

            group.add(radioButtonABC);

            group.add(radioButton123);

            radioButtonABC.addActionListener(new ActionListener() {

                  public void actionPerformed(ActionEvent arg0) {

                        // setting model of mycomboBox to srcModel

                        mycomboBox.setModel(srcModel);

                  }

            });

            radioButton123.addActionListener(new ActionListener() {

                  public void actionPerformed(ActionEvent arg0) {

                        // setting model of mycomboBox to destModel

                        mycomboBox.setModel(destModel);

                  }

            });

      }

      public JPanel setupToolsPanel() {

            mycomboBox = new JComboBox<String>();

            mycomboBox.addItemListener(new ItemListener() {

                  public void itemStateChanged(ItemEvent arg0) {

                        // getting selected text from mycomboBox and setting it on label

                        label.setText(mycomboBox.getSelectedItem().toString());

                  }

            });

            //initializing label for displaying selected value

            label = new JLabel("Testing");

            JPanel toolsPanel = new JPanel();

            toolsPanel.setLayout(new BoxLayout(toolsPanel, BoxLayout.X_AXIS));

            toolsPanel.add(label); //adding label

            toolsPanel.add(radioButtonABC);

            toolsPanel.add(radioButton123);

            toolsPanel.add(mycomboBox);

            return toolsPanel;

      }

}

/*OUTPUT*/

X A2 Testing ABC 123

X A2 Testing ABC 123 A

X A2 Testing ABC O 123 1 1 3 4 5

X A2 ABC O 123 3 3

Add a comment
Know the answer?
Add Answer to:
I want the content of the combo box updates with the corresponding radiobutton, how can I write the two radioBut...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I want the content of the combo box updates with the corresponding radiobutton, how can I...

    I want the content of the combo box updates with the corresponding radiobutton, how can I write the two radioButton actionListeners? and mycomboBox addItemListener? There are 4 GUI components at the top (NORTH) of the ContentPane of the JFrame, label, two radio buttons, and a combo box. The combo box is empty. When a user has clicked a radio button, the content of the combo box will be update You will need to use the setModel method and set the...

  • 3.1 - In this exercise, you will see how to offer a user a selection among...

    3.1 - In this exercise, you will see how to offer a user a selection among multiple choices. You can place all choices into a combo box using the following code: /** File HelloViewer.java */ import javax.swing.JFrame; public class HelloViewer { public static void main(String[] args) { JFrame frame = new HelloFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setTitle("HelloViewer"); frame.setVisible(true); } } ---------------------------- /** File HelloFrame.java */ import javax.swing.JFrame; import javax.swing.JLabel; import java.awt.BorderLayout; import java.awt.Font; public class HelloFrame extends JFrame { private String message; private...

  • import java.awt.*; import javax.swing.*; import java.awt.event.*; import javax.swing.BorderFactory; import javax.swing.border.Border; public class Q1 extends JFrame {...

    import java.awt.*; import javax.swing.*; import java.awt.event.*; import javax.swing.BorderFactory; import javax.swing.border.Border; public class Q1 extends JFrame {    public static void createAndShowGUI() {        JFrame frame = new JFrame("Q1");        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        Font courierFont = new Font("Courier", Font.BOLD, 40);        Font arialFont = new Font("Arial", Font.BOLD, 40);        Font sansFont = new Font("Sans-serif", Font.BOLD, 20);        JLabel label = new JLabel("Enter a word"); label.setFont(sansFont);        Font font1 = new Font("Sans-serif", Font.BOLD, 40);       ...

  • Can someone modify my code so that I do not get this error: Exception in thread...

    Can someone modify my code so that I do not get this error: Exception in thread "main" java.lang.NullPointerException at java.awt.Container.addImpl(Container.java:1043) at java.awt.Container.add(Container.java:363) at RatePanel.<init>(RatePanel.java:64) at CurrencyConverter.main(CurrencyConverter.java:16) This code should get the amount of money in US dollars from user and then let them select which currency they are trying to convert to and then in the textfield print the amount //********************************************************************* //File name: RatePanel.java //Name: Brittany Hines //Purpose: Panel for a program that convers different currencyNamerencies to use dollars //*********************************************************************...

  • import javax.swing.*; import java.awt.event.*; import java.awt.*; public class BookReview extends JFrame implements ActionListener {       private JLabel...

    import javax.swing.*; import java.awt.event.*; import java.awt.*; public class BookReview extends JFrame implements ActionListener {       private JLabel titleLabel;       private JTextField titleTxtFd;       private JComboBox typeCmb;       private ButtonGroup ratingGP;       private JButton processBnt;       private JButton endBnt;       private JButton clearBnt;       private JTextArea entriesTxtAr;       private JRadioButton excellentRdBnt;       private JRadioButton veryGoodRdBnt;       private JRadioButton fairRdBnt;       private JRadioButton poorRdBnt;       private String ratingString;       private final String EXCELLENT = "Excellent";       private final String VERYGOOD = "Very Good";       private final String FAIR = "Fair";       private final String POOR = "Poor";       String...

  • Can you please help me to run this Java program? I have no idea why it...

    Can you please help me to run this Java program? I have no idea why it is not running. import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.text.DecimalFormat; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JPanel; import javax.swing.ListSelectionModel; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; @SuppressWarnings({"unchecked", "rawtypes"}) public class SmartPhonePackages extends JFrame { private static final long serialVersionID= 6548829860028144965L; private static final int windowWidth = 400; private static final int windowHeight = 200; private static final double SalesTax = 1.06; private static JPanel panel;...

  • I am getting this Error can you please fix my Java code. import java.awt.Dialog; import java.awt.Label;...

    I am getting this Error can you please fix my Java code. import java.awt.Dialog; import java.awt.Label; import java.awt.TextArea; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map.Entry; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class Fall_2017 {    public TextArea courseInput;    public Label textAreaLabel;    public JButton addData;    public Dialog confirmDialog;    HashMap<Integer, ArrayList<String>> students;    public Fall_2017(){    courseInput = new TextArea(20, 40);    textAreaLabel = new Label("Student's data:");    addData = new JButton("Add...

  • Using the template below create a GUI with a push button event. Use a JTextArea to...

    Using the template below create a GUI with a push button event. Use a JTextArea to display which button was selected. /******************* Name: Date: Notes:     *******************/ import javax.swing.*; import java.awt.*; import java.awt.event.*; class Actions extends JFrame implements ActionListener { Create the components (textarea and buttons)    public Actions()    {      create the window (include a close operation)              create the container (include FlowLayout)           add the event listeners (button.addActionListener)           add the components      }           public...

  • I have currently a functional Java progam with a gui. Its a simple table of contacts...

    I have currently a functional Java progam with a gui. Its a simple table of contacts with 3 buttons: add, remove, and edit. Right now the buttons are in the program but they do not work yet. I need the buttons to actually be able to add, remove, or edit things on the table. Thanks so much. Here is the working code so far: //PersonTableModel.java import java.util.List; import javax.swing.table.AbstractTableModel; public class PersonTableModel extends AbstractTableModel {     private static final int...

  • With the Code below, how would i add a "Back" Button to the bottom of the...

    With the Code below, how would i add a "Back" Button to the bottom of the history page so that it takes me back to the main function and continues to work? import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Scanner; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextField; public class RecipeFinder { public static void main(String[]...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT