Notes - Java Swing

.plan
[Last 5] [Last 10] [Index] [RSS Feed]
[Full .plan (over 1MB)]
Contact
links

Notes

Notes
A collection of notes on various topics, such as version control, programming, food and email/newsgroup usage.
SubVersion
Vim
C and C++
FreeBSD
more...

Image Gallery

Image Gallery
Photos
Screenshots
more...

RMITCS

RMITCS Fluxbox Menu Generator
C Helpdesk Resources
Customising Your CS Account [outdated]
A brief FAQ/HOWTO on Customising X, tcsh and vim at RMITCS.

Software

Codepile
Assorted little scripts and apps.
dumps.pl - FreeBSD Backup script
dice.pl - Perl Dice roller, supports Silhouette, Shadowrun and Alternity
rastodo.py - Python Console todo/reminder
timetable.c - Console timetable/reminder
more...
Lost Emulators
Mirrors of some abandoned emulators (Generator-cbiere and DGen-SDL).
NumLock
An abandoned client-server numbers game.

Videogame Mods

LethalMod
A Max Payne 2 mod that changes the gameplay to make combat more realistic and deadly.
terroristgear
[1.4 KB .zip] A little Rogue Spear: Urban Operations mod that lets you use terrorist and multiplayer equipment (such as C4) in single player games.

Roleplaying Games

Shadowrun (4th ed)
My house firearms rules, notes on armour concealability and simplified matrix rules.
Heavy Gear (2nd ed)
A new and improved character sheet, notes on tool kits and links.
GURPS
A quad NPC sheet, firearms malfunctions and links.
GameMastering
Assorted tips for gamemasters (not specific to any particular game).

Miscellaneous

Taglines
Quotes and stuff.
crazy
Images as preformatted text, with PHP source.
Trombone slide position chart
PDF, 14KB, one a4 page.
[Back to home page]
[Back to Index]

[Raw XML]
JAVA Swing GUI tips and howtos.

Contents:


Opening and saving files using a JFileChooser

This example shows how to use a single filechooser to open and save. The filechooser will remember the directory it entered last which makes it a lot easier for the user. openFile and saveFile can be buttons or menu items with an ActionListener.


   public void actionPerformed(ActionEvent event)
   {
      Object source = event.getSource();

      if (source == openFile)
      {
         if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION)
         {
            try
            {
               File file = fileChooser.getSelectedFile(); //get file(path+name)
               BufferedReader in = new BufferedReader (new FileReader(file));
               //in.readLine() etc
            }
            catch (Exception e) { //ignore...
            }
         }
         else { //user cancelled...
         }
      }
      else if (source == saveFile)
      {
         if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION)
         {
            try
            {
               File file = fileChooser.getSelectedFile();
               PrintWriter out = new PrintWriter(file);
               //out.print() etc
               out.close();
            }
            catch (Exception e) { //ignore...
            }
         }
         else { //user cancelled...
         }
      }

Making JPopupMenu pop up

Create the JPopupMenu and add JMenuItems to it (and ActionListeners to the items) like a normal JMenu. But instead of adding it to a menubar, wrap it in a class that implements MouseAdapter, and use addMouseListener() to add the listener to the item to popup over:

rightClickMeComponent.addMouseListener(new PopupListener(popupMenu))

You create the PopupListener class - it needs to call popupMenu.show() to show the popup when the events are right. This sample PopupListener is a slightly modified version of the one from The Java Tutorial:


public class PopupListener extends MouseAdapter
{
   JPopupMenu popup;

   PopupListener(JPopupMenu popupMenu)
   {
         popup = popupMenu;
   }

   public void mousePressed(MouseEvent e)
   {
         if (e.isPopupTrigger())
             popup.show(e.getComponent(), e.getX(), e.getY());
   }

   public void mouseReleased(MouseEvent e)
   {
         if (e.isPopupTrigger())
             popup.show(e.getComponent(), e.getX(), e.getY());
   }
}

Generated Fri, 18 Jun 2010 14:53:06 +1000
Copyright © 2002-2010 Dylan Leigh.
[HTML 4.01 Transitional]