2013-09-02 5 views

답변

6

은 "견본"의 크기는 (기본값은 10 인) 필요에 따라 조정할 수 있도록 두 가지 UI 속성에 의해 제어됩니다 : 팔레트 자체를 사용자 정의

int length = 20; 
UIManager.put("ColorChooser.swatchesRecentSwatchSize", new Dimension(length, length)); 
UIManager.put("ColorChooser.swatchesSwatchSize", new Dimension(length, length)); 

AbstractColorChooserPanel가의 사용자 지정 하위 클래스를 필요 , 주로 ac & javx.swing.colorchooser에서 DefaultSwatchChooserPanel의 p 작업 (패키지이기 때문에). 기본 설정을 사용자 정의 패널로 바꾸십시오.

JColorChooser chooser = new JColorChooser(); 
List<AbstractColorChooserPanel> choosers = 
     new ArrayList<>(Arrays.asList(chooser.getChooserPanels())); 
choosers.remove(0); 
MySwatchChooserPanel swatch = new MySwatchChooserPanel(); 
choosers.add(0, swatch); 
chooser.setChooserPanels(choosers.toArray(new AbstractColorChooserPanel[0])); 
2

enter image description here

를 사용하여 쉽게 해결에 대한 JColorChooser

  • 의 원하는 부분을 사용

    import java.awt.*; 
    import java.awt.event.*; 
    import javax.swing.*; 
    import javax.swing.colorchooser.AbstractColorChooserPanel; 
    import javax.swing.event.*; 
    import javax.swing.plaf.metal.MetalComboBoxIcon; 
    
    public class DropDownComponent2 { 
    
        private JWindow _window; 
        private boolean _windowShouldBeShown = false; 
        private JComponent _component; 
        private AbstractButton _button; 
        private JFrame _ownerFrame; 
    
        public DropDownComponent2(JFrame ownerFrame, JComponent component, AbstractButton button) { 
         _ownerFrame = ownerFrame; 
         _component = component; 
         _button = button; 
         _button.addActionListener(new ActionListener() { 
          @Override 
          public void actionPerformed(ActionEvent e) { 
           _window.setVisible(false); 
           Point pt = _button.getLocationOnScreen(); 
           pt.translate(0, _button.getHeight()); 
           _window.setLocation(pt); 
           showWindow(); 
           _windowShouldBeShown = true; 
           Window[] wins = Window.getWindows(); 
           System.out.println(wins.length); 
          } 
         }); 
         _button.addAncestorListener(new AncestorListener() { 
          @Override 
          public void ancestorAdded(AncestorEvent event) { 
           _window.setVisible(false); 
          } 
    
          @Override 
          public void ancestorRemoved(AncestorEvent event) { 
           _window.setVisible(false); 
          } 
    
          @Override 
          public void ancestorMoved(AncestorEvent event) { 
           if (event.getSource() != _window) { 
            System.out.println("Ansestor moved"); 
            _window.setVisible(false); 
           } 
          } 
         }); 
         Toolkit.getDefaultToolkit().addAWTEventListener(
           new AWTEventListener() { 
            @Override 
            public void eventDispatched(AWTEvent event) { 
             if (event.getID() == MouseEvent.MOUSE_CLICKED) { 
              if (!_window.getBounds().contains(MouseInfo.getPointerInfo().getLocation())) { 
               if (_windowShouldBeShown) { 
                _windowShouldBeShown = false; 
               } else { 
                _window.setVisible(false); 
               } 
              } 
             } 
            } 
           }, AWTEvent.MOUSE_EVENT_MASK); 
         _window = new JWindow(_ownerFrame); 
         _window.getContentPane().add(component); 
         _window.addWindowFocusListener(new WindowAdapter() { 
          @Override 
          public void windowLostFocus(WindowEvent evt) { 
           System.out.println("window lost focus"); 
           _window.setVisible(false); 
          } 
         }); 
         _window.pack(); 
        } 
    
        private Rectangle getScreenRect() { 
         return new Rectangle(java.awt.Toolkit.getDefaultToolkit().getScreenSize()); 
        } 
    
        public void showWindow() { 
         Rectangle screenRect = getScreenRect(); 
         Rectangle windowRect = _window.getBounds(); 
         int sx1 = screenRect.x; 
         int sx2 = screenRect.x + screenRect.width; 
         int sy1 = screenRect.y; 
         int sy2 = screenRect.y + screenRect.height; 
         int wx1 = windowRect.x; 
         int wx2 = windowRect.x + windowRect.width; 
         int wy1 = windowRect.y; 
         int wy2 = windowRect.y + windowRect.height; 
         if (wx2 > sx2) { 
          _window.setLocation(wx1 - (wx2 - sx2), _window.getY()); 
         } 
         if (wx1 < sx1) { 
          _window.setLocation(0, _window.getY()); 
         } 
         if (wy2 > sy2) { 
          _window.setLocation(_window.getX(), wy1 - (wy2 - wy1)); 
         } 
         if (wy2 < sy1) { 
          _window.setLocation(_window.getX(), 0); 
         } 
         SwingUtilities.invokeLater(new Runnable() { 
          @Override 
          public void run() { 
           _window.setVisible(true); 
          } 
         }); 
        } 
    
        public void hideWindow() { 
         SwingUtilities.invokeLater(new Runnable() { 
          @Override 
          public void run() { 
           _window.setVisible(false); 
          } 
         }); 
        } 
    } 
    
    class DropDownFrame extends JFrame { 
    
        private static final long serialVersionUID = 1L; 
        private JButton _button; 
        private JColorChooser _colorChooser; 
        private DropDownComponent2 _dropDown; 
        private JWindow _window; 
    
        public DropDownFrame() { 
         _colorChooser = new JColorChooser(); 
         _colorChooser.setPreviewPanel(new JPanel()); 
         _colorChooser.setColor(Color.RED); 
         // Remove panels other than Swatches 
         AbstractColorChooserPanel[] panels = _colorChooser.getChooserPanels(); 
         for (int i = 0; i < panels.length; i++) { 
          if (!panels[i].getDisplayName().equals("Swatches")) { 
           _colorChooser.removeChooserPanel(panels[i]); 
          } else { 
           JPanel panel = panels[i]; 
           System.out.println(panel.getComponentCount()); // 1 
           System.out.println(panel.getPreferredSize()); //width=424,height=112 
           System.out.println(panel.getLayout()); //FlowLayout[hgap=5,vgap=5,align=center] 
          } 
         } 
         _colorChooser.getSelectionModel().addChangeListener(new ChangeListener() { 
          @Override // ### I think the key point is there 
          public void stateChanged(ChangeEvent e) { 
           _button.setForeground(_colorChooser.getColor()); 
           _dropDown.hideWindow(); 
          } 
         }); 
         _button = new JButton("Show JWindow"); 
         _button.setIcon(new MetalComboBoxIcon()); 
         _button.setHorizontalTextPosition(SwingConstants.LEFT); 
         this.getContentPane().add(_button); 
         _dropDown = new DropDownComponent2(DropDownFrame.this, _colorChooser, _button); 
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
         pack(); 
         setVisible(true); 
        } 
    
        public static void main(String args[]) { 
         SwingUtilities.invokeLater(new Runnable() { 
          @Override 
          public void run() { 
           DropDownFrame dropDownFrame = new DropDownFrame(); 
          } 
         }); 
        } 
    } 
    
  • +0

    감사합니다. – rxx