下面列出了怎么用javax.swing.SwingUtilities的API类实例代码及写法,或者点击链接到github查看源代码。
public void mouseClicked(MouseEvent evt) {
// XXX Benchmark sort performance
//long start = System.currentTimeMillis();
CommonUI.setWaitCursor(SwingUtilities.getRoot(table));
TableColumnModel columnModel = table.getColumnModel();
int viewColumn = columnModel.getColumnIndexAtX(evt.getX());
int column = table.convertColumnIndexToModel(viewColumn);
if (evt.getClickCount() == 1 && column != -1) {
// Reverse the sorting direction.
model.sortByColumn(column, !model.isAscending());
}
// XXX Benchmark performance
// System.out.println("Sort time: " +
// (System.currentTimeMillis() - start));
CommonUI.setDefaultCursor(SwingUtilities.getRoot(table));
}
private void start() throws Throwable {
do {
if ((this.method != null) && Modifier.isStatic(this.method.getModifiers())) {
run(); // invoke static method on the current thread
}
else {
SwingUtilities.invokeLater(this); // invoke on the event dispatch thread
}
Toolkit tk = Toolkit.getDefaultToolkit();
if (tk instanceof SunToolkit) {
SunToolkit stk = (SunToolkit) tk;
stk.realSync(); // wait until done
}
}
while (this.frame != null);
if (this.error != null) {
throw this.error;
}
}
private void start() throws Throwable {
do {
if ((this.method != null) && Modifier.isStatic(this.method.getModifiers())) {
run(); // invoke static method on the current thread
}
else {
SwingUtilities.invokeLater(this); // invoke on the event dispatch thread
}
Toolkit tk = Toolkit.getDefaultToolkit();
if (tk instanceof SunToolkit) {
SunToolkit stk = (SunToolkit) tk;
stk.realSync(); // wait until done
}
}
while (this.frame != null);
if (this.error != null) {
throw this.error;
}
}
@Override
public void dragEnded() {
if (SwingUtilities.isEventDispatchThread()) {
plotConfigurationTreeScrollPane.setBorder(DROP_ENDED_BORDER);
plotConfigurationTree.setBackground(Color.WHITE);
} else {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
plotConfigurationTreeScrollPane.setBorder(DROP_ENDED_BORDER);
plotConfigurationTree.setBackground(Color.WHITE);
}
});
}
}
void setBreakpointHits(final DVSupport dvs, final List<DVThread> hits) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
arrowMenu.removeAll();
threadToMenuItem.clear();
for (DVThread thread : hits) {
JMenuItem item = createMenuItem(dvs, thread);
threadToMenuItem.put(thread, item);
arrowMenu.add(item);
}
if (hits.size() == 0) {
hideHitsPanel();
} else {
setHitsText(hits.size());
showHitsPanel();
}
}
});
}
public void resetData() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
names = null;
instances = null;
bytes = null;
classNames = null;
renderers[0].setMaxValue(0);
renderers[1].setMaxValue(0);
renderers[0].setDiffMode(false);
renderers[1].setDiffMode(false);
tableModel.fireTableDataChanged();
}
});
}
public static void main(String[] args) throws Exception {
JColorChooser chooser = new JColorChooser();
AbstractColorChooserPanel[] panels = chooser.getChooserPanels();
chooser.setChooserPanels(new AbstractColorChooserPanel[] { panels[1] });
JDialog dialog = show(chooser);
pause(DELAY);
dialog.dispose();
pause(DELAY);
Test4177735 test = new Test4177735();
SwingUtilities.invokeAndWait(test);
if (test.count != 0) {
throw new Error("JColorChooser leaves " + test.count + " threads running");
}
}
private void updateBounds() {
if (SwingUtilities.isEventDispatchThread() == false) {
SwingUtilities.invokeLater(updateBoundsRunnable);
return;
}
if (adjustingBounds > 0)
return;
adjustingBounds++;
try {
JTextField parent = (JTextField) getParent();
if (parent != null)
setBounds(0, 0, parent.getWidth(), parent.getHeight());
updateVisibility();
} finally {
adjustingBounds--;
}
}
public void restartEarth() {
localPropertiesService.nullifyChecksumValues();
try {
// Re-generate KMZ
new Thread("Restarting Collect Earth after changing properties/loading project/loading KML points"){
@Override
public void run() {
EarthApp.restart();
}
}.start();
SwingUtilities.invokeLater( ()->{
JOptionPane.showMessageDialog( callingDialog, Messages.getString("OptionWizard.20"), //$NON-NLS-1$
Messages.getString("OptionWizard.21"), JOptionPane.INFORMATION_MESSAGE); //$NON-NLS-1$
if( callingDialog!= null && callingDialog instanceof PropertiesDialog){
callingDialog.dispose();
}
});
} catch (final Exception e) {
logger.error("Error when re-generating the KML code to open in GE ", e); //$NON-NLS-1$
JOptionPane.showMessageDialog(callingDialog, e.getMessage(), Messages.getString("OptionWizard.23"), //$NON-NLS-1$
JOptionPane.WARNING_MESSAGE);
}
}
@Override
public void processKeyEvent(KeyEvent ke) {
//override the default handling so that
//the parent will never receive the escape key and
//close a modal dialog
if (ke.getKeyCode() == KeyEvent.VK_ESCAPE) {
removeSearchField();
// bugfix #32909, reqest focus when search field is removed
SwingUtilities.invokeLater(new Runnable() {
//additional bugfix - do focus change later or removing
//the component while it's focused will cause focus to
//get transferred to the next component in the
//parent focusTraversalPolicy *after* our request
//focus completes, so focus goes into a black hole - Tim
@Override
public void run() {
ETable.this.requestFocus();
}
});
} else {
super.processKeyEvent(ke);
}
}
public static void main(String[] args) throws Exception {
SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
Robot robot = new Robot();
robot.setAutoDelay(50);
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
createAndShowGUI();
}
});
toolkit.realSync();
Point point = getRowPointToClick(2);
robot.mouseMove(point.x, point.y);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
toolkit.realSync();
}
private synchronized void setSortingMode(final PropertySheet ps, final int mode) throws Exception {
throwMe = null;
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
try {
ps.setSortingMode(mode);
} catch (Exception e) {
throwMe = e;
}
}
});
if (throwMe != null) {
Exception ex = throwMe;
throwMe = null;
throw (throwMe);
}
}
@Override
public ProgressDisplayer showProgress(final String caption, final String message, final ProgressController controller) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
if (progressLabel == null) initComponents();
DialogDescriptor dd = createDialogDescriptor(caption, message, controller);
Dialog d = DialogDisplayer.getDefault().createDialog(dd);
d.pack();
owner = d;
if (owner instanceof JDialog) {
((JDialog)owner).setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
}
d.setVisible(true);
}
});
return this;
}
@Override
protected void setUp() throws Exception {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
iowin = IOContainer.getDefault();
JComponent wnd = LifecycleTest.getIOWindow();
jf = new JFrame();
jf.getContentPane().setLayout(new BorderLayout());
jf.getContentPane().add(wnd, BorderLayout.CENTER);
jf.setBounds(20, 20, 700, 300);
jf.setVisible(true);
io = (NbIO) new NbIOProvider().getIO("Test" + testNum++, false);
pane = ((OutputTab) iowin.getSelected()).getOutputPane();
}
});
}
private void fireViewStateChangeEvent(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_fireViewStateChangeEvent
if (jToggleButton1.isSelected()) {
overviewPanel.setOptimalSize();
jTableScrollPane.setViewportView(overviewPanel);
jPanel2.remove(overviewPanel);
} else {
jTableScrollPane.setViewportView(jVillageTable);
jPanel2.add(overviewPanel, BorderLayout.CENTER);
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
jPanel2.updateUI();
}
});
}
}
private static void notifyException(final Exception ex) {
final ByteArrayOutputStream sb = new ByteArrayOutputStream();
final PrintWriter w = new PrintWriter(sb);
w.printf("Unexpected exception: %s%n%n", ex.getMessage());
w.printf("Stack trace:%n%n");
ex.printStackTrace(w);
w.flush();
SwingUtilities.invokeLater(() -> {
String message;
try {
message = sb.toString(StandardCharsets.UTF_8.name());
} catch (UnsupportedEncodingException ex1) {
LOGGER.severe(ex1.getLocalizedMessage());
message = sb.toString();
}
final InfoTextPanel itp = new InfoTextPanel(message);
final NotifyDescriptor d = new NotifyDescriptor.Message(itp, NotifyDescriptor.ERROR_MESSAGE);
DialogDisplayer.getDefault().notify(d);
});
}
private void expandAll(Container container) {
for (final Component component : container.getComponents()) {
if (component instanceof MBeanNodePanel) {
((MBeanNodePanel) component).expand();
}
if (component instanceof Container) {
expandAll((Container) component);
}
if (component instanceof JScrollPane) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
// pour annuler le scrollToVisible des MBeanNodePanel,
// on repositionne le scrollPane tout en haut
((JScrollPane) component).getViewport().setViewPosition(new Point(0, 0));
}
});
}
}
}
private static void createUI(final String lookAndFeelString)
throws Exception {
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
String[][] data = {{"Foo"}};
String[] cols = {"One"};
table = new JTable(data, cols);
table.setSelectionMode(
DefaultListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
frame = new JFrame(lookAndFeelString);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(table);
frame.pack();
frame.setSize(500, frame.getSize().height);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.toFront();
}
});
}
@Override
protected void installKeyboardActions() {
super.installKeyboardActions();
JTextComponent comp = getComponent();
UIDefaults uidefaults = XToolkit.getUIDefaults();
String prefix = getPropertyPrefix();
InputMap map = (InputMap)uidefaults.get(prefix + ".focusInputMap");
if (map != null) {
SwingUtilities.replaceUIInputMap(comp, JComponent.WHEN_FOCUSED,
map);
}
}
/**
* @see java.awt.event.MouseAdapter#mousePressed(java.awt.event.MouseEvent)
*/
public void mousePressed(MouseEvent e) {
if (!SwingUtilities.isLeftMouseButton(e) || !getComponent().isEnabled()) {
return;
}
currentMouseX = e.getX();
currentMouseY = e.getY();
if (isOverFindButton()) {
doFind();
}
if (isOverCancelButton()) {
isCancelArmed = true;
getComponent().repaint();
} else if (isCancelArmed) {
isCancelArmed = false;
getComponent().repaint();
}
}
/**
* Sets visibility of the notification window. If this method is called from a thread other than
* the event dispatch thread, it will invokeLater() to hide the thread the next time the EDT is
* not busy.
*
* @param isVisible Whether the window should be visible
*/
public static void setNotificationWindowVisible(final boolean isVisible) {
if (SwingUtilities.isEventDispatchThread()) {
notificationFrame.setVisible(isVisible);
} else {
SwingUtilities.invokeLater(
new Runnable() {
@Override
public void run() {
NotificationsHandler.notificationFrame.setVisible(isVisible);
}
});
}
}
public static void main(String[] args) throws Exception {
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.getContentPane().add(new JLabel("<html>a<title>"));
frame.pack();
frame.setVisible(true);
}
});
}
public void actionPerformed(ActionEvent e) {
RequestProcessor.getDefault().post(new Runnable() {
public void run() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
log.fine("Options Dialog - Back to modern."); //NOI18N
//OptionsDisplayerImpl.this.showOptionsDialog(null);
OptionsDisplayer.getDefault().open();
}
});
}
});
}
/**
* Adds an Action to the actionMap of the necessary steps to be executed for closing the global search results dialog
*
* @param actionMap
* the ActionMap that should know how to close the global search results dialog
*/
private void addCloseFullSearchDialogAction(ActionMap actionMap) {
actionMap.put(CLOSE_FULL_SEARCH_DIALOG, new ResourceAction("") {
@Override
public void actionPerformed(ActionEvent e) {
searchField.requestFocusInWindow();
SwingUtilities.invokeLater(() -> {
GlobalSearchPanel.this.hideComponents();
logGlobalSearchFocusLost();
});
}
});
}
private static void start() {
SwingUtilities.invokeLater(new Test6541987());
click(KeyEvent.VK_ALT, KeyEvent.VK_H);
click(KeyEvent.VK_TAB);
click(KeyEvent.VK_TAB);
click(KeyEvent.VK_TAB);
click(KeyEvent.VK_TAB);
}
/**
* Display a simple message in the progress window.
*
* @param msg the message to display
*/
public void notifyMsg (final String msg)
{
SwingUtilities.invokeLater(
new Runnable()
{
@Override
public void run ()
{
bar.setString(msg);
}
});
}
public static void main(String... args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new AlphaCompositesApplication().setVisible(true);
}
});
}
@Override
public void done() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
area.setText(cpuInfo + "<br>" + memInfo + "<br>" + osInfo);
osInfo = null;
cpuInfo = null;
memInfo = null;
}
});
}
protected void ch() {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Object object = getActiveNode();
deleteModel.setEnabled(((object instanceof ModelsNode)
&& (((ModelsNode) object).getChildCount() == 0) && (rules
.canDeleteQualifier(((ModelsNode) object)
.getQualifier().getId()))));
}
});
}
/**
* Start the load waiter thread.
* @param barrier the notification barrier
*/
void startLoadWaiter(final Semaphore barrier) {
loadWaiter = new Thread("Start Game Loading") {
@Override
public void run() {
try {
final World world = new World(commons);
world.definition = selectedDefinition;
world.difficulty = Difficulty.values()[difficulty];
final Labels labels = new Labels();
labels.load(commons.rl, U.startWith(selectedDefinition.labels, "labels"));
world.labels = labels;
world.loadCampaign(commons.rl);
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
commons.labels().replaceWith(labels);
commons.world(world);
commons.worldLoading = false;
commons.nongame = false;
barrier.release();
}
});
} finally {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
loadWaiter = null;
}
});
}
}
};
loadWaiter.setPriority(Thread.MIN_PRIORITY);
loadWaiter.start();
}