devxlogo

Beginning Swing-Based Applications

Beginning Swing-Based Applications

When you begin all Swing-based applications, you must set the look and feel and add a WindowListener so that when a user clicks on the exit box, the program exits. Consider putting these items in as base class JxFrame derived from JFrame so that you don’t have to rewrite this code each time:

 public class JxFrame extends JFrame {	public JxFrame(String title)    {		super(title);		setCloseClick();		setLF();	}	private void setCloseClick()    {		//create window listener to respond to window close click		addWindowListener(new WindowAdapter()		 {			public void windowClosing(WindowEvent e) {System.exit(0);}			});	}	//------------------------------------------	private void setLF()    {	// Force SwingApp to come up in the System L&F		String laf = UIManager.getSystemLookAndFeelClassName();		try {		 UIManager.setLookAndFeel(laf);		}		catch (UnsupportedLookAndFeelException exc)			{System.err.println("Warning: UnsupportedLookAndFeel: " + laf);}		catch (Exception exc) {System.err.println("Error loading " + laf +": " + exc);			}	}}

See also  Why ChatGPT Is So Important Today
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist