To create a floating dialog box just create an instance of a Dialog or Window within a Frame. If not in a frame, at least have access to a reference to Frame. I.e,
// Not a modal dialog, because the second parameter is false Dialog d = new Dialog(this, false); // Dialogs and Windows are created hidden, sho you'll need show() d.show(); // Stuff you can do with a Dialog d.setLayout(new BorderLayout()); d.add("South", new Button("Click here")); Usually you'll need to sub-class either the Windows/Dialog class to provide functionality.
(Submitted by Dario Scopesi)
I've made for myself a small MsgBox dialog, which I find useful. You are shown a dialog with a message, and you can click OK to close the dialog. It's only a skeleton for something better refined. You use it in this way:
new MsgBox("A title","Hello world!").show();Here is the code:
// MsgBox.java // by Dario Scopesi (dscopesi@mbox.atlantide.it) // 25-11-1995 //class implementing a simple MsgBox import java.awt.*; class MsgBox extends Dialog { public MsgBox(String title,String msg) { super(null,title,true); // the parent is null... // Note: Brian Miller points out that some platforms require this // to be non-null: specifically, a Frame object. setLayout(new BorderLayout()); Panel p=new Panel(); p.setLayout(new BorderLayout()); resize(new Dimension(200,200)); //setResizable(false); p.add("North",new Label(msg)); p.add("South",new Button("OK")); add("Center",p); } public boolean action(Event e, Object o) { if("OK".equals(o)) { hide(); return true; } return false; } }
"I want to create an MDI interface. The child frame needs to be always on top of its parent, is clipped by the parent and minimizes onto the parent not the desktop."
This is a short example of displaying images in an java application and for setting the application's icon. This works on Solaris. I don't know if it will work on other platforms. Do not look at the programming style - it is just a hack!
To change the mouse cursor, use Frame.setCursor(int). E.g.(Submitted by Cliff Berg)f.setCursor(Frame.HAND_CURSOR);changes the mouse cursor in Frame f to the shape of a hand.
There seem to be some limitations at present. For example, on Windows 95 with the 1.0 JDK, we have had problems creating a "HAND_CURSOR", but not a "CROSSHAIR_CURSOR", "TEXT_CURSOR", or "WAIT_CURSOR". Also, when the mouse is in a pressed state, it does not seem to be possible to change the cursor.
"(i.e. with the same functionality of ScrollView in MFC, so that any part of a larger image can be viewed just by scrolling)"
import java.applet.*; import java.awt.*; /** * This class creates a scrollable drawing area. * Here the drawing is explicity created using Graphics * class, but instead you can use your own image or other * Components. The basic idea is: * 1. Use double buffering to create flicker free scrolling * 2. Use Borderlayout to add scrollbars * 3. handle events targeted to the Scrollbars, and repaint */ public class ScrolledWindow extends Applet { private Scrollbar _hbar, _vbar; private Graphics _offscreen; private Image _bufferedImage; private int dx, dy; public void init() { // compute the width and height of the scrollable content int contentWidth = 500; int contentHeight = 100; // use double buffering to prepare the content _bufferedImage = createImage(contentWidth, contentHeight); _offscreen = _bufferedImage.getGraphics(); _offscreen.setColor(Color.black); _offscreen.fillRect(0, 0, contentWidth, contentHeight); _offscreen.setColor(Color.white); _offscreen.setFont(new Font("TimesRoman", Font.BOLD, 36)); _offscreen.drawString("Java Experiments!", 0, 50); // set up the layout and add scrollbars int scrollIncrement = 5; // set it to what makes sense _hbar = new Scrollbar(Scrollbar.HORIZONTAL, 0, scrollIncrement, 0, contentWidth); _vbar = new Scrollbar(Scrollbar.VERTICAL, 0, scrollIncrement, 0, contentHeight); setLayout(new BorderLayout()); add("East", _vbar); add("South", _hbar); } public boolean handleEvent(Event e) { if (e.target == _hbar) { dx = _hbar.getValue(); repaint(); } else if (e.target == _vbar) { dy = _vbar.getValue(); repaint(); } return super.handleEvent(e); } public void paint(Graphics g) { // draw the content at (-dx, -dy) coordinates. // This will give the scrolling effect. g.drawImage(_bufferedImage, -dx, -dy, this); } }
"I read somewhere that a WINDOW_MOVED event will get posted during window resizes, but it doesn't seemed work for me."
One thing that WILL happen is paint() will be called. Thus, in paint() you could check the window's size against the previous size:// Check if window was just resized public void paint(Graphics g) { Dimension d = size(); if ((d.width != curDim.width) || (d.height != curDim.height)) { curDim = d; // do whatever... } }