Short source code examples

This code shows how to create a Java FlowLayout that flows left and has horizontal spacing of ten pixels and vertical spacing of five pixels:

FlowLayout flowLayout = new FlowLayout(FlowLayout.LEFT, 10, 5);
jPanel.setLayout(flowLayout);

Here’s what the FlowLayout constructor arguments look like:

FlowLayout(int align, int horizontalSpacing, int verticalSpacing);

FlowLayout 'align' property definitions

The align property can be any of these:

If you want to set your Java JScrollPane scrollbars to always scroll, both horizontally and vertically, this code shows the solution:

These are some simple Java/Scala JOptionPane examples in the form of a Scala utility class (an object, actually).

The following Java source code comes from Sun Microsystems, and shows a JTable example, including the use of an AbstractTableModel, a JScrollPane, and methods like:

  • isCellEditable
  • getColumnName
  • getValueAt
  • getRowCount
  • setPreferredScrollableViewportSize

Here’s the example Java JTable source code:

The following source code comes from Sun Microsystems, and shows a JComboBox, ActionListener, and methods like addActionListener, actionPerformed, getSelectedItem, and more.

Here’s the example Java source code:

The following source code comes from Sun Microsystems, and shows how to implement a MouseListener, including the following MouseListener methods, which all take a MouseEvent:

  • addMouseListener
  • mousePressed
  • mouseReleased
  • mouseEntered
  • mouseExited
  • mouseClicked

Here’s the example Java source code:

The following source code example comes from Sun Microsystems, and shows various things related to showing and clicking a JButton, including using the JButton class, ActionListener, ActionEvent, ImageIcon, setEnabled, setMnemonic, setToolTipText, and actionPerformed.

jbutton, actionperformed, actionlistener, actionevent, setmnemonic, java, swing, jframe, jpanel, tool tip, tooltip, image, imageicon, event, click

The following source code comes from Sun Microsystems, and shows various things related to showing and clicking a JButton, including using the JButton class, ActionListener, ActionEvent, setMnemonic, and actionPerformed.

jbutton, actionperformed, actionlistener, actionevent, setmnemonic, java, swing, jframe, jpanel

The following source code comes from Sun Microsystems, and shows various things related to handling mouse events, including MouseEvent, MouseMotionListener, and the mouseDragged and mouseMoved methods that accept MouseEvent objects.

This is a Java JFileChooser example from Sun Microsystems that shows how to work with a JFileChooser, and also shows related features, including ActionEvent, ActionListener, the actionPerformed and showOpenDialog methods, and more.

(I’ve included it here because I work with Java Swing applications quite a bit, and I want it to be easier to find on my website.)

This is a Java JCheckBox example from Sun Microsystems that shows how to work with a JCheckBox, also showing other features, including ItemEvent, ItemListener, setMnemonic, addItemListener, and more.

(I’ve included it here because I work with Java Swing applications quite a bit, and I want it to be easier to find on my website.)

This is a Java JList example from Sun Microsystems that shows how to work with a JList, including some of the most common and important features, including DefaultListModel, ListSelectionModel, ListSelectionListener, ActionListener, and more. I’ve included it here because I work with Java Swing applications quite a bit, and I want it to be easier to find on my website.

The following source code snippet shows how to create a JComboBox, set a simple DefaultComboBoxModel on it, and most importantly, add an ItemListener to it so your code can react to users changing the selection in the combobox.

The source code is written in Scala, but as you can see, it converts easily to Java:

This source code shows how to create a custom JTabbedPane to help you set the foreground color of a tab’s title text:

I use this following Scala source code to create a Java JTextArea component, set its font, make it readonly, set its margin (insets), then put the textarea into a JScrollPane:

The following source code shows how to set the font, caret position, and margins on a JTextArea component:

JTextArea textArea = new JTextArea();

// somewhere later in your code ...
textArea.setFont(new Font("Monaco", Font.PLAIN, 12));
textArea.setMargin(new Insets(12, 12, 12, 12));

textArea.setCaretPosition(0);

The Font and margin/insets probably make sense in terms of what they do.

Here’s some source code for a Java method that lets you copy text (a String) to the clipboard on your operating system:

I just found the following source code that shows how to add row striping colors to a Java JTable.

The following code is a “show file dialog” method that works on Mac OS X. I wrote the code a long time ago, but this still looks like the preferred way to show a file-chooser dialog on OS X, as of Java 7 and Mac OS X 10.9 and 10.10.

Here’s the Java source code:

Here’s the Scala source code for an Akka Actor I use in an application where I convert PDF pages to plain text: