forked from LupusNobilis/java-org.hwo.ui
131 lines
3.5 KiB
Java
131 lines
3.5 KiB
Java
package org.hwo.ui;
|
|
|
|
import java.awt.BorderLayout;
|
|
import java.awt.FlowLayout;
|
|
|
|
import javax.swing.JButton;
|
|
import javax.swing.JDialog;
|
|
import javax.swing.JFrame;
|
|
import javax.swing.JPanel;
|
|
import javax.swing.border.EmptyBorder;
|
|
import java.awt.Dialog.ModalExclusionType;
|
|
import java.awt.Dialog.ModalityType;
|
|
import java.awt.GridBagLayout;
|
|
import javax.swing.JLabel;
|
|
import java.awt.GridBagConstraints;
|
|
import javax.swing.JTextField;
|
|
import javax.swing.JWindow;
|
|
|
|
import java.awt.Insets;
|
|
import java.awt.event.ActionListener;
|
|
import java.awt.event.ActionEvent;
|
|
|
|
public class JInputDialog extends JDialog {
|
|
|
|
|
|
public static String show(String title,String message,String preload){
|
|
return show(null,title,message,preload);
|
|
}
|
|
public static String show(JFrame parent,String title,String message,String preload){
|
|
|
|
JInputDialog jid = new JInputDialog();
|
|
if (parent != null){
|
|
jid.setLocationRelativeTo(parent);
|
|
}
|
|
jid.setTitle(title);
|
|
jid.lMessage.setText(message);
|
|
jid.setInputText(preload);
|
|
|
|
jid.setVisible(true);
|
|
if (jid.accepted)
|
|
return jid.getInputText();
|
|
|
|
return null;
|
|
}
|
|
|
|
private final JPanel contentPanel = new JPanel();
|
|
private JTextField tfInputText;
|
|
private JLabel lMessage;
|
|
|
|
private boolean accepted;
|
|
|
|
/**
|
|
* Create the dialog.
|
|
*/
|
|
public JInputDialog() {
|
|
setModalityType(ModalityType.APPLICATION_MODAL);
|
|
setModalExclusionType(ModalExclusionType.APPLICATION_EXCLUDE);
|
|
setModal(true);
|
|
setBounds(100, 100, 661, 222);
|
|
getContentPane().setLayout(new BorderLayout());
|
|
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
|
|
getContentPane().add(contentPanel, BorderLayout.CENTER);
|
|
GridBagLayout gbl_contentPanel = new GridBagLayout();
|
|
gbl_contentPanel.columnWidths = new int[]{0, 0};
|
|
gbl_contentPanel.rowHeights = new int[]{0, 0, 0};
|
|
gbl_contentPanel.columnWeights = new double[]{1.0, Double.MIN_VALUE};
|
|
gbl_contentPanel.rowWeights = new double[]{1.0, 0.0, Double.MIN_VALUE};
|
|
contentPanel.setLayout(gbl_contentPanel);
|
|
{
|
|
lMessage = new JLabel("Message Text");
|
|
GridBagConstraints gbc_lMessage = new GridBagConstraints();
|
|
gbc_lMessage.insets = new Insets(0, 0, 5, 0);
|
|
gbc_lMessage.gridx = 0;
|
|
gbc_lMessage.gridy = 0;
|
|
contentPanel.add(lMessage, gbc_lMessage);
|
|
}
|
|
{
|
|
tfInputText = new JTextField();
|
|
GridBagConstraints gbc_tfInputText = new GridBagConstraints();
|
|
gbc_tfInputText.fill = GridBagConstraints.BOTH;
|
|
gbc_tfInputText.gridx = 0;
|
|
gbc_tfInputText.gridy = 1;
|
|
contentPanel.add(tfInputText, gbc_tfInputText);
|
|
tfInputText.setColumns(10);
|
|
}
|
|
{
|
|
JPanel buttonPane = new JPanel();
|
|
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
|
|
getContentPane().add(buttonPane, BorderLayout.SOUTH);
|
|
{
|
|
JButton okButton = new JButton("OK");
|
|
okButton.addActionListener(new ActionListener() {
|
|
public void actionPerformed(ActionEvent arg0) {
|
|
accept();
|
|
}
|
|
});
|
|
okButton.setActionCommand("OK");
|
|
buttonPane.add(okButton);
|
|
getRootPane().setDefaultButton(okButton);
|
|
}
|
|
{
|
|
JButton cancelButton = new JButton("Cancel");
|
|
cancelButton.addActionListener(new ActionListener() {
|
|
public void actionPerformed(ActionEvent e) {
|
|
cancel();
|
|
}
|
|
});
|
|
cancelButton.setActionCommand("Cancel");
|
|
buttonPane.add(cancelButton);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
public String getInputText(){
|
|
return tfInputText.getText();
|
|
}
|
|
public void setInputText(String inputText){
|
|
tfInputText.setText(inputText);
|
|
}
|
|
|
|
private void accept(){
|
|
accepted = true;
|
|
setVisible(false);
|
|
}
|
|
|
|
private void cancel(){
|
|
setVisible(false);
|
|
}
|
|
}
|