Java GUI listeners without AWT

JavaSwingLayoutAwtListener

Java Problem Overview


I am a starting Java developer, learning just from internet tutorials. I am learning full screen GUI applications. I was told yesterday that I shouldn't use AWT in my programs, because it is outdated. I already know about light and heavyweight components, the main problem is the mouse and keyboard listeners. Why is AWT outdated? How to make a program without AWT (adding listeners to JComponents etc) (what kind of Swing things can replace the AWT)?

Java Solutions


Solution 1 - Java

You're mis-interpreting the information given to you. You should avoid using Swing components with AWT components. It's OK to use Swing with the AWT listener structure, layout managers, etc. and in fact it's impossible not to.

Solution 2 - Java

There have been some good answers, but I would like to cover a slightly different aspect. Things that Swing provides beyond AWT.

##Components## Swing supports styled documents in JEditorPane & JTextPane & to a limited extent using HTML in some other JComponents. AWT does not support styled documents in any component.

AWT provides no tree based structure like JTree, no tabular structure such as JTable, no version of JToolBar.

http://i.stack.imgur.com/iueSU.png" width="677" height="398">

AWT has no equivalent (that I can find or recall) for JColorChooser & none for the simple utility class - JOptionPane.

http://i.stack.imgur.com/UZB98.png">

##Listeners## As mentioned in a comment, see the 20+ extra/alternate listeners in the javax.swing.event package.

##Pluggable Look & Feel## Swing components can be set to a particular look & feel at run-time, including a native PLAF.

http://i.stack.imgur.com/Jaqap.png">

See the screen shots on the Nested Layout Example for some more samples.

##Layouts## In addition to the plethora of AWT layouts, Swing provides:

  1. BoxLayout
  2. GroupLayout
  3. OverlayLayout
  4. ScrollPaneLayout
  5. SpringLayout
  6. ViewportLayout

##Other


There is probably a lot more I missed in that brief description, but the bottom line is that Swing is an altogether newer and more enabled GUI toolkit.

Swing both builds on, and relies heavily on, classes in the AWT.

Solution 3 - Java

Java's Swing takes ActionListeners, which are part of the AWT package. If you wish to use swing, you must use some form of an AWT ActionListener. That is just the way things are. I don't suggest using Java at all for complex guis, but nor would I say that AWT is outdated, as there is no direct replacement. Thus, just go ahead and use AWT.

As an alternative, you could look into JOGL, but that's more if you are trying to create something game-oriented.

Solution 4 - Java

This is a small example which can demonstrate, the use of javax.swing.Action package you should also refer to java doc for javax.swing.event package i think you are finding that . . .

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JToolBar;

class SysOutHelloAction extends AbstractAction {
	private static final Icon printIcon = new ImageIcon("Print.gif");

	SysOutHelloAction() {
		super("Print", printIcon);
		putValue(Action.SHORT_DESCRIPTION, "Hello, World");
	}

	public void actionPerformed(ActionEvent actionEvent) {
		System.out.println("Hello, World");
	}
}

public class SwingActionTester {
	public static void main(String args[]) {
		JFrame frame = new JFrame("Action Sample");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		final Action printAction = new SysOutHelloAction();
		JMenuBar menuBar = new JMenuBar();
		JMenu menu = new JMenu("File");
		menuBar.add(menu);
		menu.add(new JMenuItem(printAction));
		JToolBar toolbar = new JToolBar();
		toolbar.add(new JButton(printAction));
		JButton enableButton = new JButton("Enable");
		ActionListener enableActionListener = new ActionListener() {
			public void actionPerformed(ActionEvent actionEvent) {
				printAction.setEnabled(true);
			}
		};
		enableButton.addActionListener(enableActionListener);
		JButton disableButton = new JButton("Disable");
		ActionListener disableActionListener = new ActionListener() {
			public void actionPerformed(ActionEvent actionEvent) {
				printAction.setEnabled(false);
			}
		};
		disableButton.addActionListener(disableActionListener);
		JButton relabelButton = new JButton("Relabel");
		ActionListener relabelActionListener = new ActionListener() {
			public void actionPerformed(ActionEvent actionEvent) {
				printAction.putValue(Action.NAME, "Changed Action Value");
			}
		};
		relabelButton.addActionListener(relabelActionListener);
		JPanel buttonPanel = new JPanel();
		buttonPanel.add(enableButton);
		buttonPanel.add(disableButton);
		buttonPanel.add(relabelButton);
		frame.setJMenuBar(menuBar);
		frame.add(toolbar, BorderLayout.SOUTH);
		frame.add(buttonPanel, BorderLayout.NORTH);
		frame.setSize(300, 200);
		frame.setVisible(true);
	}
}

Solution 5 - Java

You are right. Both Java AWT and Java Spring are obsolete. Use JavaFX instead.
And, as a commentary, I am frustrated with Java, that it was supposed to be "write once run everywhere", when now it turns out to be "must keep rewriting your app every three months" because new Java releases break previous code, and new packages replace the old.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHiddeView Question on Stackoverflow
Solution 1 - JavaHovercraft Full Of EelsView Answer on Stackoverflow
Solution 2 - JavaAndrew ThompsonView Answer on Stackoverflow
Solution 3 - JavaMirroredFateView Answer on Stackoverflow
Solution 4 - JavaHarshit ThackerView Answer on Stackoverflow
Solution 5 - JavaBaruch AttaView Answer on Stackoverflow