Swing Section Index | Page 5
How can I disable selection of ("grey out") several cells in a column of a JTable according to the value of the cells?
You can "disable" the cells by setting "enabled" to false in your table cell renderer. For example:
public class MyTCRenderer
extends JLabel
implements TableCellRenderer {
public C...more
How can I make user only run one instance of my GUI application?
The easiest way is to use RMI to start the app and check to see if it's already running.
The basic structure is this:
The main method of your app checks the RMI registry to see if the app is ...more
How can I retain the selected text when trying to drag/drop text from a JTextField?
This is a nasty bug in Swing...
The problem is that the Caret's mouse listener and the DragGesture's mouse listener are watching for the same thing.
The caret considers "press" a change of locat...more
How can set a specific CellEditor for one cell in a JTable?
To do this, extend the class DefaultCellEditor and override the method
public Component getTableCellEditorComponent(JTable table,
Object value,
...more
How do I insert a separator into a JPopupMenu?
You can simply add a JSeparator component as follows:
menu.add(new JSeparator());
JTable : Detecting data changes How can I detect changes of particular editable cell in a JTable ?
The trick is to listen to the model of the table.
Table models fire events in the TableModelListener interface.
If you implement this interface, you can add a listener to the table model as foll...more
How do I initialize the text field (file name) in a JFileChooser?
You have to work with the UI component for this:
JFileChooser fileChooser = new JFileChooser(".");
BasicFileChooserUI ui = (BasicFileChooserUI)fileChooser.getUI();
ui.setFileName("help.jpg");
more
How do I load an image onto a JButton in an applet? new JButtion("foo.gif") throws a security exception.
Passing a String to a JButton tries to load the image from a File. You need to create an ImageIcon with URL and pass that into the constructor:
URL picURL = getClass().getResource("mypic.gif");
...more
How can I restrict the size of a component in a BoxLayout?
BoxLayout respects the preferred, maximum and minimum sizes of its managed components.
You can manage the sizes in two ways:
Subclass your components and override their sizing methods:
getPref...more
Why does JOptionPane have a constructor?
The static methods available called "showXxxDialog" will satisfy many common dialogs, and are provided as a matter of convenience. Note that all dialogs displayed this way are modal.
You can crea...more
Can I make JMenuBar open "up" like Windows' Start menu?
This isn't possible to do with default Swing support...
You may be able to do something like it if you implement your own look and feel for JMenu and friends, though.
How can I construct a Flowchart in Java Swing?
There are some toolkits that might help with the graph construction. Tom Sawyer's in particular, has some very nice automated graph layout support.
http://www.jhotdraw.org/
http://www.tomsawyer....more
How do I write a status bar in AWT/Swing?
Here's something I just knocked up. If you use this example remember to add your main component to the CENTER of the frame's content pane.
[FAQ Manager Note] Note that you can have anything you ...more
How to use mouse's right click on JTree to pop a menu?
Just add a MouseListener to your JTree, then just check if it's the popup trigger.
You'll need to perform the check in both your mousePressed and mouseReleased methods, as the trigger may differ ...more
Why does getPreferredSize() return (0,0) for my frame? (Or why does pack() crunch my frame to nothing?)
This happens if you don't have a layout manager set for the components inside the frame.
If you use appropriate layout managers, you should see decent results from pack().
For details on using l...more