Short source code examples

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));


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:

As a quick source code example, this is how I emulate pressing the Fn key on a Mac OS X keyboard using AppleScript:

tell application "System Events"
    key code 63
    key code 63
end tell

The key code for the Fn key is 63, so all that code does is emulate pressing that key twice. Doing this triggers the Mac speech-to-text facility, which I use in my Sarah application (like Siri for the Mac).

Without much introduction or discussion, here’s an example of how to use Scala’s Try, Success, and Failure classes:

I used the following Scala source code to build a semi-transparent dialog (a Java JFrame, actually) for the second version of Sarah:

Yesterday I needed to be able to specify a port with a git clone command, because the git server runs ssh on a non-standard port. In short, the solution was to put this text in my ~/.ssh/config file:

I get stock quotes on my Radio Pi system with this simple Python script:

These are some notes on how I installed a Play Framework + MongoDB application on a Linux server at A2 Hosting. These notes probably won’t make sense to other people, they’re just here for me. (Sorry about that, I don’t have a great way to post things here just for myself.)

Here are the notes:

Here’s a quick Linux Bash if/else example featuring an or clause:


day_of_week=`date +"%a"`

if [ "$day_of_week" == "Sat" ] || [ "$day_of_week" == "Sat" ]; then
  echo 'Weekend!'
  echo 'Not weekend!'

The if/or clause is similar to other if/or expressions, and uses the || syntax shown.

Until now I haven’t had a Sencha Touch app.js example out here, so here’s one: