import java.awt.event.*; import javax.swing.*; public class YourDialog extends JDialog implements ActionListener { JButton button; public YourDialog() { button = new JButton("Close"); button.addActionListener(this); add(button); pack(); setVisible(true); } public void actionPerformed(ActionEvent e) { dispose(); } }
this.pack(); this.setLocationRelativeTo(null); this.setVisible(true); passSomeTime(); this.setVisible(false); this.dispatchEvent(new WindowEvent( this, WindowEvent.WINDOW_CLOSING));
constraints.fill = GridBagConstraints.BOTH; JLabel l = new JLabel("You have got 2 new Messages."); panel.add(l, constraints); constraints.gridx++; constraints.weightx = 0f; constraints.fill = GridBagConstraints.NONE; constraints.anchor = GridBagConstraints.NORTH; JButton b = new JButton(new AbstractAction("x") { b.setMargin(new Insets(1, 4, 1, 4)); b.setFocusable(false); panel.add(b, constraints); dialog.setUndecorated(true); dialog.setSize(300, 100); new Color(0.7f, 0.7f, 1f), new Color(0.6f, 0.6f, 1f)}); dialog.setContentPane(panel); dialog.setVisible(true);
private void showPlatformWarning() { final JButton closeOption = new JButton(NbBundle.getMessage(J2SEActionProvider.class, "CTL_BrokenPlatform_Close")); closeOption.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(J2SEActionProvider.class, "AD_BrokenPlatform_Close")); final ProjectInformation pi = project.getLookup().lookup(ProjectInformation.class); final String projectDisplayName = pi == null ? NbBundle.getMessage(J2SEActionProvider.class, "TEXT_BrokenPlatform_UnknownProjectName") : pi.getDisplayName(); final DialogDescriptor dd = new DialogDescriptor( NbBundle.getMessage(J2SEActionProvider.class, "TEXT_BrokenPlatform", projectDisplayName), NbBundle.getMessage(J2SEActionProvider.class, "MSG_BrokenPlatform_Title"), true, new Object[]{closeOption}, closeOption, DialogDescriptor.DEFAULT_ALIGN, null, null); dd.setMessageType(DialogDescriptor.WARNING_MESSAGE); final Dialog dlg = DialogDisplayer.getDefault().createDialog(dd); dlg.setVisible(true); }
import java.awt.Component; import javax.swing.*; public class Main { public static void main(String[] args) { JDialog dialog = new JDialog(); JPanel panel = new JPanel(); panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); JLabel label = new JLabel("My Label"); label.setAlignmentX(Component.LEFT_ALIGNMENT); panel.add(label); JScrollPane pane = new JScrollPane(new JTextArea(3, 15)); pane.setAlignmentX(Component.LEFT_ALIGNMENT); panel.add(pane); dialog.add(panel); dialog.pack(); dialog.setVisible(true); } }
import javax.swing.*; import java.awt.*; public class t { public static void main(String[] args) { JDialog downloadingDialog = new JDialog((JFrame)null ,"Start donwloading..."); JProgressBar progressBar = new JProgressBar(JProgressBar.HORIZONTAL); progressBar.setIndeterminate(true); downloadingDialog.setLayout(new FlowLayout(FlowLayout.LEFT)); downloadingDialog.add(progressBar); downloadingDialog.setSize(300, 100); downloadingDialog.setVisible(true); } }
JTextField textfield = new JTextField(8); textfield.setBounds(10, 10, 40, 20); panel.add(textfield); dialog.add(panel); dialog.setSize(200, 100); dialog.setLocationRelativeTo(null); dialog.setVisible(true);
import javax.swing.*; class TestDialogMinimize { public static void main(String[] args) { Runnable r = new Runnable() { public void run() { JFrame f = new JFrame("Has a Dialog"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setSize(400,400); JDialog d = new JDialog(f); d.setSize(200,200); f.setVisible(true); d.setVisible(true); } }; SwingUtilities.invokeLater(r); } }
pack(); @Override public void run() { new CustomDialog(null, "David").setVisible(true);
import java.awt.Window; import javax.swing.JFrame; import javax.swing.JDialog; public class WhyNoDispose extends JDialog { public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { try { WhyNoDispose frame = new WhyNoDispose("my title"); frame.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } public WhyNoDispose(String title) { super((JFrame)null, title); pack(); } }
JButton showWaitBtn = new JButton(new ShowWaitAction("Show Wait Dialog")); JPanel panel = new JPanel(); panel.add(showWaitBtn); JFrame frame = new JFrame("Frame"); frame.getContentPane().add(panel); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); progressBar.setIndeterminate(true); JPanel panel = new JPanel(new BorderLayout()); panel.add(progressBar, BorderLayout.CENTER); panel.add(new JLabel("Please wait......."), BorderLayout.PAGE_START); dialog.add(panel); dialog.pack(); dialog.setLocationRelativeTo(win); dialog.setVisible(true);
public static FileObject showDialog(PhpVisibilityQuery phpVisibilityQuery, SourceGroup[] folders, Class<?> target, String preselectedFileName) { BrowseFolders bf = new BrowseFolders(phpVisibilityQuery, folders, target, preselectedFileName); String title = target == DataFolder.class ? "LBL_SelectFolder" : "LBL_SelectFile"; // NOI18N JButton selectButton = new JButton(NbBundle.getMessage(BrowseFolders.class, title)); String description = target == DataFolder.class ? "ACSD_SelectFolder" : "ACSD_SelectFile"; // NOI18N selectButton.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(BrowseFolders.class, description)); JButton cancelButton = new JButton(NbBundle.getMessage(BrowseFolders.class, "LBL_Cancel")); cancelButton.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(BrowseFolders.class, "ACSD_Cancel")); JButton[] options = new JButton[] {selectButton, cancelButton}; OptionsListener optionsListener = new OptionsListener(bf, target); options[0].setActionCommand(OptionsListener.COMMAND_SELECT); options[0].addActionListener(optionsListener); options[1].setActionCommand(OptionsListener.COMMAND_CANCEL); options[1].addActionListener(optionsListener); title = target == DataFolder.class ? "LBL_BrowseFolders" : "LBL_BrowseFiles"; // NOI18N DialogDescriptor dialogDescriptor = new DialogDescriptor(bf, NbBundle.getMessage(BrowseFolders.class, title), true, options, options[0], DialogDescriptor.BOTTOM_ALIGN, null, null); dialogDescriptor.setClosingOptions(new Object[] {options[0], options[1]}); Dialog dialog = DialogDisplayer.getDefault().createDialog(dialogDescriptor); dialog.setVisible(true); return optionsListener.getResult(); }
l.setOpaque(false); c.add(l, constraints); constraints.anchor = GridBagConstraints.NORTH; final JButton b = new JButton(new AbstractAction("x") { b.setFocusable(false); c.add(b, constraints); f.setVisible(true);
import java.awt.*; import java.awt.Dialog.ModalityType; import javax.swing.*; public class SSCCE { public static void main(String[] args) { JDialog dlg = new JDialog(null, ModalityType.APPLICATION_MODAL); dlg.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); JPanel pnl = new JPanel(new BorderLayout()); pnl.add(new JLabel("As big as needed"), BorderLayout.CENTER); //pnl.setMinimumSize(new Dimension(300,300)); dlg.add(pnl); System.out.println("Dialog minimum size: " + dlg.getMinimumSize()); dlg.pack(); // make the dialog as large as needed to display content // Minimum size is not enforced by the OS without this call dlg.setMinimumSize(dlg.getPreferredSize()); System.out.println("Dialog minimum size: " + dlg.getMinimumSize()); dlg.setVisible(true); } }
import java.awt.Dialog; import java.awt.Label; import java.awt.Window; public class Main { public static void main(String[] args) { Dialog d = new Dialog(((Window)null),"Hello world!"); d.setBounds(0, 0, 180, 70); d.add(new Label("Hello world!")); d.setVisible(true); } }
import javax.swing.JDialog; import javax.swing.JFrame; public class MyDialog { private JFrame frame = new JFrame(); private JDialog dialog = new JDialog(); public MyDialog() { frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(400, 300); frame.setVisible(true); //dialog = new JDialog(frame, JDialog.ModalityType.TOOLKIT_MODAL); dialog = new JDialog(frame, true); dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); dialog.setModal(true); dialog.setSize(300, 200); dialog.setVisible(true); } public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { MyDialog dialog = new MyDialog(); } }); } }
import javax.swing.JDialog; import javax.swing.JFrame; public class DialogAndDaemons { public static void main(String[] args) { JFrame frame = new JFrame(); JDialog dialog = new JDialog(frame, "Dialog"); dialog.pack(); dialog.setLocationRelativeTo(null); dialog.setVisible(true); } }
import javax.swing.*; import java.awt.*; public class MyOptionPane { static Object showInputDialog(Object f, Object message, String title, int messageType, Icon ico, Object[] options, Object initValue) { JOptionPane pane = new JOptionPane(message, messageType, JOptionPane.OK_CANCEL_OPTION, ico, options, initValue); JDialog dialog = pane.createDialog((Component) f, title); if (!dialog.isResizable()) { dialog.setResizable(true); } pane.setInitialSelectionValue(pane.getInitialValue()); // set it pane.setWantsInput(true); dialog.pack(); dialog.setVisible(true); return pane.getInputValue(); } }
import javax.swing.*; class CenterTheDialog { CenterTheDialog() { for (int ii=1; ii<4; ii++) { JFrame f = new JFrame("Frame " + ii); f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); f.setSize(400,300); f.setLocationByPlatform(true); f.setVisible(true); JDialog d = new JDialog(f); d.setSize(300,200); d.setLocationRelativeTo(f); d.setVisible(true); } } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { new CenterTheDialog(); } }); } }
public static FileObject showDialog(SourceGroup[] folders, Class<?> target, String preselectedFileName) { BrowseFolders bf = new BrowseFolders(folders, target, preselectedFileName); String title = target == DataFolder.class ? "LBL_SelectFolder" : "LBL_SelectFile"; // NOI18N JButton selectButton = new JButton(NbBundle.getMessage(BrowseFolders.class, title)); String description = target == DataFolder.class ? "ACSD_SelectFolder" : "ACSD_SelectFile"; // NOI18N selectButton.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(BrowseFolders.class, description)); JButton cancelButton = new JButton(NbBundle.getMessage(BrowseFolders.class, "LBL_Cancel")); cancelButton.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(BrowseFolders.class, "ACSD_Cancel")); JButton[] options = new JButton[] {selectButton, cancelButton}; OptionsListener optionsListener = new OptionsListener(bf, target); options[0].setActionCommand(OptionsListener.COMMAND_SELECT); options[0].addActionListener(optionsListener); options[1].setActionCommand(OptionsListener.COMMAND_CANCEL); options[1].addActionListener(optionsListener); title = target == DataFolder.class ? "LBL_BrowseFolders" : "LBL_BrowseFiles"; // NOI18N DialogDescriptor dialogDescriptor = new DialogDescriptor(bf, NbBundle.getMessage(BrowseFolders.class, title), true, options, options[0], DialogDescriptor.BOTTOM_ALIGN, null, null); dialogDescriptor.setClosingOptions(new Object[] {options[0], options[1]}); Dialog dialog = DialogDisplayer.getDefault().createDialog(dialogDescriptor); dialog.setVisible(true); return optionsListener.getResult(); }