Category: Basic Swing

Jan 02

Basic Swing Details

* WindowUtilities.java Utility class that simplifies creating a window and setting the look and feel. * ExitListener.java A WindowListener with support to close the window. * JAppletExample.java A simple applet (JApplet) created in Swing. Illustrates setting the look and feel, adding components to the content pane, and changing the layout to FlowLayout (default is BorderLayout). …

Continue reading

Jan 02

Creates various buttons. In Swing

import java.awt.*; import javax.swing.*; /** Simple example illustrating the use of JButton, especially * the new constructors that permit you to add an image. * */ public class JButtons extends JFrame { public static void main(String[] args) { new JButtons(); } public JButtons() { super("Using JButton"); WindowUtilities.setNativeLookAndFeel(); addWindowListener(new ExitListener()); Container content = getContentPane(); content.setBackground(Color.white); content.setLayout(new …

Continue reading

Jan 02

A simple example that illustrates creating panels

A simple example that illustrates creating panels. In Swing, a JPanel can contain custom borders. Typically, utility methods in the BorderFactory class are used to create a border for the panel. Uses the following class: * SixChoicePanel.java A JPanel that displays six radio buttons with labels. import java.awt.*; import javax.swing.*; /** Simple example illustrating the …

Continue reading

Jan 02

Creates three common types of sliders

Creates three common types of sliders: one without tick marks, one with tick marks, and one with both tick marks and labels. import java.awt.*; import javax.swing.*; /** Simple example illustrating the use of JSliders, especially * the ability to specify tick marks and labels. * * */ public class JSliders extends JFrame { public static …

Continue reading

Jan 02

A JPanel that displays six radio buttons with labels.

A JPanel that displays six radio buttons with labels. import java.awt.*; import javax.swing.*; /** A JPanel that displays six JRadioButtons. * *. */ public class SixChoicePanel extends JPanel { public SixChoicePanel(String title, String[] buttonLabels) { super(new GridLayout(3, 2)); setBackground(Color.lightGray); setBorder(BorderFactory.createTitledBorder(title)); ButtonGroup group = new ButtonGroup(); JRadioButton option; int halfLength = buttonLabels.length/2; // Assumes even length …

Continue reading

Jan 02

Demonstrates the use of a JColorChooser which presents a dialog with three different tabbed panes to allow the user to select a color preference

Demonstrates the use of a JColorChooser which presents a dialog with three different tabbed panes to allow the user to select a color preference. The dialog returns a Color object based on the user’s selection or null if the user entered Cancel. import java.awt.*; import java.awt.event.*; import javax.swing.*; /** Simple example illustrating the use of …

Continue reading

Jan 02

Demonstrates the use of a JColorChooser which presents a dialog with three different tabbed panes to allow the user to select a color preference

Demonstrates the use of a JColorChooser which presents a dialog with three different tabbed panes to allow the user to select a color preference. The dialog returns a Color object based on the user’s selection or null if the user entered Cancel. import java.awt.*; import java.awt.event.*; import javax.swing.*; /** Simple example illustrating the use of …

Continue reading

Jan 02

mall example showing the basic use of a JToolBar

mall example showing the basic use of a JToolBar import java.awt.*; import javax.swing.*; import java.awt.event.*; /** Small example showing basic use of JToolBar. * * */ public class JToolBarExample extends JFrame implements ItemListener { private BrowserToolBar toolbar; private JCheckBox labelBox; public static void main(String[] args) { new JToolBarExample(); } public JToolBarExample() { super("JToolBar Example"); WindowUtilities.setNativeLookAndFeel(); …

Continue reading

Jan 02

A simple button that contains an image and a label for use in a toolbar

ToolBarButton.java A simple button that contains an image and a label for use in a toolbar. import java.awt.*; import javax.swing.*; /** Part of a small example showing basic use of JToolBar. * The point here is that dropping a regular JButton in a * JToolBar (or adding an Action) in JDK 1.2 doesn’t give * …

Continue reading

Jan 02

Basic tool bar for holding multiple buttons.

BrowserToolBar.java A basic tool bar for holding multiple buttons. import java.awt.*; import javax.swing.*; /** Part of a small example showing basic use of JToolBar. * Creates a small dockable toolbar that is supposed to look * vaguely like one that might come with a Web browser. * Makes use of ToolBarButton, a small extension of …

Continue reading

Jan 02

Simple button that the user can select to load the entered URL.

JIconButton.java A simple button that the user can select to load the entered URL. import javax.swing.*; /** A regular JButton created with an ImageIcon and with borders * and content areas turned off. * */ public class JIconButton extends JButton { public JIconButton(String file) { super(new ImageIcon(file)); setContentAreaFilled(false); setBorderPainted(false); setFocusPainted(false); } } Note: Brought from …

Continue reading

Jan 02

Simple example illustrating the use of check boxes

JCheckBoxTest.java Simple example illustrating the use of check boxes. import javax.swing.*; import java.awt.event.*; */ public class JCheckBoxTest extends JPanel implements ItemListener, ActionListener{ JCheckBox checkBox1, checkBox2; public JCheckBoxTest() { checkBox1 = new JCheckBox("Java Servlets"); checkBox2 = new JCheckBox("JavaServer Pages"); checkBox1.setContentAreaFilled(false); checkBox2.setContentAreaFilled(false); checkBox1.addItemListener(this); checkBox2.addActionListener(this); add(checkBox1); add(checkBox2); } public void actionPerformed(ActionEvent event) { System.out.println("JavaServer Pages selected: " + …

Continue reading

Jan 02

Creates three radio buttons and illustrates handling

JRadioButtonTest.java Creates three radio buttons and illustrates handling ItemEvents in response to selecting a radio button. import javax.swing.JRadioButton; import javax.swing.ButtonGroup; import java.awt.*; import java.awt.event.*; import javax.swing.*; /** */ public class JRadioButtonTest extends JPanel implements ItemListener { public JRadioButtonTest() { String[] labels = {"Java Swing","Java Servlets", "JavaServer Pages"}; JRadioButton[] buttons = new JRadioButton[3]; ButtonGroup group = …

Continue reading

Skip to toolbar