The following code produces a window with buttons, but an error message pops up when I run i and actally press the button. According to the Spring tooltip:
Cannot make a static reference to the non-static method setBackground(Color) from the type JComponent
This program is literally entered from my Java textbook line for line, as far as I can tell. It's an older book, so there might be incompatibility, but it doesn't seem likely.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ButtonTest
{
public static void main(String[] args)
{
final ButtonFrame frame = new ButtonFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
class ButtonFrame extends JFrame
{
public ButtonFrame()
{
setTitle("Button Test");
setSize(Default_width, Default_height);
//panel
ButtonPanel panel = new ButtonPanel();
Container contentPane=getContentPane();
contentPane.add(panel);
}
public static final int Default_width = 300;
public static final int Default_height = 200;
}
class ButtonPanel extends JPanel
{
public ButtonPanel()
{
JButton yellowButton = new JButton("Yellow");
JButton blueButton = new JButton("Blue");
JButton redButton = new JButton("Red");
add(yellowButton);
add(blueButton);
add(redButton);
ColorAction yellowAction= new ColorAction(Color.YELLOW);
ColorAction redAction = new ColorAction(Color.RED);
ColorAction blueAction = new ColorAction(Color.BLUE);
yellowButton.addActionListener(yellowAction);
blueButton.addActionListener(blueAction);
redButton.addActionListener(redAction);
}
}
class ColorAction implements ActionListener
{
public ColorAction(Color c)
{
backgroundColor=c;
}
public void actionPerformed(ActionEvent event)
{
ButtonPanel.setBackground(backgroundColor);
}
private Color backgroundColor;
}
One approach is to nest ColorAction as an inner class in ButtonPanel, where it has implicit access to the enclosing panel.
Addendum: As noted in comments by #Andrew Thompson and #nachokk, the implicit accessibility can be made explicit by qualifying this using the enclosing class name. See JLS §15.8.4. Qualified this for details. In this example, these two invocations are equivalent:
setBackground(backgroundColor);
ButtonPanel.this.setBackground(backgroundColor);
As an more general alternative, consider encapsulating the target panel and color in an Action, as outlined here.
class ButtonPanel extends JPanel {
public ButtonPanel() {
JButton yellowButton = new JButton("Yellow");
JButton blueButton = new JButton("Blue");
JButton redButton = new JButton("Red");
add(yellowButton);
add(blueButton);
add(redButton);
ColorAction yellowAction = new ColorAction(Color.YELLOW);
ColorAction redAction = new ColorAction(Color.RED);
ColorAction blueAction = new ColorAction(Color.BLUE);
yellowButton.addActionListener(yellowAction);
blueButton.addActionListener(blueAction);
redButton.addActionListener(redAction);
}
private class ColorAction implements ActionListener {
public ColorAction(Color c) {
backgroundColor = c;
}
#Override
public void actionPerformed(ActionEvent event) {
setBackground(backgroundColor);
}
private Color backgroundColor;
}
}
ButtonPanel.setBackground()
is not a static method so you can't call it as one. You need a concrete instance of ButtonPanel to set the background.
ButtonPanel bp = new ButtonPanel();
bp.setBackground(backgroundColor);
Also change in look and feel can help:
//UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
Related
I have been learning Java just a few weeks so bear that in mind.. I have managed to solve many issues myself until this one:
What I am trying to achieve is using JComponent in a tabbed view so that in one tab you're able to pick a colour (my println shows it actually gets an sRGB value). Then in another class I should be able to get this value and use it in coloring JPanels.
Can I just pass the Color object or what is the best way to achieve. What I am trying here is not working too well. Sorry for the messy code - I am a newbie...
Here is the main part of the color chooser:
public class Colors extends JPanel implements ChangeListener {
private JColorChooser jcc = null;
protected JLabel title;
static Color newColor;
public Colors() {
super(new BorderLayout());
//Set up the banner at the top of the window
title = new JLabel();
//Set up color chooser for setting text color
jcc = new JColorChooser(title.getForeground());
jcc.getSelectionModel().addChangeListener(this);
jcc.setBorder(BorderFactory.createTitledBorder(
"Choose New Color"));
AbstractColorChooserPanel[] panels=jcc.getChooserPanels();
for(AbstractColorChooserPanel p:panels) {
String displayName = p.getDisplayName();
switch (displayName) {
case "HSV":
jcc.removeChooserPanel(p);
break;
case "HSL":
jcc.removeChooserPanel(p);
break;
case "CMYK":
jcc.removeChooserPanel(p);
break;
case "RGB":
jcc.removeChooserPanel(p);
break;
}
}
add(jcc, BorderLayout.PAGE_END);
}
public void stateChanged(ChangeEvent e) {
Color newColor = jcc.getColor();
title.setForeground(newColor);
System.out.println("color = " + newColor);
}
public static Color getNewCol() {
System.out.println("this now =" + newColor);
return newColor;
}
public static Component createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("Color Selector");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//Create and set up the content pane.
JComponent newContentPane = new Colors();
return newContentPane;
}
}
And then I am trying to get hold of the chosen color in the Main.Java class in this way (which is likely wrong). I also note that the value seems to remain always null - probably I am instantiating in a wrong way (value gets lost?)
a.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("Click add");
value++;
if (value < 21) {
JButton jb1 = new JButton();
//How to get the COLOR from the JColorChooser class?
Color box = Colors.getNewCol();
jb1.setBackground(box);
jb1.setOpaque(true);
//FOR TEST ONLY jb1.setBackground(Color.BLUE);
panel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
panel.add(jb1);
panel.setVisible(true);
//window.add(paneeli);
window.pack();
d = new Dimension(700, 500);
window.setSize(d);
Probably the answer is too obvious but I just can't see it at the moment.
I'd get take your static newColor variable and make it non-static. In my ChangeListener I'd fire the JPanel's innate PropertyChangeSupport so that listeners can be notified. The key is to use an observer design pattern -- For example:
import java.awt.BorderLayout;
import java.awt.Color;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.*;
import javax.swing.event.*;
#SuppressWarnings("serial")
public class TestColors extends JPanel {
private JTabbedPane tabbedPane = new JTabbedPane();
private Colors colors = new Colors();
private JPanel colorDisplayPanel = new JPanel();
public TestColors() {
tabbedPane.add("Colors", colors);
tabbedPane.add("Color Display", colorDisplayPanel);
setLayout(new BorderLayout());
add(tabbedPane);
// add a PropertyChangeListener to our Colors isntance.
colors.addPropertyChangeListener(Colors.NEW_COLOR,
new PropertyChangeListener() {
#Override
public void propertyChange(PropertyChangeEvent evt) {
Color color = (Color) evt.getNewValue();
colorDisplayPanel.setBackground(color);
}
});
}
private static void createAndShowGui() {
JFrame frame = new JFrame("TestColors");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new TestColors());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
#SuppressWarnings("serial")
class Colors extends JPanel implements ChangeListener {
public static final String NEW_COLOR = "new color";
private JColorChooser jcc = null;
protected JLabel title;
private Color newColor = null;
public Colors() {
super(new BorderLayout());
// Set up the banner at the top of the window
title = new JLabel("This is my Banner!", SwingConstants.CENTER);
// Set up color chooser for setting text color
jcc = new JColorChooser(title.getForeground());
jcc.getSelectionModel().addChangeListener(this);
jcc.setBorder(BorderFactory.createTitledBorder("Choose New Color"));
add(jcc, BorderLayout.CENTER);
add(title, BorderLayout.PAGE_START);
}
public void stateChanged(ChangeEvent e) {
// Color newColor = jcc.getColor();
Color oldValue = newColor;
newColor = jcc.getColor();
// fire a notification to the Colors JPanel's property change support
// object. Any listeners will be notified of the color change
firePropertyChange(NEW_COLOR, oldValue, newColor);
title.setForeground(newColor);
}
public Color getNewCol() {
System.out.println("this now =" + newColor);
return newColor;
}
}
I have 2 classes. Both implements runnable to create the GUI. The first one is the main, and the second one is the secondary class.
I want within the actionlistener of the main class to startup the secondary class.
Here is the code (the two classes are separated files):
public class Main implements Runnable
{
private JTextField txt1, txt2;
private JLabel lbl1, lbl2;
public void run()
{
JFrame frame = new JFrame("Secondary");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container pane = frame.getContentPane();
JPanel background = new JPanel();
background.setLayout(new BoxLayout(background, BoxLayout.LINE_AXIS));
.........
// Horizontally adding the textbox and button in a Box
Box box = new Box(BoxLayout.Y_AXIS);
......
background.add(box);
pane.add(background);
frame.pack();
frame.setVisible(true);
}
private class SListener implements ActionListener
{
public void actionPerformed(ActionEvent a)
{
Secondary s = new Secondary();
}
}
public static void main (String[] args)
{
Main gui = new Main();
SwingUtilities.invokeLater(gui);
}
}
public class Secondary implements Runnable
{
private JTextField txt1, txt2;
private JLabel lbl1, lbl2;
public Secondary()
{
Secondary gui = new Secondary();
SwingUtilities.invokeLater(gui);
}
public void run()
{
JFrame frame = new JFrame("Secondary");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container pane = frame.getContentPane();
JPanel background = new JPanel();
background.setLayout(new BoxLayout(background, BoxLayout.LINE_AXIS));
.........
// Horizontally adding the textbox and button in a Box
Box box = new Box(BoxLayout.Y_AXIS);
......
background.add(box);
pane.add(background);
frame.pack();
frame.setVisible(true);
}
}
I want to keep the code in two files, I don't want to mixed the two classes in one file.
As you can see from the code, in the Secondary class, in it's constructor I create an Instance of the Secondary class and I run the gui so that when the Instance of this class is created in the Main class, to run the gui.
Unfortunately this technique is not working.
Any ideas?
Thanks
The following line are complety wrong:
public Secondary(){
Secondary gui = new Secondary();
SwingUtilities.invokeLater(gui);
}
Each time you call new Secondary() somewhere in your code, the above code will be triggered, which in turn calls new Secondary() again, and again, and again, ... and your program is blocked.
You probably want to replace it either by
public Secondary(){
SwingUtilities.invokeLater(this);
}
which will avoid the loop, but this is weird behaviour for a constructor.
It makes much more sense to switch to an empty constructor (or delete it all together)
public Secondary(){
}
and rewrite your listener to
public void actionPerformed(ActionEvent a){
Secondary s = new Secondary();
SwingUtilities.invokeLater( s );
}
I would recommend that you completely re-design your program. I find that it is most helpful to gear my GUI's towards creation of JPanels, not top level windows such as JFrame, which can then be placed into JFrames or JDialogs, or JTabbedPanes, or swapped via CardLayouts, wherever needed. I find that this greatly increase the flexibility of my GUI coding, and is exactly what I suggest that you do. So...
Your first class creates a JPanel that is then placed into a JFrame.
In the first class's ActionListener, create an instance of the 2nd class, place it into a JDialog (not a JFrame), and then display it.
For example,
import java.awt.Component;
import java.awt.Dialog.ModalityType;
import java.awt.Dimension;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.*;
public class TwoWindowEg {
public TwoWindowEg() {
// TODO Auto-generated constructor stub
}
private static void createAndShowGui() {
GuiPanel1 mainPanel = new GuiPanel1();
JFrame frame = new JFrame("Main GUI");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
class GuiPanel1 extends JPanel {
private static final int PREF_W = 800;
private static final int PREF_H = 650;
private GuiPanel2 guiPanel2 = new GuiPanel2(); // our second class!
private JDialog dialog = null; // our JDialog
public GuiPanel1() {
setBorder(BorderFactory.createTitledBorder("GUI Panel 1"));
add(new JButton(new LaunchNewWindowAction("Launch New Window")));
add(new JButton(new DisposeAction("Exit", KeyEvent.VK_X)));
}
#Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}
private class LaunchNewWindowAction extends AbstractAction {
public LaunchNewWindowAction(String name) {
super(name);
}
#Override
public void actionPerformed(ActionEvent e) {
if (dialog == null) {
// get the Window that holds this JPanel
Window win = SwingUtilities.getWindowAncestor(GuiPanel1.this);
dialog = new JDialog(win, "Second Window", ModalityType.APPLICATION_MODAL);
dialog.add(guiPanel2);
dialog.pack();
}
dialog.setVisible(true);
}
}
}
class GuiPanel2 extends JPanel {
public GuiPanel2() {
setBorder(BorderFactory.createTitledBorder("GUI Panel 1"));
add(new JLabel("The second JPanel/Class"));
add(new JButton(new DisposeAction("Exit", KeyEvent.VK_X)));
}
}
class DisposeAction extends AbstractAction {
public DisposeAction(String name, int mnemonic) {
super(name);
putValue(MNEMONIC_KEY, mnemonic);
}
#Override
public void actionPerformed(ActionEvent e) {
Component comp = (Component) e.getSource();
Window win = SwingUtilities.getWindowAncestor(comp);
win.dispose();
}
}
Alternatively, you could swap JPanel "views" using a CardLayout, but either way, you will want to avoid showing two JFrames. Please have a look at The Use of Multiple JFrames, Good/Bad Practice?.
I'm working on a pathfinder program which uses GUI. I tried to run the program before I added some stuff and it showed everything fine. But after working on it some more it stopped showing the buttons and the menu bar.
Here is the code. Some variable names are in Swedish as well, but I hope that it won't be an issue. (Please take in mind that the program is far from finished.)
Thank you in advance!
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
public class Pathfinder extends JFrame {
JButton hittaVäg, visaFörbindelse, nyPlats, nyFörbindelse, ändraFörbindelse;
JMenuBar menyBar;
JMenuItem ny, avsluta, hittaVägMeny, visaFörbindelseMeny, nyPlatsMeny, nyFörbindelseMeny, ändraFörbindelseMeny;
String str = System.getProperty("user.dir");
JFileChooser jfc;
BildPanel Bild = null;
Pathfinder(){
super("PathFinder");
setLayout(new BorderLayout());
setSize(590, 400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
jfc = new JFileChooser(".");
JPanel norra = new JPanel();
add(norra, "norra");
JButton hittaVäg = new JButton("Hitta väg");
JButton visaFörbindelse = new JButton("Visa förbindelse");
JButton nyPlats = new JButton("Ny plats");
JButton nyFörbindelse = new JButton("Ny förbindelse");
JButton ändraFörbindelse = new JButton("Ändra förbindelse");
norra.add(hittaVäg);
norra.add(visaFörbindelse);
norra.add(nyPlats);
norra.add(nyFörbindelse);
norra.add(ändraFörbindelse);
hittaVäg.addActionListener(new HittaLyss());
visaFörbindelse.addActionListener(new VisaLyss());
nyPlats.addActionListener(new NyPlatsLyss());
nyFörbindelse.addActionListener(new NyFörbindelseLyss());
ändraFörbindelse.addActionListener(new NyFörbindelseLyss());
JMenuBar menyBar = new JMenuBar();
setJMenuBar(menyBar);
JMenu arkivMeny = new JMenu("Arkiv");
JMenu operationerMeny = new JMenu("Operationer");
menyBar.add(arkivMeny);
menyBar.add(operationerMeny);
JMenuItem ny = new JMenuItem("Ny");
JMenuItem avsluta = new JMenuItem("Avsluta");
arkivMeny.add(ny);
arkivMeny.add(avsluta);
ny.addActionListener(new NyLyss());
avsluta.addActionListener(new AvslutaLyss());
JMenuItem hittaVägMeny = new JMenuItem("Hitta väg");
JMenuItem visaFörbindelseMeny = new JMenuItem("Visa förbindelse");
JMenuItem nyPlatsMeny = new JMenuItem("Ny plats");
JMenuItem nyFörbindelseMeny = new JMenuItem("Ny förbindelse");
JMenuItem ändraFörbindelseMeny = new JMenuItem("Ändra förbindelse");
operationerMeny.add(hittaVägMeny);
operationerMeny.add(visaFörbindelseMeny);
operationerMeny.add(nyPlatsMeny);
operationerMeny.add(nyFörbindelseMeny);
operationerMeny.add(ändraFörbindelseMeny);
hittaVäg.addActionListener(new HittaLyss());
visaFörbindelse.addActionListener(new VisaLyss());
nyPlats.addActionListener(new NyPlatsLyss());
nyFörbindelse.addActionListener(new NyFörbindelseLyss());
ändraFörbindelse.addActionListener(new ÄndraFörbindelseLyss());
}
class HittaLyss implements ActionListener{
public void actionPerformed(ActionEvent ave){
}
}
class VisaLyss implements ActionListener{
public void actionPerformed(ActionEvent ave){
}
}
class NyPlatsLyss implements ActionListener{
public void actionPerformed(ActionEvent ave){
}
}
class NyFörbindelseLyss implements ActionListener{
public void actionPerformed(ActionEvent ave){
}
}
class ÄndraFörbindelseLyss implements ActionListener{
public void actionPerformed(ActionEvent ave){
}
}
class NyLyss implements ActionListener{
public void actionPerformed(ActionEvent ave){
int svar = jfc.showOpenDialog(Pathfinder.this);
if (svar == JFileChooser.APPROVE_OPTION){
File f = jfc.getSelectedFile();
String filnamn = f.getAbsolutePath();
if (Bild != null)
remove(Bild);
Bild = new BildPanel(filnamn);
add(Bild, BorderLayout.CENTER);
validate();
repaint();
pack();
}
}
}
class AvslutaLyss implements ActionListener{
public void actionPerformed(ActionEvent ave){
}
}
public static void main (String[] args){
new Pathfinder();
}
}
One of the issues is IllegalArgumentException at line
add(norra, "norra");
The layout of the frame's content pane is set to BorderLayout, but this layout does not understand "norra" constraint. See How to Use BorderLayout for more details and examples.
Also, you should call setVisible once all the components are added and initialized.
Call setVisible(true) on the JFrame after adding all components, not before. The order should be:
Add components
Call pack() on the JFrame
Then call setVisible(true) on the JFrame.
You need to first add the components and then call setVisible(true).
So the order matters here. Try it, see if it helps.
I used a plug-in of Eclipse to create the class diagram of this code:
public class ButtonGrid
{
private static int difficulty, moveleft, Counter, treasure_x , treasure_y;
private static String message;
JTextField tf = new JTextField();
public static JTextField tf2 = new JTextField();
JFrame frame = new JFrame(); //creation of the main game window
JPanel panel = new JPanel();
JPanel panel2 = new JPanel(new FlowLayout());
JPanel panel3 = new JPanel();
JLabel hint = new JLabel("Hint:");
JButton[][] grid; //grid buttons
public ButtonGrid (int width, int length)
{
}
ActionListener al = new ActionListener() //Action listener for the buttongrid
{
public void actionPerformed(ActionEvent e)
{
}
};
ActionListener al2 = new ActionListener() // Action listener for the reset button
{
public void actionPerformed (ActionEvent e)
{
}
}
};
public static void main (String[] args)
{
}
I cut some useless parts to reduce the size. The diagram that Eclipse draw is this one:
Do you think it's correct? I'm wondering because i thougth the ActionListeners were considered sub-classes, and also the ActionListener in the main method is not showed, but maybe it's just me not understanding how class diagrams work.
It looks right to me. The ActionListeners you have defined are anonymous classes for your protected attributes a1, and a2. Basically what the anonymous classes are doing is subclassing the ActionListener class. These new, unnamed classes are set to a1, and a2. That is why they show up the way they do in the class diagram. Also the reason that the one in your main method isn't showing up, is that anonymous ActionListener is a local variable to your main function.
Here is some information that Oracle: has about anonymous classes (http://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html)
Hope this help, good luck with your programming.
Your diagram seems correct. None of the variables you create inside the methods will appear in this diagram. Only the variables you define on the top (or outside the methods but inside the class definition) will appear in the diagram:
private static int difficulty, moveleft, Counter, treasure_x , treasure_y;
private static String message;
JTextField tf = new JTextField();
public static JTextField tf2 = new JTextField();
JFrame frame = new JFrame(); //creation of the main game window
JPanel panel = new JPanel();
JPanel panel2 = new JPanel(new FlowLayout());
JPanel panel3 = new JPanel();
JLabel hint = new JLabel("Hint:");
JButton[][] grid; //grid buttons
ActionListener al = new ActionListener() //Action listener for the buttongrid
{
//defintion of this ActionListner
};
ActionListener al2 = new ActionListener() // Action listener for the reset button
{
//definition of this ActionListener
};
ActionListener is actually an interface:
http://docs.oracle.com/javase/7/docs/api/java/awt/event/ActionListener.html
You must define it or else you can't use it. A subclass is a class that has a parent class:
http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html
I just joined, and am glad to be here~ So, this morning (at like 2am, but thats besides the point :P ) I was doing a little bit of Java tests with JFrame and other GUI stuff. This is my first time working with GUIs. I was trying to make a little java app that would act as a dream journaller. However, my progress was frozen when I encountered a problem i could not solve. My code is as follows.
import java.awt.*;
import javax.swing.*;
import java.applet.*;
public class Display extends Canvas
{
static final int WIDTH = 600;
static final int HEIGHT = 400;
public static String defaultEntry = "Dreams...";
public static final String TITLE = "Dream Journal Testing";
Button erase;
public static void main(String[] args)
{
Display d = new Display();
d.create();
}
public void create()
{
JFrame frame = new JFrame();
System.out.println("Running");
Panel cardOne = new Panel();
Panel p1 = new Panel();
Panel p2 = new Panel();
Panel p3 = new Panel();
Panel grid = new Panel();
cardOne.setLayout(new BorderLayout());
p1.setLayout(new GridLayout(2,1,3,6));
TextArea textArea1 = new TextArea(defaultEntry);
/*Font f1 = new Font("Courier", Font.PLAIN, 16);
setFont(f1);*/
Label l1 = new Label("Welcome to the Dream Journal! :)");
Label l2 = new Label("Type your dream below:");
p1.add(l1);
p1.add(l2);
p2.add(textArea1);
p3.setLayout(new FlowLayout(FlowLayout.CENTER));
Button ok = new Button("Save");
erase = new Button("Erase");
p3.add(erase);
p3.add(ok);
cardOne.add("North",p1);
cardOne.add("Center",p2);
cardOne.add("South",p3);
frame.add(cardOne);
//frame.add(cardOne);
//frame.setLocationRelativeTo(null);
frame.pack();
frame.setTitle(TITLE);
frame.setSize(WIDTH, HEIGHT);
frame.setResizable(false);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
System.out.println(textArea1.getText());
}
/*public boolean handleEvent(Event evt)
{
if(evt.target == erase)
{
System.out.println("it works");
return true;
}
else return super.handleEvent(evt);
}
*/
public boolean action(Event evt, Object arg)
{
if("Erase".equals(arg))
{
System.out.println("hello");
//textArea1.setText("");
}
return true;
}
}
The problem i have is I am not able to figure out how to make it so if the "Erase" AWT button is pushed, the system will print a line (as a test). I have tried
public boolean action(Event evt, Object arg)
And
public boolean handleEvent, but neither worked. Anyone have any suggestions for the Java noob that is me? Thanks!! :)
One way is to add an action listener to the button (e.g. for Save). Another way is to create an Action (e.g. for Erase).
Don't mix Swing with AWT components unless it is necessary. It is not worth even learning how to use AWT components at this point in time, use Swing only for best results and best help.
Here is a version of the app. using all Swing components.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Display
{
static final int WIDTH = 600;
static final int HEIGHT = 400;
public static String defaultEntry = "Dreams...";
public static final String TITLE = "Dream Journal Testing";
JButton erase;
public static void main(String[] args)
{
Display d = new Display();
d.create();
}
public void create()
{
JFrame frame = new JFrame();
System.out.println("Running");
JPanel cardOne = new JPanel();
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
cardOne.setLayout(new BorderLayout());
p1.setLayout(new GridLayout(2,1,3,6));
JTextArea textArea1 = new JTextArea(defaultEntry);
JLabel l1 = new JLabel("Welcome to the Dream Journal! :)");
JLabel l2 = new JLabel("Type your dream below:");
p1.add(l1);
p1.add(l2);
p2.add(textArea1);
p3.setLayout(new FlowLayout(FlowLayout.CENTER));
JButton ok = new JButton("Save");
ok.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
System.out.println("Do " + ae.getActionCommand());
}
});
erase = new JButton(new EraseAction());
p3.add(erase);
p3.add(ok);
// Use the constants
cardOne.add(BorderLayout.PAGE_START,p1);
cardOne.add(BorderLayout.CENTER,p2);
cardOne.add(BorderLayout.PAGE_END,p3);
frame.add(cardOne);
frame.pack();
frame.setTitle(TITLE);
frame.setSize(WIDTH, HEIGHT);
frame.setResizable(false);
frame.setLocationByPlatform(true);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
System.out.println(textArea1.getText());
}
}
class EraseAction extends AbstractAction {
EraseAction() {
super("Erase");
}
#Override
public void actionPerformed(ActionEvent arg0) {
System.out.println("Do " + arg0.getActionCommand());
}
}
First let me explain you the Funda of Event Handler....
- First of all there are Event Source, when any action take place on the Event Source, an Event Object is thrown to the call back method.
- Call Back method is the method inside the Listener (Interface) which is needed to be implemented by the Class that implements this Listener.
- The statements inside this call back method will dictate whats needed to be done, when the action is done on the Event Source.
Eg:
Assume
Event Source - Button
When Clicked - Event object is thrown at the call back method
Call back method - actionPerformed(ActionEvent e) inside ActionListener.
Now your case :
Now this can be done in 2 ways.....
1. Let you Display class implements the ActionListener, then Register the button with
the ActionListener, and finally implement the abstract method actionPerformed() of ActionListener.
Eg:
public class Display extends Canvas implements ActionListener{
public Display(){
// Your code....
setComponent(); // Initializing the state of Components
}
public void setComponent(){
// Your code.........
Button b = new Button("Click");
b.addActionListener(this); // Registering the button.
// Your code..........
}
public void actionPerformed(ActionEvent event) {
// Do here whatever you want on the Button Click
}
}
2. Use Anonymous class.
- Anonymous class are declared and initialized simultaneously.
- Anonymous class must implement or extend to only one interface or class resp.
Your Display class will NOT implement ActionListener here....
public class Display extends Canvas {
public Display(){
// Your code....
setComponent(); // Initializing the state of Components
}
public void setComponent(){
// Your code.........
Button b = new Button("Click");
// Registering the button and Implementing it
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event) {
// Do here whatever you want on the Button Click
}
});
// Your code..........
}
}
You need to implement ActionListner :
public class Display extends Canvas implements ActionListener
and add yourself to your button as such:
erase.addActionListener(this);
and then implement the required method:
public void actionPerformed(ActionEvent event) {
//do stuff
}
For more info, check out this tutorial on creating ActionListeners.
You'll find that this observable pattern is widely used the in Java GUI.
A couple high level critiques:
You are using many older AWT components (ie Button) when there are similar, but newer (read: more flexible) Swing components available (ie JButton). Take a look at this for a quick explanation on the difference.
The event model that you have implemented was revamped in 1997 to the observable pattern that I suggested above. If you would like to learn more, you can read this.