File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Swing Program with Buttons Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Swing Program with Buttons" Watch "Swing Program with Buttons" New topic
Author

Swing Program with Buttons

Lisa Lopez
Greenhorn

Joined: Apr 26, 2006
Posts: 3
Each of the buttons that I have listed performs some type of action. The button that says "reverse" in the text field should output the word hello backward olleh. The string at the very bottom, private String reverse(String inStr) is just not outputting correctly.

I'm really new at this and I can't figure out why it's not working. Most of it is working except for the hello reverse part. Can anyone help, please.



import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class MemoSaver2 extends JFrame implements ActionListener

{

public static final int WIDTH = 600;
public static final int HEIGHT = 300;
public static final int LINES = 10;
public static final int CHAR_PER_LINE = 40;
private JTextArea theText;
private String memo1 = "Memo 1 Saved.";
private String memo2 = "Memo 2 Saved.";
private String memo3 = "No Memo 3.";
private String memo4 = "No Memo 4.";
private String memo5 = "No Memo 5.";
private String memo6 = "No Memo 6.";
private String reverse;

public MemoSaver2()

{
setTitle("Memo Saver");
setSize(WIDTH, HEIGHT);

JLabel myLabel = new JLabel();
addWindowListener(new WindowDestroyer());
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());

JPanel buttonPanel = new JPanel();;
buttonPanel.setBackground(Color.GRAY);
buttonPanel.setLayout(new GridLayout(3,2));/*ii*/

JButton memo1Button = new JButton("Save Memo 1");
memo1Button.addActionListener(this);
buttonPanel.add(memo1Button);
JButton memo2Button = new JButton("Save Memo 2");
memo2Button.addActionListener(this);
buttonPanel.add(memo2Button);
JButton clearButton = new JButton("Clear");
clearButton.addActionListener(this);
buttonPanel.add(clearButton);

JButton memo3Button = new JButton("Get Memo 1");
memo3Button.addActionListener(this);
buttonPanel.add(memo3Button);
JButton get1Button = new JButton("Get Memo 2");
get1Button.addActionListener(this);
buttonPanel.add(get1Button);

JButton get2Button = new JButton("Exit");

get2Button.addActionListener(this);
buttonPanel.add(get2Button);
JButton get3Button = new JButton("Reverse");
get3Button.addActionListener(this);
buttonPanel.add(get3Button);
JButton get4Button = new JButton("My Name");
get4Button.addActionListener(this);
buttonPanel.add(get4Button);
JButton get5Button = new JButton("My Info");
get5Button.addActionListener(this);
buttonPanel.add(get5Button);
contentPane.add(buttonPanel, BorderLayout.SOUTH);
//contentPane.add(buttonPanel, GridLayout);
JPanel textPanel = new JPanel();
textPanel.setBackground(Color.BLUE);
theText = new JTextArea(LINES, CHAR_PER_LINE);
theText.setBackground(Color.WHITE);
textPanel.add(theText);
contentPane.add(textPanel, BorderLayout.CENTER);


}

public void actionPerformed(ActionEvent e)
{
String actionCommand = e.getActionCommand();
if (actionCommand.equals("Save Memo 1"))
{
memo1 = theText.getText();
theText.setText("Memo 1 saved."); /*iii*/
}
else if (actionCommand.equals("Save Memo 2")) /*iii*/
{
memo2 = theText.getText();
theText.setText("Memo 2 saved."); /*iii*/
}
else if (actionCommand.equals("Exit")) /*iv*/
{
System.exit(0);
}
else if (actionCommand.equals("Student"))
JOptionPane.showMessageDialog(
null, "Student");
else if (actionCommand.equals("Clear"))
theText.setText("");
else if (actionCommand.equals("Get Memo 1"))
theText.setText(memo1);
else if (actionCommand.equals("Get Memo 2"))
theText.setText(memo2);
else if (actionCommand.equals("My Info"))
{
memo5 = theText.getText();
theText.setText("" + ""
+ "student@park.edu");
}
else if (actionCommand.equals("Hello"))
{
memo3 = theText.getText();
theText.setText("");
}
else if (actionCommand.equals("My Name"))
theText.setText("Student");


}
public static void main(String[]args)

{

MemoSaver2 guiMemo = new MemoSaver2();
guiMemo.setVisible(true);

}

public class WindowDestroyer extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}



private String reverse(String inStr)

{
memo3 = theText.getText();
theText.setText("hello");

//reverse the string then return it

return memo3;
}


}

}


Lisa
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> The string at the very bottom, private String reverse(String inStr)
> is just not outputting correctly.

there is no 'reverse' code in the method, it returns what it gets from the text area

memo3 = theText.getText();
theText.setText("hello");
//reverse the string then return it
return memo3;

but, at this stage it is irrelevant - the method reverse(..) is not called
Lisa Lopez
Greenhorn

Joined: Apr 26, 2006
Posts: 3
Do I put the reverse code in the button?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> Do I put the reverse code in the button?

no, the button calls the method reverse(..)
and supplies the data to satisfy the argument

the 'reverse' code goes into the reverse(..)method,
where you have the notation
//reverse the string then return it

generally you would have it like this


and in your 'reverse' button's actionPerformed(),
the method would be called something like this
theText.setText(reverse(theText.getText()));
samir Sadiki
Ranch Hand

Joined: Apr 22, 2006
Posts: 31
Hi Lisa
Your code is very good.The only thing that is missing is an implementation of the reverse method.
This method should take s string, reverse it, and give you back the reversed string.
When you click on the button reverse, any text that is in the textArea should be reversed.
Try this code
Lisa Lopez
Greenhorn

Joined: Apr 26, 2006
Posts: 3
Thanks Michael and Samir for your help. I think I got it working now.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Swing Program with Buttons