2012-01-22 7 views
2

私はここと他のサイトにあるすべての提案を試しました。JTabbedPaneのJPanel上のJDialog

このJDialogをJTabbedPaneのパネルの中央に配置することはできません。

標準のJOptionPane.showDialogXYZ()メソッドを使用することはできませんので、閉じるボタンを無効にする必要があります。

アイデア?

import java.awt.BorderLayout; 
import java.awt.Dialog.ModalityType; 
import java.awt.Frame; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.WindowAdapter; 
import java.awt.event.WindowEvent; 
import java.beans.PropertyChangeEvent; 
import java.beans.PropertyChangeListener; 

import javax.swing.JButton; 
import javax.swing.JDialog; 
import javax.swing.JFrame; 
import javax.swing.JOptionPane; 
import javax.swing.JPanel; 
import javax.swing.JTabbedPane; 
import javax.swing.SwingUtilities; 

public class CenterDialog extends JFrame 
{ 
    public CenterDialog() 
    { 
     setResizable(false); 

     setName(getClass().getSimpleName()); 
     setTitle("My Frame"); 
     setSize(300, 300); 

     JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP); 

     // Add the panel 
     tabbedPane.addTab("Button panel", new MyButtonPanel()); 

     add(tabbedPane, BorderLayout.CENTER); 

     getContentPane().add(tabbedPane); 
    } 

    private class MyButtonPanel extends JPanel 
    { 

     public MyButtonPanel() 
     { 
      JButton btnShowDialog = new JButton("Show Dialog"); 
      btnShowDialog.addActionListener(new BtnShowDialogActionListener()); 
      add(btnShowDialog); 
     } 

     private class BtnShowDialogActionListener implements ActionListener 
     { 
      public void actionPerformed(ActionEvent e) 
      { 
       // TODO: Figure out how to center this dialog box 
       final String YES = "Yup"; 
       final String NO = "Nope"; 
       final Object[] options = { YES, NO }; 

       final JOptionPane optionPane = new JOptionPane("Is this centered.", JOptionPane.QUESTION_MESSAGE, 
         JOptionPane.YES_NO_OPTION, null, options, NO); 

       Frame f = JOptionPane.getFrameForComponent(((JButton) e.getSource()).getParent()); 
       final JDialog dialog = new JDialog(f, "Question", ModalityType.APPLICATION_MODAL); 

       dialog.setContentPane(optionPane); 
       dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); 

       dialog.addWindowListener(new WindowAdapter() 
       { 
        public void windowClosing(WindowEvent we) 
        { 
         System.out.println("Ignoring close button"); 
        } 
       }); 

       optionPane.addPropertyChangeListener(new PropertyChangeListener() 
       { 
        public void propertyChange(PropertyChangeEvent e) 
        { 
         String prop = e.getPropertyName(); 

         if (dialog.isVisible() && (e.getSource() == optionPane)) 
         { 
          if (prop.equals(JOptionPane.VALUE_PROPERTY)) 
          { 
           dialog.setVisible(false); 
          } 
         } 
        } 
       }); 

       dialog.pack(); 
       dialog.setVisible(true); 
      } 
     } 
    } 

    private static void createAndShowGUI() 
    { 
     // Create and set up the window. 
     CenterDialog frame = new CenterDialog(); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) 
    { 
     SwingUtilities.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
       try 
       { 
        createAndShowGUI(); 
       } 
       catch (Exception e) 
       { 
        e.printStackTrace(); 
        System.exit(0); 
       } 
      } 
     }); 
    } 

} 

}

+0

返信いただきありがとうございます。 relativeToにどのコンポーネントを使用したかにかかわらず、dialog.setLocationRelativeTo(relativeTo)はダイアログを適切に中央に配置しませんでした。 その後、Window.setLocationRelativeTo()のソースを取得してクラスに配置し、正しくデバッグすることができました。ダイアログがまだパックされていないため、dialog.getBounds()が呼び出され、(0,0)が返されるという問題があります。パックされていないので、次元はありませんでした。これは、setLocationRelativeTo()で数式を混乱させました。解決策は、まずdialog.pack()を呼び出します。 – bigleftie

答えて

7

特定のコンポーネントに、ダイアログの相対を中心とする方法(手動の計算は必要ありません、コンポーネント・ツー・スクリーンは、内部座標マッピングを処理します):

dialog.setLocationRelativeTo(someComponent); 

は、あなたがしたい正確に何に応じて、コンポーネントを選択します。達成:

// center relative to the button 
dialog.setLocationRelativeTo((Component) e.getSource()); 

// center relative to button's parent 
dialog.setLocationRelativeTo(((Component) e.getSource()).getParent()); 

// center relative to the tabbedPane 
JTabbedPane tabbed = // walk the parent chain until you reach it 
dialog.setLocationRelativeTo(tabbed); 
2

私はあなたがグローバル、その後dialog.setLocationRelativeTo(tabbedPane);

編集としてtabbedPaneを設定することにより、後にしているものにやや近いました:より精巧な、そしておそらく視覚的に正確で、解決策を計算することです。 X、あなたの使って、JDialogのy座標、このような何か:

int xDiff = (tabbedPane.getWidth() - dialog.getWidth())/2; 
int x = tabbedPane.getX() + xDiff; 
int yDiff = (tabbedPane.getHeight() - dialog.getHeight())/2; 
int y = tabbedPane.getY() + yDiff; 
dialog.setLocation(x, y); 

私は正直だ場合は、I・ディ完璧に働かないでください、しかし私の理論はあります!

+1

hach​​ .. "グローバルな"ヒットであなたの編集前の答えを読んでいなくなった:-)どちらも欲しくも必要もない - 階層を上に移動するだけ。手動計算に関して:ダイアログの場所はスクリーン座標で、コンポーネントの場所はそれが存在するコンテナに関連しているので、最初にスクリーン座標に変換する必要があります(setLocationRelativeTo ;-)のソースを参照してください) – kleopatra

+0

グローバル私は正しい場所を取得するために見ている間、それは怠惰な回避策のようなものでした。私はtabbedPaneが親に対して相対的に配置されていると感じました。正直なところ、私の実験では、tabbedPaneが素敵でぴったりとしているので、私は怠け者で(再び)、JFrameの位置を数学に使用していました;)私は本当に返事>。<編集:言い換えれば、あなたの答えは+1! – c24w

関連する問題