FontMetrics fm = getFontMetrics(getFont()); System.out.println(fm.getHeight()); System.out.println(fm.getLeading()); System.out.println(fm.stringWidth("This is a sample string"));Note that the height is the actual space the font requires, while leading is the space the font requests to be used between lines of text on the screen.
Layout Manager | Respects... |
---|---|
BorderLayout | preferred height (NORTH, SOUTH)
preferred width (EAST, WEST) |
FlowLayout | preferred size |
CardLayout | none |
GridLayout | none |
GridBagLayout | preferred size (for initial component sizes) |
To create a radio button, you need to create check boxes, and add them to a checkboxgroup.
public class Test { public static void main(String[] args) { Frame f = new Frame(); f.setLayout(new GridLayout(0,1)); // one column and multiple rows CheckboxGroup group = new CheckboxGroup(); f.add(new Checkbox("One", group, true)); f.add(new Checkbox("Two", group, false)); f.add(new Checkbox("Three", group, false)); f.pack(); f.setVisible(true); } }
IBM began work on SWT a few years ago, because Swing was still immature and didn't perform well. They decided to create a new toolkit to provide better performance using native widgets.
For the content pane of a javax.swing.JApplet, the default layout manager is a BorderLayout.
Frame[] f = Frame.getFrames(); Frame active = null; for (int i = 0; i < f.lenght; i++) { if (f[i].isActive()) { active = f[i]; break; } }
If you wish to change the look and feel of a running program, you will need to call the updateComponentTreeUI() method of the SwingUtilities class to update each top-level window.
public void mousePressed( java.awt.event.MouseEvent evt) { if ( evt.getModifiers() == InputEvent.BUTTON1_MASK) // left button process else // right button process }
Rectangle rect = table.getCellRect(row, column, true); table.scrollRectToVisible(rect);
The problem is that your main() method may not end at that point. It could be busy doing some other task. If the user had to wait for the main() method to finish before they could interact with the GUI, they could end up quite unhappy.
So the AWT library implements its own thread to watch GUI interaction. This thread is essentially a little loop that checks the system event queue for mouse clicks, key presses and other system-level events. (You can also put your own events in the system queue, but that's another story...).
The AWT thread (aka the "Event Dispatch" thread) grabs a system event off the queue and determines what to do with it. If it looks like a click on top of a component, it calls the mouse click processing handler for that component. That component, in turn, could fire other events. For example, if you click on a JButton, the AWT thread passes the mouse click to the JButton, which interprets it as a "button press", and fires its own actionPerformed event. Anyone listening will have their actionPerformed method called.
The AWT thread also handles repainting of your GUI. Anytime you call repaint(), a "refresh" request is placed in the event queue. Whenever the AWT thread sees a "refresh" request, it calls the appropriate methods to layout the GUI and paint any components that require painting.
Note: The AWT "thread" may in fact be implemented by multiple threads under some runtime environments. These threads coordinate effort to watch for mouse clicks, keypresses, repaint requests, etc. As far as you're concerned you can treat this all as one "AWT thread".
setToolTipText("<html>This is the first line<br>This is the second line</html>");
JTextField textField = new JTextField(); UndoManager manager = new UndoManager(); Document document = textField.getDocument(); document.addUndoableEditListener(manager);Then, when it is time to undo or redo the last edit, you would notify the UndoManager by calling either the undo() or redo() method of the manager. If the manager cannot undo an operation, the runtime exception CannotUndoException is thrown. If a redo operation cannot be redone, the runtime exception CannotRedoException is thrown.