tutorials.de Buch-Aktion 05/2012
ERLEDIGT
NEIN
ANTWORTEN
3
ZUGRIFFE
1351
EMPFEHLEN
  • An Twitter übertragen
  • An Facebook übertragen
AUF DIESES THEMA
ANTWORTEN
  1. #1
    DarthShader DarthShader ist offline Mitglied Platin
    Registriert seit
    May 2004
    Beiträge
    684
    Hallo,

    ich habe hier einen modalen JDialog, der Parent ist ein normaler JFrame.

    Ist der Dialog geöffnet und ich klicke daneben auf den JFrame, so erklingt solch ein "bing" Sound (jedenfalls unter Windows) als Hinweis, dass dort nicht hingeklickt werden kann (da ja der Dialog modal ist).

    Meine Frage ist nun, kann ich diesen danebenliegenden Mausklick abfangen? Ich möchte damit zwei Dinge erreicht: 1. Der "bing" Sound soll nicht kommen und 2. ich möchte den Dialog schließen.


    Vielen Dank für Eure Hilfe!
     

  2. #2
    DarthShader DarthShader ist offline Mitglied Platin
    Registriert seit
    May 2004
    Beiträge
    684
    Kann mir bei dieser Sache keiner helfen? Könnte es sein, dass diese Sache eher von Betriebssystem gesteuert wird, oder fängt wirklich Java diesen Klick neben den Dialog ab, denn dieses Modality-Management macht Java ja auch.

    Über Hilfe würde ich mich sehr freuen, denn in dieser Sache weiß ich einfach nicht weiter.

    Danke!
     

  3. #3
    desanocra desanocra ist offline Rookie
    Registriert seit
    Nov 2007
    Beiträge
    5
    Falls Du tatsächlich erreichen willst das bei einem click daneben sich das Fenster schliesst,
    benutze doch einfach ein popup menu als Dialog.

    Du poppst es auf, und wenn man daneben clickt schliesst es sich.

    Hier ein Beispiel das ich geschrieben habe.
    Ich habe auch ToolbarView etc hinzugefügt aber die sind nicht wichtig.
    Wichtig ist eigentlich nur der erste, zweite und dritte Block.

    Code :
    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
    
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
     
    import javax.swing.BorderFactory;
    import javax.swing.JPopupMenu;
     
    import <BLA_BLA>.client.gui.view.components.ToolbarButtonType;
    import <BLA_BLA>.client.gui.view.components.ToolbarView;
     
     
    @SuppressWarnings("serial")
    public abstract class PopupView extends JPopupMenu {
     
        public PopupView(final String inCaption) {
            initialize(inCaption);
        }
        
        protected ToolbarView createToolbar() {
            ToolbarView aToolbar = new ToolbarView();
            String[][] aBtns = {
                    {"popup.ok",     ToolbarButtonType.ACCEPT.toString()},
                    {"popup.cancel", ToolbarButtonType.CANCEL.toString()},
            };
            ActionListener aListener = new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    String aName = ((Component)e.getSource()).getName();
                    toolbarAction(aName);
                }
            };
            aToolbar.setBorder(BorderFactory.createEmptyBorder());
            aToolbar.setDefaultButtonsVisible(false);
            aToolbar.addButtonsRight(aBtns, aListener);
            return aToolbar;
        }
        
        private void initialize(final String inCaption) {
     
            this.setBorder(
                    BorderFactory.createCompoundBorder(
                            BorderFactory.createLineBorder(Color.black),
                            //BorderFactory.createEmptyBorder(2, 2, 2, 2),
                            BorderFactory.createTitledBorder(inCaption)
                    ));
     
            mToolbar = createToolbar();
            this.setLayout(new BorderLayout());
            this.add(mToolbar, BorderLayout.SOUTH);
        }
        
        protected void toolbarAction(final String inName) {
            if(inName.equals("popup.ok")) {
                fireEvent();
            } else if(inName.equals("popup.cancel")) {
            }
            this.setVisible(false);
        }
     
        public void addListener(ActionListener inListener) {
            synchronized(mMutex) {
                mListenerList.add(inListener);      
            }
        }
        public void removeListener(ActionListener inListener) {
            synchronized(mMutex) {
                mListenerList.remove(inListener);       
            }
        }
     
        private void fireEvent() {
            synchronized (mMutex ) {
                List<ActionListener> aList = Collections.synchronizedList(mListenerList);
                synchronized(aList) {
                    ActionEvent aEvent = new ActionEvent(this, 0, "ok");
                    for(ActionListener aListener : aList)
                        aListener.actionPerformed(aEvent);
                }
            }
        }
     
        private Object mMutex = new Object();
        private ArrayList<ActionListener> mListenerList = new ArrayList<ActionListener>();
        private ToolbarView mToolbar;
    }

    Hier eine Beispiel Implementierung der Klasse

    Code :
    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
    
    import java.awt.BorderLayout;
    import java.awt.Component;
     
    import javax.swing.JTextField;
     
     
    @SuppressWarnings("serial")
    public class PopupTextEditor extends PopupView {
     
        public PopupTextEditor(final String inCaption) {
            super(inCaption);
            initialize();
        }
     
        private void initialize() {
            mTextEditor = new JTextField();
            this.add(mTextEditor, BorderLayout.NORTH);
        }
        
        public void setCustomComponent(Component inComponent) {
            mCustomComponent = inComponent;
            this.add(inComponent, BorderLayout.CENTER);     
        }
        
        public Component getCustomComponent(){
            return mCustomComponent;
        }
     
        public String getValue() {
            return mTextEditor.getText();
        }
     
        private JTextField mTextEditor;
        private Component mCustomComponent;
    }

    Hier wie man es anwendet

    Code :
    1
    2
    3
    4
    5
    6
    7
    
    mPopupCaptionEditor = new PopupTextEditor(ClientResources.getRes("view.caption.editor.title"));
            mPopupCaptionEditor.setCustomComponent(getEigentlicherInhaltView());
            mPopupCaptionEditor.addListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    // Popup Caption Editor Action;
                }
            });

    Code :
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    
    public enum ToolbarButtonType {
        CUSTOM,
        ADD,
        REMOVE,
        EDIT,
        SEARCH,
        HELP,
        NEW,
        CLEAR,
        SAVE,
        ACCEPT,
        CANCEL,
        PRINT,
        ATTACHMENT
    }

    Code :
    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
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    
    import java.awt.BorderLayout;
    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.HashMap;
    import java.util.List;
     
    import javax.swing.AbstractButton;
    import javax.swing.BorderFactory;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JPanel;
    import javax.swing.border.EtchedBorder;
     
    import <BLA>.client.gui.GuiFactory;
    import <BLA>.client.gui.utils.GuiConstants;
    import <BLA>.common.client.res.ClientResources;
     
    @SuppressWarnings("serial")
    public class ToolbarView extends JPanel {
     
        public ToolbarView(boolean inWithRigthArea) {
            initialize(inWithRigthArea);
        }
        public ToolbarView() {
            initialize(true);
        }
     
        private void initialize(boolean inWithRigthArea) {
            this.setOpaque(GuiConstants.OPAQUE);
            this.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
            this.setLayout(new BorderLayout());
     
            mLeftArea = new JPanel();
            mLeftArea.setOpaque(GuiConstants.OPAQUE);
            mLeftArea.setLayout(new FlowLayout(FlowLayout.LEFT));
            this.add(mLeftArea, BorderLayout.CENTER);
            if(inWithRigthArea) {
                mRightArea = new JPanel();
                mRightArea.setOpaque(GuiConstants.OPAQUE);
                mRightArea.setLayout(new FlowLayout(FlowLayout.RIGHT));
                mBtnNavigator = new JButton();
                ImageIcon aIcon = ClientResources.getImage("img.btn.home");
                mBtnNavigator.setIcon(aIcon);
                mBtnNavigator.setToolTipText(ClientResources.getRes("tooltip.btn.home"));
     
                mBtnHelp = new JButton();
                aIcon = ClientResources.getImage("img.btn.help");
                mBtnHelp.setIcon(aIcon);
                mBtnHelp.setToolTipText(ClientResources.getRes("tooltip.btn.help"));
     
                mRightArea.add(mBtnNavigator);
                mRightArea.add(mBtnHelp);
                this.add(mRightArea, BorderLayout.EAST);
     
                ActionListener aActionListener = new ActionListener() {
     
                    public void actionPerformed(ActionEvent e) {
                        if(e.getSource() == mBtnNavigator) {
                            GuiFactory.getProcessCtrl().navigationGoNavigation();
                        } else if(e.getSource() == mBtnHelp) {
                            if(mHelpContext != null) {
                                GuiFactory.getProcessCtrl().navigationGoHelp(mHelpContext);
                            }
                        }
                    }
                };
     
                mBtnNavigator.addActionListener(aActionListener);
                mBtnHelp.addActionListener(aActionListener);
            }
            
            mActionListener = new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    fireEvent(e);
                }
            };
        }
     
        /**
         * { COMMAND ,   ToolbarButtonType , IMAGE , TOOLTIP, CAPTION},
         * @param inButtons
         * @param inListener
         */
        public void addButtons(String[][] inButtons, ActionListener inListener) {
            addButtonsToPanel(mLeftArea, inButtons, inListener);
        }
     
        public void addButtonsRight(String[][] inButtons, ActionListener inListener) {
            addButtonsToPanel(mRightArea, inButtons, inListener);
        }
     
        private void addButtonsToPanel(Container inArea, String[][] inButtons, ActionListener inListener) {
            for(int i=0; i < inButtons.length; i++) {
                String aIdent = inButtons[i][0];
                ToolbarButtonType aType = ToolbarButtonType.valueOf(inButtons[i][1]);
                String aImage = null;
                String aTooltip = null;
                String aCaption = null;
                if(aType == ToolbarButtonType.CUSTOM) {
                } else if(aType == ToolbarButtonType.ADD) {
                    aImage   = "img.btn.add";
                    aTooltip = "tooltip.btn.add";
                    aCaption = "tooltip.btn.add";
                } else if(aType == ToolbarButtonType.REMOVE) {
                    aImage   = "img.btn.delete";
                    aTooltip = "tooltip.btn.delete";
                    aCaption = "tooltip.btn.delete";
                } else if(aType == ToolbarButtonType.EDIT) {
                    aImage   = "img.btn.change";
                    aTooltip = "tooltip.btn.change";
                    aCaption = "tooltip.btn.change";
                } else if(aType == ToolbarButtonType.SEARCH) {
                    aImage   = "img.btn.search";
                    aTooltip = "tooltip.btn.search";
                    aCaption = "tooltip.btn.search";
                } else if(aType == ToolbarButtonType.HELP) {
                    aImage   = "img.btn.help";
                    aTooltip = "tooltip.btn.help";
                    aCaption = "tooltip.btn.help";
                } else if(aType == ToolbarButtonType.NEW) {
                    aImage   = "img.btn.new";
                    aTooltip = "tooltip.btn.new";
                    aCaption = "tooltip.btn.new";
                } else if(aType == ToolbarButtonType.CLEAR) {
                    aImage   = "img.btn.clear";
                    aTooltip = "tooltip.btn.clear";
                    aCaption = "tooltip.btn.clear";
                } else if(aType == ToolbarButtonType.SAVE) {
                    aImage   = "img.btn.save";
                    aTooltip = "tooltip.btn.save";
                    aCaption = "tooltip.btn.save";
                } else if(aType == ToolbarButtonType.ACCEPT) {
                    aImage   = "img.btn.accept";
                    aTooltip = "tooltip.btn.accept";
                    aCaption = "tooltip.btn.accept";
                } else if(aType == ToolbarButtonType.CANCEL) {
                    aImage   = "img.btn.cancel";
                    aTooltip = "tooltip.btn.cancel";
                    aCaption = "tooltip.btn.cancel";
                } else if(aType == ToolbarButtonType.PRINT) {
                    aImage   = "img.btn.print";
                    aTooltip = "tooltip.btn.print";
                    aCaption = "tooltip.btn.print";
                } else if(aType == ToolbarButtonType.ATTACHMENT) {
                    aImage   = "img.btn.attachment";
                    aTooltip = "tooltip.btn.attachment";
                    aCaption = "tooltip.btn.attachment";
                }           
                
                if(inButtons[i].length > 2) {
                  aImage = inButtons[i][2];
                }
                if(inButtons[i].length > 3) {
                  aTooltip = inButtons[i][3];
                }
                if(inButtons[i].length > 4) {
                  aCaption = inButtons[i][4];
                }
     
                AbstractButton aBtn = createButton();
                mBtnMap.put(aIdent, aBtn);
                aBtn.setName(aIdent);
                if(aImage != null) {
                  ImageIcon aImageIcon = ClientResources.getImage(aImage);
                  aBtn.setIcon(aImageIcon);
                }
                if(aTooltip != null) {
                   aBtn.setToolTipText(ClientResources.getRes(aTooltip));
                }
                if(aCaption != null) {          
                  aBtn.setText(ClientResources.getRes(aCaption));
                }
                inArea.add(aBtn);
                aBtn.addActionListener(mActionListener);
                
            }
            addActionListener(inListener);
        }
        
        public AbstractButton getButton(final String inBtnId) {
            return mBtnMap.get(inBtnId);
        }
     
        public void setHelpContext(String inCtx) {
            mHelpContext = inCtx;
        }
     
        public JButton getNavigtorButton() {
            return mBtnNavigator;
        }
     
        public JButton getHelpButton() {
            return mBtnHelp;
        }
        
        public void setDefaultButtonsVisible(boolean inVisible) {
            mBtnHelp.setVisible(inVisible);
            mBtnNavigator.setVisible(inVisible);
        }
     
        public JPanel getRightArea() {
            return mRightArea;
        }
     
        public void addActionListener(ActionListener inListener) {
            synchronized(mMutex) {
                mListenerList.add(inListener);      
            }
        }
        public void removeActionListener(ActionListener inListener) {
            synchronized(mMutex) {
                mListenerList.remove(inListener);       
            }
        }
     
        private void fireEvent(ActionEvent inEvent) {
            synchronized (mMutex ) {
                List<ActionListener> aList = Collections.synchronizedList(mListenerList);
                synchronized(aList) {
                    for(ActionListener aListener : aList)
                        aListener.actionPerformed(inEvent);
                }
            }
        }
     
        public AbstractButton createButton() {
            return new JButton();
        }
        
        private Object mMutex = new Object();
        private ArrayList<ActionListener> mListenerList = new ArrayList<ActionListener>();
        private transient ActionListener mActionListener;
        
        private String mHelpContext=null;
     
        private JPanel mLeftArea;
        private JPanel mRightArea;
        private JButton mBtnNavigator; 
        private JButton mBtnHelp; 
        private HashMap<String, AbstractButton> mBtnMap = new HashMap<String, AbstractButton>();
    }
     

  4. #4
    DarthShader DarthShader ist offline Mitglied Platin
    Registriert seit
    May 2004
    Beiträge
    684
    Hi desanocra,

    Zitat Zitat von desanocra Beitrag anzeigen
    Falls Du tatsächlich erreichen willst das bei einem click daneben sich das Fenster schliesst,
    benutze doch einfach ein popup menu als Dialog.

    Du poppst es auf, und wenn man daneben clickt schliesst es sich.

    Hier ein Beispiel das ich geschrieben habe.
    vielen Dank für Dein ausführliches Beispiel. Ich denke jedoch, dass ich kein Popup dafür verwende. Ich habe eine etwas einfachere Variante gefunden: ich verwende kein echt modalen Dialog. Verliert er jedoch den Fokus, was ich mit einem WindowListener abfange, so schließe ich den Dialog. Das funktionioert bisher ganz gut.
     

Ähnliche Themen

  1. modalen Dialog im Vordergrund anzeigen
    Von sinamine im Forum .NET Windows Forms
    Antworten: 7
    Letzter Beitrag: 25.03.08, 14:45
  2. Antworten: 5
    Letzter Beitrag: 08.01.08, 10:43
  3. Zugriff auf modalen Dialog
    Von jb007 im Forum Swing, Java2D/3D, SWT, JFace
    Antworten: 1
    Letzter Beitrag: 14.05.07, 21:26
  4. Schriftart im Modalen Dialog ändern?
    Von Apollo75 im Forum VisualStudio & MFC
    Antworten: 6
    Letzter Beitrag: 05.06.06, 10:35
  5. Antworten: 5
    Letzter Beitrag: 24.05.05, 12:49