When you use the getsource() method with an actionevent object, the result is a(n) ____________.

My assignment is to create 2 JPanels, one for a 2d array, 6 rows of 5 empty JTextFields and another for JButtons that should somewhat resemble an on-screen keyboard. I created the JPanels with the empty JTextFields and JButtons, now I need a way so that when I press a JButton with a letter from the alphabet it will assign that letter to the first available JTextField on the first available row and move one column at a time until the whole row is filled with letters (trying to add letters to a full row should do nothing). I also need to create a backspace button which I have that will remove the last letter (pressing backspace on an empty row should do nothing).

import java.awt.*; import javax.swing.*; import java.awt.event.*; public class Wordle extends JFrame implements ActionListener { private JPanel p1; private JPanel p2; private JTextField [][] g; public Wordle() { setSize(500,300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); p1 = new JPanel(); p1.setLayout(new GridLayout(6, 5)); g=new JTextField [5][6]; for(int r=0; r<g.length; r++) { for(int c=0; c<g[r].length; c++) { g[r][c]= new JTextField(); getContentPane().add(g[r][c]); p1.add(g[r][c]); } } p2 = new JPanel(); p2.setLayout(new GridLayout(4, 7)); JButton a= new JButton("A"); a.addActionListener(this); p2.add(a); JButton b= new JButton("B"); b.addActionListener(this); p2.add(b); JButton c= new JButton("C"); c.addActionListener(this); p2.add(c); JButton d= new JButton("D"); d.addActionListener(this); p2.add(d); JButton e= new JButton("E"); e.addActionListener(this); p2.add(e); JButton f= new JButton("F"); f.addActionListener(this); p2.add(f); JButton g= new JButton("G"); g.addActionListener(this); p2.add(g); JButton h= new JButton("H"); h.addActionListener(this); p2.add(h); JButton i= new JButton("I"); i.addActionListener(this); p2.add(i); JButton j= new JButton("J"); j.addActionListener(this); p2.add(j); JButton k= new JButton("K"); k.addActionListener(this); p2.add(k); JButton l= new JButton("L"); l.addActionListener(this); p2.add(l); JButton m= new JButton("M"); m.addActionListener(this); p2.add(m); JButton n= new JButton("N"); n.addActionListener(this); p2.add(n); JButton o= new JButton("O"); o.addActionListener(this); p2.add(o); JButton p= new JButton("P"); p.addActionListener(this); p2.add(p); JButton q= new JButton("Q"); q.addActionListener(this); p2.add(q); JButton r= new JButton("R"); r.addActionListener(this); p2.add(r); JButton s= new JButton("S"); s.addActionListener(this); p2.add(s); JButton t= new JButton("T"); t.addActionListener(this); p2.add(t); JButton u= new JButton("U"); u.addActionListener(this); p2.add(u); JButton v= new JButton("V"); v.addActionListener(this); p2.add(v); JButton w= new JButton("W"); w.addActionListener(this); p2.add(w); JButton x= new JButton("X"); x.addActionListener(this); p2.add(x); JButton y= new JButton("Y"); y.addActionListener(this); p2.add(y); JButton z= new JButton("Z"); z.addActionListener(this); p2.add(z); JButton BackSpace= new JButton("<-"); BackSpace.addActionListener(this); p2.add(BackSpace); JButton Enter= new JButton("["); Enter.addActionListener(this); p2.add(Enter); this.getContentPane().add(p1,BorderLayout.NORTH); this.getContentPane().add(p2,BorderLayout.SOUTH); this.setVisible(true); } public void actionPerformed(ActionEvent e) //need help with { if(e.getSource().equals("A")) { for(int r=0; r<g.length; r++) { for(int c=0; c<g[r].length; c++) { g[r][c].setText("A"); } } } } public static void main(String[] args) { new Wordle(); } }

This is my code and my main issue is setting the text in the 2d array of JTextFields using actionlistener, I don't mind doing each if loop for each letter individually as long as it makes sense and works the way I intended it to, btw if you haven't already noticed this is the game "Wordle" I am trying to make, I'm still a newbie programmer and all of this is new to me so some kind of intuitive explanation would be much appreciated.

sliderA = new JSlider( JSlider.HORIZONTAL, 0, 1000, 400); sliderB = new JSlider( JSlider.HORIZONTAL, 0, 1000, 400); . . . sliderA.setName( "sliderA" ); sliderB.setName( "sliderB" ); sliderA.addChangeListener( this ); sliderB.addChangeListener( this );

Any two unique strings will work. It is OK to use the same word for the reference variable and the name of a component. The two are completely separate, and Java will not get confused.


An event object contains a reference to the component that generated the event. To extract that reference from the event object use:

Object getSource()

Since the return type of getSource() is Object (the class at the very top of the class hierarchy) use a type cast with it:

// listener method public void stateChanged( ChangeEvent evt ) { JSlider source; source = (JSlider)evt.getSource(); . . . . }

Now you have a reference to the slider that caused the event and you can use any of that slider's methods.


(Review:) What interface does a slider's listener implement?

ActionEvent Class

Package: java.awt

An instance of the ActionEvent class is passed to the ActionListener when an action event occurs. You can use this object to determine information about the event.

Method

Method

Description

object getSource()

Returns the object on which the event occurred

You can use the getSource method to determine which component sourced the event when the listener is registered as an event listener with more than one component. For example:

private class ClickListener

implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

if (e.getSource() == button1)

{

// code to handle button1 click

}

if (e.getSource() == button2)

{

// code to handle button2 click

}

}

}

In this example, the private class ClickListener can be registered with two buttons (button1 and button2). The getSource method is used in the actionPerformed method to determine which button was clicked.

When you use the getsource() method with an actionevent object, the result is a(n) ____________.
For more information, see Event Handling.

15.When you use thegetSource()method with anActionEventobject, the result isa(n) __________.a.Objectb.ActionEventc.Componentd.TextField