GUI: action listener compiler-error - java

I cannot seem to make the Exit Button work and thus my program does not compile. If I comment out everything related to the exit button the program works and functions properly. All other buttons work. What is wrong with my Exit Button?
/**
* Write a description of class Converterr here.
*
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
public class Converterr
{
private JLabel usdL, pesosL, eurosL;
private JTextField usdTF, pesosTF, eurosTF;
private JButton pesosB, eurosB, exitB;
PesosButtonHandler pbHandler;
EurosButtonHandler eubHandler;
ExitButtonHandler ebHandler;
public void driver()
{
JFrame c = new JFrame ("Currency Converter");
c.setSize(400,300);
c.setDefaultCloseOperation(c.EXIT_ON_CLOSE);
//Content Pane
Container cp = c.getContentPane ( );
cp.setLayout ( new GridLayout (5,2) );
pesosL = new JLabel ("Pesos: ", SwingConstants.RIGHT);
usdL = new JLabel ("USD: ", SwingConstants.RIGHT);
eurosL = new JLabel ("Euros: ", SwingConstants.RIGHT);
usdTF = new JTextField(8);
pesosTF = new JTextField(8);
eurosTF = new JTextField(8);
pesosTF.setEditable(false);
eurosTF.setEditable(false);
pesosB = new JButton ("Convert to Pesos");
eurosB = new JButton ("Convert to Euros");
exitB = new JButton ("Exit");
// add to content pane container
cp.add(usdL);
cp.add(usdTF);
cp.add(pesosL);
cp.add(pesosTF);
cp.add(eurosL);
cp.add(eurosTF);
cp.add(pesosB);
cp.add(eurosB);
cp.add(exitB);
c.setVisible(true);
//Instantiate Listeners
pbHandler = new PesosButtonHandler();
eubHandler = new EurosButtonHandler();
ebHandler = new ExitButtonHandler();
pesosB.addActionListener(pbHandler);
eurosB.addActionListener(eubHandler);
exitB.addActionListener(ebHandler);
}
//action listener interfaces
private class PesosButtonHandler implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
double inusd;
double outpesos;
inusd = Double.parseDouble(usdTF.getText() );
outpesos = inusd * 12.31;
pesosTF.setText(Double.toString(outpesos));
}
}
private class EurosButtonHandler implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
double inusd, outeuros;
inusd = Double.parseDouble(usdTF.getText() );
outeuros = inusd * .78;
eurosTF.setText(Double.toString(outeuros));
}
}
private class ExitButtonHandler implements ActionListener
{
public void ActionPerformed (ActionEvent e)
{
System.exit(0);
}
}
public static void main (String [ ] args)
{
Converterr conv = new Converterr();
conv.driver();
}
}
Error Message:
Converterr.ExitButtonHandler is not abstract and does not override
abstract method actionPerformed(java.awt.event.ActionEnvt) in java.awt.event.ActionListener

public void ActionPerformed (ActionEvent e)
You have a typo in the method name. It should be:
public void actionPerformed (ActionEvent e)

Related

Receiving a string in Java graphics from a text box

I started to make a graphic for program I built in which to insert a name and length of a song, how do I do it in graphics? I found out how to pick up a button but I do not understand how to absorb something inserted into a text box
import java.awt.*;
import java.awt.event.*;
public class Active extends Frame {
public void init() {
ActionListener al = new MyActionListener();
TextField tf = new TextField(20);
Button b;
setLayout(new FlowLayout());
setSize(1000, 1000);
b = new Button ("first");
b.setActionCommand("First");
b.addActionListener(al);
add(b);
b = new Button ("Second");
b.setActionCommand("Second");
b.addActionListener(al);
add(b);
setVisible(true);
add(tf);
}
public Active(String caption) {
super(caption);
init();
}
public static void main(String[] args) {
Active m = new Active("Active buttons");
}
}
the main:
import java.awt.*;
import java.awt.event.*;
public class MyActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String s = e.getActionCommand();
if(s.equals("First")) {
System.out.println("The first button was switched");
}
if(s.equals("Second")) {
System.out.println("The second button was switched");
}
}
}
I would write it with the Swing GUI Toolkit. Here is a short example of that:
public class MyPanel extends JPanel
{
private JTextField songNameField;
private JTextField songLengthField;
private JButton assignBtn;
public MyPanel()
{
this.songNameField = new JTextField();
this.songLengthField = new JTextField();
this.assignBtn = new JButton("Assign");
this.assignBtn.addActionListener(new ActionListener() {
#Override public void actionPerformed(ActionEvent event)
{
String name = songNameField.getText();
int length = Integer.parseInt(parsesongLengthField.getText());
...
}
});
this.add(songNameField);
this.add(songLengthField);
this.add(assignBtn);
}
}

Missing something to finish my program

Im working on my palindrome program and implementing it into a JFrame. Im stuck on how to display the result in the resultTF in the CalculateButtonHandler scope. any help would be appreciated.
Heres my code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
public class Exercise5 extends JFrame
{
private static final int Width = 400;
private static final int Height = 200;
private JLabel wordJL,resultJL;
private JTextField wordTF,resultTF;
private JButton checkJB,exitJB;
private CalculateButtonHandler checkHandler;
private ExitButtonHandler exitB;
public Exercise5()
{
setTitle ("Palindrome");
wordJL = new JLabel ("Enter a word: ", SwingConstants.RIGHT);
resultJL = new JLabel ("Result: ", SwingConstants.RIGHT);
wordTF = new JTextField(10);
resultTF = new JTextField(10);
checkJB = new JButton ("Calculate");
checkHandler = new CalculateButtonHandler();
exitJB = new JButton ("Exit");
exitB = new ExitButtonHandler();
exitJB.addActionListener (exitB);
Container pane = getContentPane();
pane.setLayout (new GridLayout (3,2));
pane.add(wordJL);
pane.add(wordTF);
pane.add(checkJB);
pane.add(exitJB);
pane.add(resultJL);
pane.add(resultTF);
setSize(Width, Height);
setVisible (true);
setDefaultCloseOperation (EXIT_ON_CLOSE);
}
private class CalculateButtonHandler implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
if(e.getSource().equals(checkJB)) {
String pal1, pal2="";
pal1 = wordTF.getText();
int length = pal1.length();
for ( int i = length - 1 ; i >= 0 ; i-- ) {
pal2 = pal2 + pal1.charAt(i);
}
if (pal1.equals(pal2))
resultTF.setText("True");
else
resultTF.setText("False");
}
}
}
private class ExitButtonHandler implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
System.exit(0);
}
}
public static void main (String[] args){
Exercise5 rectObject = new Exercise5();
}
}
you haven't added checkHandler as the action listener of checkJB.. try:
checkJB = new JButton ("Calculate");
checkHandler = new CalculateButtonHandler();
checkJB.addActionListener(checkHandler); //THIS LINE!

Setting Text With Buttons

I have the following code below. My objective is to allow each button to add to the text field so that the user can input a phone number. The only thing I can't seem to get working is the field to allow multiple text input. Once you click on another button, it replaces it in the text field so only one digit at a time is present. How can I fix this so each button adds its number in without just completely replacing it? Also, why is my border manager not applying to the code? Thank you!!
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
public class Keys
{
private JPanel phone;
public static void main (String[] args)
{
JFrame frame = new JFrame ("Keys");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel phone = new JPanel();
phone.setBorder (BorderFactory.createRaisedBevelBorder());
JTabbedPane tp = new JTabbedPane();
tp.addTab ("KeyPad", new KeyPad());
frame.getContentPane().add(phone);
frame.getContentPane().add(tp);
frame.pack();
frame.pack();frame.setVisible(true);
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class KeyPad extends JPanel
{
private JLabel resultLabel;
private JButton button0, button1, button2, button3, button4, button5, button6, button7, button8, button9, buttonclear;
public KeyPad()
{
setLayout (new GridLayout (5, 3));
resultLabel = new JLabel ("---");
button0 = new JButton ("0");
button1 = new JButton ("1");
button2 = new JButton ("2");
button3 = new JButton ("3");
button4 = new JButton ("4");
button5 = new JButton ("5");
button6 = new JButton ("6");
button7 = new JButton ("7");
button8 = new JButton ("8");
button9 = new JButton ("9");
buttonclear = new JButton ("Clear");
button0.addActionListener (new ButtonListener0());
button1.addActionListener (new ButtonListener1());
button2.addActionListener (new ButtonListener2());
button3.addActionListener (new ButtonListener3());
button4.addActionListener (new ButtonListener4());
button5.addActionListener (new ButtonListener5());
button6.addActionListener (new ButtonListener6());
button7.addActionListener (new ButtonListener7());
button8.addActionListener (new ButtonListener8());
button9.addActionListener (new ButtonListener9());
buttonclear.addActionListener(new ButtonListenerC());
add (resultLabel);
add (button0);
add (button1);
add (button2);
add (button3);
add (button4);
add (button5);
add (button6);
add (button7);
add (button8);
add (button9);
add (buttonclear);
setPreferredSize (new Dimension(250,250));
setBackground (Color.green);
}
private class ButtonListener0 implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
resultLabel.setText ("0");
}
}
private class ButtonListener1 implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
resultLabel.setText ("1");
}
}
private class ButtonListener2 implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
resultLabel.setText ("2");
}
}
private class ButtonListener3 implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
resultLabel.setText ("3");
}
}
private class ButtonListener4 implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
resultLabel.setText ("4");
}
}
private class ButtonListener5 implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
resultLabel.setText ("5");
}
}
private class ButtonListener6 implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
resultLabel.setText ("6");
}
}
private class ButtonListener7 implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
resultLabel.setText ("7");
}
}
private class ButtonListener8 implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
resultLabel.setText ("8");
}
}
private class ButtonListener9 implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
resultLabel.setText ("9");
}
}
private class ButtonListenerC implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
resultLabel.setText (" ");
}
}
}
In each of your actionPerformed method if should be :
resultLabel.setText (resultLabel.getText()+"theNumber");
There seems to be a lot of copy/paste in your code. Try to see if you can make it more simpler.
You can concatenate the new text on to the old text by doing something like this:
resultLabel.setText(resultLabel.getText() + "3");
This gets the current text, then appends "3" to the end of it.
First of all you should only have one ButtonListener, since every ButtonListener does the the same (except the number it should add. The following code should work:
private class ButtonListener implements ActionListener
{
private String value = "";
public ButtonListner(String value) {
this.value = value;
}
public void actionPerformed (ActionEvent event)
{
resultLabel.setText(resultLabel.getText() + value);
}
}
You can init your ButtonListeners this way:
button0.addActionListener(new ButtonListener("0"));

Action Listener: sending a text input to an object in Java

I don't know how to send a text input to an object in Java, from a button pressed on an interface using the action Listener() method.
import javax.swing.*;
import java.awt.event.*;
public class Preassessment extends javax.swing.JFrame implements ActionListener {
static JTextField concept = new JTextField(15);
JButton enter = new JButton("Enter");
JLabel conceptLabel = new JLabel("Concept: ");
public Preassessment() {
super("Preassessment Sys");
setSize(350, 180);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel pane = new JPanel();
pane.add(conceptLabel);
pane.add(concept);
pane.add(enter);
add(pane);
enter.addActionListener(this);
setVisible(true);
}
public void actionPerformed(ActionEvent event) {
if(event.getSource() == enter){
conceptLabel.setText(concept.getText());
}
}
public static void main(String[] arguments) {
Preassessment preassess = new Preassessment();
Preassessment agInterface = new Preassessment(); //object to receive the text
}
}
Your question is not concrete enough. In any case you need a reference to the targetobject somewhere in your code, so one way to do it is:
public class Preassessment extends javax.swing.JFrame implements ActionListener {
static JTextField concept = new JTextField(15);
JButton enter = new JButton("Enter");
JLabel conceptLabel = new JLabel("Concept: ");
public Preassessment(ActionListener listener) {
super("Preassessment Sys");
setSize(350, 180);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel pane = new JPanel();
pane.add(conceptLabel);
pane.add(concept);
pane.add(enter);
add(pane);
enter.addActionListener(listener);
setVisible(true);
}
public void actionPerformed(ActionEvent event) {
if(event.getSource() == enter){
conceptLabel.setText(concept.getText());
}
}
public static void main(String[] arguments) {
Preassessment agInterface = new Preassessment(); //object to receive the text
Preassessment preassess = new Preassessment(agInterface);
}
}

How can i add textfields to an actionlistener of a button in java swing? i have a query regarding strings as well

public class BasicGuiOnlyText extends JPanel{
static String outputHtml="<html>";*>Creating The String*
private JFrame f = new JFrame("Static Web Page Builder"); *>Creating the Frame*
private JMenuBar mb = new JMenuBar(); *>Creating Menu*
private JMenu mnuFile = new JMenu("File");
private JMenuItem mnuItemNew=new JMenuItem("New HTML");
private JMenuItem mnuItemSave=new JMenuItem("Save");
private JMenuItem mnuItemQuit = new JMenuItem("Quit");
private JMenu mnuCreate = new JMenu("Create");
private JMenuItem mnuItemFinish=new JMenuItem("Finish");
private JMenu mnuHelp = new JMenu("Help");
private JMenuItem mnuItemAbout = new JMenuItem("About");
public BasicGuiOnlyText(){
f.setJMenuBar(mb);
mnuFile.add(mnuItemNew);>Adding Menu Items
mnuItemNew.addActionListener(new TextFieldSet());*>Adding MenuItemListeners*
mnuFile.add(mnuItemSave);
mnuFile.add(mnuItemQuit);
mnuItemQuit.addActionListener(new ListenMenuQuit());
mnuHelp.add(mnuItemAbout);
ButtonHandler phandler = new ButtonHandler();
mnuItemAbout.addActionListener(phandler);
mnuCreate.add(mnuItemFinish);
ButtonHandler1 fhandler = new ButtonHandler1();
mnuItemFinish.addActionListener(fhandler);
mb.add(mnuFile);
mb.add(mnuCreate);
mb.add(mnuHelp);
f.getContentPane().setLayout(new BorderLayout());
f.addWindowListener(new ListenCloseWdw());
}
>public class TextFieldSet implements ActionListener{
>JTextField tf=new JTextField(20);
>JPanel tfPanel = new JPanel(new GridLayout(1,1));
>public void actionPerformed(ActionEvent e){
>JTextArea text1 = new JTextArea("Write Your Text", 15, 40);
>JPanel textAreaPanel = new JPanel(new BorderLayout());
>textAreaPanel.add(text1, BorderLayout.CENTER);
>tfPanel.setText("Write your text here:");
>tfPanel.add(tf);
>}
>}
Here i want to add the text field only when this new Html button is clicked. I tried with this code. But this is not working. How it can be done?
class ButtonHandler1 implements ActionListener{ >For the Final HTML tag
public void actionPerformed(ActionEvent e){
outputHtml="</html>";
//Here i need help. i have to add </html> tag when the finish button is clicked. But this code is not working. What should i do?***
}
}
public class ListenMenuQuit implements ActionListener{ *>For the Exit*
public void actionPerformed(ActionEvent e){
System.exit(0);
}
}
class ButtonHandler implements ActionListener{ *>For displaying the Help*
public void actionPerformed(ActionEvent e){
JOptionPane.showMessageDialog(null, "It supports GUI environment to enter your choices.", "HELP", JOptionPane. INFORMATION_MESSAGE);
}
}
public class ListenCloseWdw extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
public void launchFrame(){ *>Launches the Frame*
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(500,500);
f.setVisible(true);
f.setLocationRelativeTo(null);
}
public static void main(String args[]){ *>Main Method*
BasicGuiOnlyText gui = new BasicGuiOnlyText();
gui.launchFrame();
try {
OutputStream htmlfile= new FileOutputStream(new File("test.html"));*>For >the creation of HTML file*
PrintStream printhtml = new PrintStream(htmlfile);
printhtml.println(outputHtml);
printhtml.close();
htmlfile.close();
}
catch (Exception e) {}
}
}
I hope I did understood your question correctly. See below for an example which adds the text "Hello world!" to a JTextField when the JButton is pressed.
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.JTextComponent;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class AppendTextDemo {
public static void main( String[] args ) {
EventQueue.invokeLater( new Runnable() {
#Override
public void run() {
JFrame testFrame = new JFrame( "AppendTextDemo" );
JPanel content = new JPanel( new BorderLayout( ) );
JTextField textField = new JTextField( 20 );
content.add( textField, BorderLayout.CENTER );
JButton appendButton = new JButton( "Append text" );
content.add( appendButton, BorderLayout.SOUTH );
appendButton.addActionListener( new AppendTextActionListener( "Hello world!", textField ) );
testFrame.setContentPane( content );
testFrame.pack();
testFrame.setVisible( true );
testFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
} );
}
private static class AppendTextActionListener implements ActionListener{
private final String textToAppend;
private final JTextComponent textComponent;
private AppendTextActionListener( String textToAppend, JTextComponent textComponent ) {
this.textToAppend = textToAppend;
this.textComponent = textComponent;
}
#Override
public void actionPerformed( ActionEvent e ) {
Document document = textComponent.getDocument();
try {
document.insertString( document.getLength(), textToAppend, null );
} catch ( BadLocationException e1 ) {
e1.printStackTrace();//do something useful with the exception
}
}
}
}
If this was not what you meant, please clarify your question

Categories

Resources