Short source code examples

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:

I just updated my Radio Pi “RSS Feed” script, and in short, here is the source code:

As a quick note, I use code like this in my Mac/Java/Scala applications to determine where I can put my application data files on a user’s Mac OS X filesystem:

The following Java source code comes from this Oracle URL. I’ve copied it here so I can find it more easily, because I’m currently doing a lot of work with Java/Scala Swing keycode/keystroke events, including KeyListener and KeyEvent types of things.

The following Scala source code shows the value of various keys on the keyboard that I was interested in today: