1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
|
public void DisplayAlert(
boolean bList,
String sTitle,
String sText,
int iX, int iY,
int iWidth, int iHeight,
String sIconName,
Object[] opts,
int iDefault
)
{
sAlertButton = "" ;
JOptionPane jop = new JOptionPane();
JTextArea jl = new JTextArea(sText);
JScrollPane sa = new JScrollPane(jl);
JComboBox combo = null ;
sa.setPreferredSize(new Dimension(iWidth, iHeight));
sa.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
sa.setBorder(null);
jl.setLineWrap(true);
jl.setWrapStyleWord(true);
jl.setEditable(false);
int icon = JOptionPane.QUESTION_MESSAGE ;
Icon iIcon = null ;
if(sIconName != null)
{
if(sIconName.equalsIgnoreCase("question")) icon = JOptionPane.QUESTION_MESSAGE ;
else if(sIconName.equalsIgnoreCase("information")) icon = JOptionPane.INFORMATION_MESSAGE ;
else if(sIconName.equalsIgnoreCase("warning")) icon = JOptionPane.WARNING_MESSAGE ;
else if(sIconName.equalsIgnoreCase("error")) icon = JOptionPane.ERROR_MESSAGE ;
else
{
Image img = loadImage(sIconName);
if(img != null) iIcon = new ImageIcon(img);
}
}
// dialog font
if(fDialogFont != null)
{
jl.setFont(fDialogFont);
sa.setFont(fDialogFont);
}
// dialog colors
if(cDialogFore != null)
{
jl.setForeground(cDialogFore);
sa.setForeground(cDialogFore);
}
if(cDialogBack != null)
{
jl.setBackground(cDialogBack);
sa.setBackground(cDialogBack);
}
// adapt the JOptionPane properties
Object[] objs = new Object[10] ;
objs[0] = UIManager.get("OptionPane.background");
objs[1] = UIManager.get("Panel.background");
objs[2] = UIManager.get("ComboBox.background");
objs[3] = UIManager.get("ComboBox.foreground");
objs[4] = UIManager.get("ComboBox.selectionBackground");
objs[5] = UIManager.get("ComboBox.selectionForeground");
UIManager.put("OptionPane.background", cDialogBack);
UIManager.put("Panel.background", cDialogBack);
UIManager.put("ComboBox.selectionForeground", cDialogBack.brighter());
UIManager.put("ComboBox.selectionBackground", cDialogBack.darker());
if(bList)
{
combo = new JComboBox(opts);
combo.setSelectedItem((String)opts[iDefault]); // valeur initiale
jop = new JOptionPane(new Object[]{sa, combo},icon,JOptionPane.OK_CANCEL_OPTION);
if(iIcon != null) jop.setIcon(iIcon);
}
else
{
if(iIcon != null)
{
jop = new JOptionPane(new Object[] {sa},
JOptionPane.PLAIN_MESSAGE,
JOptionPane.DEFAULT_OPTION,
iIcon,
opts,
opts[iDefault]
);
}
else
{
jop = new JOptionPane(new Object[] {sa},
icon,
JOptionPane.DEFAULT_OPTION,
null,
opts,
opts[iDefault]
);
}
}
jop.setBackground(cDialogBack);
jop.setForeground(cDialogFore);
//colorize components
String s = null ;
for(int i =0; i<jop.getComponentCount();i++) {
Container cp = (Container)jop.getComponent(i);
cp.setBackground(cDialogBack);
for(int j=0;j<cp.getComponentCount();j++)
{
if(cp.getComponent(j).getClass().toString().equalsIgnoreCase("class javax.swing.JPanel"))
{
JPanel jp = (JPanel)cp.getComponent(j);
jp.setOpaque(false);
jp.setBackground(cDialogBack);
for(int k=0;k<jp.getComponentCount();k++) jp.getComponent(k).setBackground(cDialogBack);
}
else if(cp.getComponent(j).getClass().toString().equalsIgnoreCase("class javax.swing.JLabel"))
{
JLabel jlb = (JLabel)cp.getComponent(j);
jlb.setBackground(cDialogBack);
}
else if(cp.getComponent(j).getClass().toString().equalsIgnoreCase("class javax.swing.JButton"))
{
JButton jb = (JButton)cp.getComponent(j);
jb.setForeground(cDialogFore);
jb.setBackground(cDialogBack);
s = jb.getText() ;
int ii = s.indexOf("&");
if(ii > -1) {
s = "" + s.charAt(ii+1);
ii = (int)s.charAt(0);
jb.setText(jb.getText().replaceAll("&",""));
jb.setMnemonic(ii);
}
}
else log(cp.getComponent(j).getClass().toString());
}
}
//String s = null ;
JDialog dialog = jop.createDialog(this, sTitle);
dialog.setBounds(iX,iY,iWidth,iHeight);
// center to screen
if(iX < 0 && iY < 0) dialog.setLocationRelativeTo(null);
// center to Forms frame
else if(iX == 0 && iY == 0) dialog.setLocationRelativeTo(this.getParent());
// locate at exact position
else dialog.setLocation(iX, iY);
// show the dialog
dialog.setVisible(true);
UIManager.put("OptionPane.background", objs[0]);
UIManager.put("Panel.background", objs[1]);
UIManager.put("ComboBox.background", objs[2]);
UIManager.put("ComboBox.foreground", objs[3]);
UIManager.put("ComboBox.selectionBackground", objs[4]);
UIManager.put("ComboBox.selectionForeground", objs[5]);
if(bList) {
Object value = jop.getValue();
if (value instanceof Integer && ((Integer)value).intValue()==JOptionPane.OK_OPTION) {
sAlertButton = "" + (combo.getSelectedIndex() + 1) ; //combo.getSelectedItem();
}
}
else
{
Object selectedValue = jop.getValue();
//If there is an array of option buttons:
if(opts != null) {
for(int counter = 0, maxCounter = opts.length;
counter < maxCounter; counter++) {
if(opts[counter].equals(selectedValue))
sAlertButton = "" + (counter+1) ;
}
}
}
log("DisplayAlert() button cliqued:"+sAlertButton);
} |
Partager