Posts Tagged ‘java’

Intercepting Events With A Glass Pane

Thursday, August 25th, 2005

There are a ton of GlassPane examples out there, but it was hard to find a good concise one that did exactly what I wanted. So here are the results of a spike.

This should create a GlassPane that catches all mouse / key events and disappears only when escape is pressed.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
 
public class GlassPane extends JPanel {
	public void install(final RootPaneContainer rootPane) {
		final Component prevGlassPane = rootPane.getGlassPane();
		rootPane.setGlassPane(this);
 
		addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				Toolkit.getDefaultToolkit().beep();
			}
		});
		addFocusListener(new FocusAdapter() {
			public void focusLost(FocusEvent e) {
				requestFocus();
			}
		});
		addKeyListener(new KeyAdapter() {
			public void keyTyped(KeyEvent e) {
				Toolkit.getDefaultToolkit().beep();
			}
 
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
					setVisible(false);
					rootPane.setGlassPane(prevGlassPane);
				}
			}
		});
 
		setOpaque(false);
		setVisible(true);
		repaint();
		requestFocus();
	}
 
	public void paint(Graphics g) {
		super.paint(g);
 
		g.setFont(new Font("Arial", Font.BOLD, 24));
		g.setColor(Color.blue);
 
		g.drawString("Glass!!!", 110, 140);
	}
 
	public static void main(String[] args) {
		final JFrame frame = new JFrame("GlassPane Test");
 
		JButton showButton = new JButton("Click here to show glass pane");
		showButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new GlassPane().install(frame);
			}
		});
 
		frame.getContentPane().setLayout(new BorderLayout(10, 10));
		frame.getContentPane().add(BorderLayout.NORTH, new JTextField("TextField1"));
		frame.getContentPane().add(BorderLayout.CENTER, new JTextField("TextField2"));
		frame.getContentPane().add(BorderLayout.EAST, showButton);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(new Dimension(575, 300));
		frame.setVisible(true);
	}
}

What Should You Use For Java Persistence

Thursday, January 22nd, 2004

People keep asking me what to use for persistence in java. Definitely not entity beans :)

|These days I’m more in the .Net world, but I still know quite a few !ThoughtWorkers in the Java world, I called on|people.JoeWalnes|
(a published author now!) and one of the !ThoughtWorkers that does a decent job of impressing the hell out of me, for a recommendation I could pass on.

joejoejoewalnes: if you want relational db persistence, hibernate is the way to go. if you want to pay, try kodo (a commercial jdo impl)… both are very good
joejoejoewalnes: if you want simpler persistence and dont care about a rdbms, try prevayler or jdbm
joejoejoewalnes: and if you want to learn about hibernate, buy my book

So there you have it people.

*Hibernate – http://www.hibernate.org/

*Kodo – http://www.solarmetric.com/Software/Kodo_JDO/

*Prevayler – http://www.prevayler.org/

*JDBM – http://jdbm.sourceforge.net/

and as for Joe’s shameless plug ;)

*Java Open Source Programming on Amazon – http://www.amazon.com/exec/obidos/tg/detail/-/0471463620/102-2820143-0413763