Swing components gallery

The following table shows some commonly used Swing components and lists what are probably the most useful and commonly used methods and listeners associated with those components. Remember that we don't list certain methods that are common to all components: for example, you can call setVisible() or setEnabled() on pretty much any component where it'd make sense to do so.

See below for more information on adding listeners to Swing components using the help of an IDE such as Eclipse.

ComponentCommon constructor parametersImportant methodsUseful Listeners
JButton

JButton

  • Text
  • Icon
  • Text/icon
  • Action
  ActionListener
JCheckBox

JCheckBox

  • Text
  • Text/selected
setSelected()
isSelected()
ActionListener
ChangeListener
JRadioButton

JRadioButton

  • Text
  • Text/selected
setSelected()
isSelected()
ButtonGroup.add(...)
ActionListener
ChangeListener
JList

JList

  • Array of items
  • ListModel
getSelectedValue()
getSelectedItem()
ListSelectionListener
JTextField

JTextField

  • No columns
  • Initial text
getText()
setText()
getDocument()
DocumentListener
(Added to Document object returned by getDocument().)
JTextArea

JTextArea

  • No rows/columns
  • Rows/cols/text
JTabbedPane

JTabbedPane

  • Tab placement
addTab()
insertTab()
getSelectedComponent()
setSelectedComponent()
ChangeListener
JScrollPane

JScrollPane

  • Component
  • Component, scrollbar options
setViewportView()  
JSplitPane

JSplitPane

  • Orientation, 2 components

For orientation, use JScrollPane.HORIZONTAL_SPLIT or JScrollPane.VERTICAL_SPLIT.

setDividerLocation()  

JComponent

    MouseListener

Look and feel

You'll see from the above images that Swing components by default have quite a distinct "look and feel" to them. This will make your application appear similar on different platforms, but has the disadvantage that your apps will look unlike other programs running on the system, and may be offputting to users. However, you can give Swing programs a Windows look and feel (or Mac OS L&F etc) with a simple line of code discussed at the latter link.

Using listeners

In general, every component allows you to attach a number of listeners. A listener is generally a special "mini class" (strictly, it's an implementation of an interface) whose methods will get called when intersting things happen: mouse clicks, button presses etc. When we list that a component uses a particular listener in the table above, then it means you need to add some code such as the following:

JList l = new JList();
l.addListSelectionListener(new ListSelectionListener() {
  ...
});

In the part where we've put ..., you'll need to add definitions of or more methods. Which methods you need to define depends on the particular listener. The easiest way to find out which methods you need to add is to get your IDE to tell you. Figure 1 shows a typical case in Eclipse, for example. When we add the addListSelectionListener() line, a little cross appears to the left of the code (1). Clicking on this cross then gives us the option to "Add Unimplemented Methods" (2). Eclipse then gives us some "template" methods to fill in for that listener.

Figure 1: Using the Eclipse IDE to automatically add listener methods.


Editorial page content written by Neil Coffey. Copyright © Javamex UK 2021. All rights reserved.