2013-04-20 6 views
1

입력 양식이 사용자와 다를 때 textField 뒤에 경고 아이콘을 설정하는 클래스가 있습니다. 텍스트 필드의 경우이 클래스는 완벽하게 작동하지만 textArea와 함께 사용하려고하면 경고 아이콘이 올바른 위치에 설정되지 않습니다. 여기 아이콘이 GlassPane에 바로 설정되어 있지 않습니다.

을 설정하고 경고 아이콘 제거하는 클래스입니다 : 나는 텍스트 영역에 대한 getY() 함수는 항상 0을 돌려주는 것을 발견했다

public class GlassValidationPane extends JComponent { 
    private HashMap<Component, JLabel> warningLabels = new HashMap<>(); 
    private ImageIcon warningIcon; 
    private final ImageUtilities iU = new ImageUtilities(); 

    public GlassValidationPane() { 
     setLayout(null); 
     setOpaque(false); 
     Icon icon = UIManager.getIcon("OptionPane.warningIcon"); 
     int imgW = icon.getIconWidth(); 
     int imgH = icon.getIconHeight(); 
     BufferedImage img = iU.getBufferedImageOfIcon(icon, imgW, imgH); 
     warningIcon = new ImageIcon(iU.resize(img, 18, 18)); 
    } 

    void showWarningIcon(Component c) { 
     if (warningLabels.containsKey(c)) { 
      return; 
     } 

     JLabel label = new JLabel(); 
     label.setIcon(warningIcon); 

     //int x=c.getX();//this will make it insode the component 
     int x = c.getWidth() + c.getX() + label.getIcon().getIconWidth();//this makes it appear outside/next to component); 
     int y = c.getY(); 
     System.out.println("ïn show warning: " + y); 

     label.setBounds(x, y, label.getIcon().getIconWidth(), label.getIcon().getIconHeight()); 
     add(label); 
     label.setVisible(true); 
     revalidate(); 
     repaint(); 
     warningLabels.put(c, label); 
    } 

    public void removeWarningIcon(Component c) { 
     for (Map.Entry<Component, JLabel> entry : warningLabels.entrySet()) { 
      Component component = entry.getKey(); 
      JLabel jLabel = entry.getValue(); 
      if (component == c) { 
       remove(jLabel); 
       revalidate(); 
       repaint(); 
       break; 
      } 
     } 
     warningLabels.remove(c); 
    } 

    public void refreshLocations() { 
     for (Map.Entry<Component, JLabel> entry : warningLabels.entrySet()) { 
      Component c = entry.getKey(); 
      JLabel label = entry.getValue(); 
      int x = c.getWidth() + c.getX() + label.getIcon().getIconWidth();//this makes it appear outside/next to component 
      int y = c.getY(); 

      label.setBounds(x, y, label.getIcon().getIconWidth(), label.getIcon().getIconHeight()); 
      revalidate(); 
      repaint(); 
     } 
    } 

    public boolean allSet(){ 

     if(!warningLabels.isEmpty()){ 
      JOptionPane.showMessageDialog(null, "Please fill every text field in, or adjust te wrong input", "Empty input/Wrong input", JOptionPane.ERROR_MESSAGE); 
      return false; 
     } 
     return true; 
    } 

private class ImageUtilities { 

    public BufferedImage resize(BufferedImage image, int width, int height) { 
     BufferedImage bi = new BufferedImage(width, height, BufferedImage.TRANSLUCENT); 
     Graphics2D g2d = (Graphics2D) bi.createGraphics(); 
     g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY)); 
     g2d.drawImage(image, 0, 0, width, height, null); 
     g2d.dispose(); 
     return bi; 
    } 

    public BufferedImage getBufferedImageOfIcon(Icon icon, int imgW, int imgH) { 
     BufferedImage img = new BufferedImage(imgW, imgH, BufferedImage.TYPE_INT_ARGB); 
     Graphics2D g2d = (Graphics2D) img.getGraphics(); 
     g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
     icon.paintIcon(null, g2d, 0, 0); 
     g2d.dispose(); 
     return img; 
    } 
} 


} 

을하지만, 항상 반환 이유를 찾을 수없는

public class JobInput extends JFrame { 

    private JPanel contentPane; 
    private JTextField txtJobId; 
    private GlassValidationPane gvp; 
    private JTextArea textAreaDesription; 
    private boolean INSERT; 

    /** 
    * Create the frame. 
    */ 
    public JobInput(String titel, boolean INSERT) { 
     setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); 
     addWindowListener(new WindowAdapter() { 
      @Override 
      public void windowClosing(WindowEvent e) { 
       setVisible(false); 
       dispose(); 
      } 
     }); 
     setBounds(100, 100, 296, 300); 
     contentPane = new JPanel(); 
     contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
     setContentPane(contentPane); 
     GridBagLayout gbl_contentPane = new GridBagLayout(); 
     gbl_contentPane.columnWidths = new int[]{0, 0, 0}; 
     gbl_contentPane.rowHeights = new int[]{0, 0, 0, 0, 0, 0}; 
     gbl_contentPane.columnWeights = new double[]{0.0, 0.0, Double.MIN_VALUE}; 
     gbl_contentPane.rowWeights = new double[]{0.0, 0.0, 0.0, 1.0, 0.0, Double.MIN_VALUE}; 
     contentPane.setLayout(gbl_contentPane); 

     JLabel lblTitel = new JLabel(titel); 
     lblTitel.setFont(new Font("Arial", Font.BOLD, 15)); 
     GridBagConstraints gbc_lblTitel = new GridBagConstraints(); 
     gbc_lblTitel.gridwidth = 2; 
     gbc_lblTitel.insets = new Insets(0, 0, 5, 0); 
     gbc_lblTitel.gridx = 0; 
     gbc_lblTitel.gridy = 0; 
     contentPane.add(lblTitel, gbc_lblTitel); 

     JSeparator separator = new JSeparator(); 
     GridBagConstraints gbc_separator = new GridBagConstraints(); 
     gbc_separator.fill = GridBagConstraints.BOTH; 
     gbc_separator.gridwidth = 2; 
     gbc_separator.insets = new Insets(0, 0, 5, 0); 
     gbc_separator.gridx = 0; 
     gbc_separator.gridy = 1; 
     contentPane.add(separator, gbc_separator); 

     JLabel lblJobid = new JLabel("JobID"); 
     GridBagConstraints gbc_lblJobid = new GridBagConstraints(); 
     gbc_lblJobid.anchor = GridBagConstraints.EAST; 
     gbc_lblJobid.insets = new Insets(0, 0, 5, 5); 
     gbc_lblJobid.gridx = 0; 
     gbc_lblJobid.gridy = 2; 
     contentPane.add(lblJobid, gbc_lblJobid); 

     txtJobId = new JTextField(); 
     GridBagConstraints gbc_txtJobId = new GridBagConstraints(); 
     gbc_txtJobId.insets = new Insets(0, 0, 5, 0); 
     gbc_txtJobId.fill = GridBagConstraints.HORIZONTAL; 
     gbc_txtJobId.gridx = 1; 
     gbc_txtJobId.gridy = 2; 
     contentPane.add(txtJobId, gbc_txtJobId); 
     txtJobId.setColumns(10); 

     JLabel lblDescription = new JLabel("Description"); 
     GridBagConstraints gbc_lblDescription = new GridBagConstraints(); 
     gbc_lblDescription.anchor = GridBagConstraints.NORTH; 
     gbc_lblDescription.insets = new Insets(0, 0, 5, 5); 
     gbc_lblDescription.gridx = 0; 
     gbc_lblDescription.gridy = 3; 
     contentPane.add(lblDescription, gbc_lblDescription); 



     textAreaDesription = new JTextArea(); 
     textAreaDesription.setLineWrap(true); 
     textAreaDesription.setWrapStyleWord(true); 
     GridBagConstraints gbc_textArea = new GridBagConstraints(); 
     gbc_textArea.insets = new Insets(0, 0, 5, 0); 
     gbc_textArea.fill = GridBagConstraints.BOTH; 
     gbc_textArea.gridx = 1; 
     gbc_textArea.gridy = 3; 
     JScrollPane scroll = new JScrollPane (textAreaDesription, 
       JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 

     contentPane.add(scroll, gbc_textArea); 

     JButton btnOk = new JButton("Ok"); 
     GridBagConstraints gbc_btnOk = new GridBagConstraints(); 
     gbc_btnOk.gridwidth = 2; 
     gbc_btnOk.gridx = 0; 
     gbc_btnOk.gridy = 4; 
     contentPane.add(btnOk, gbc_btnOk); 

     gvp = new GlassValidationPane(); 

     FocusAdapter fl = new FocusAdapter() { 
      @Override 
      public void focusGained(FocusEvent fe) { 
       super.focusGained(fe); 
       ((JTextComponent) fe.getSource()).setBorder(BorderFactory.createLineBorder(Color.gray)); 
      } 

      public void focusLost(FocusEvent fe) { 
       super.focusLost(fe); 
       if(fe.getSource().equals(txtJobId)){ 
        validationForInteger(txtJobId); 
       } else if(fe.getSource().equals(textAreaDesription)){ 
        validationForText(textAreaDesription); 
       } else{ 
        gvp.removeWarningIcon(((Component) fe.getSource())); 
        ((JTextComponent) fe.getSource()).setBorder(BorderFactory.createLineBorder(Color.gray)); 
       } 
      } 
     }; 
     txtJobId.addFocusListener(fl); 
     textAreaDesription.addFocusListener(fl); 
     setGlassPane(gvp); 
     gvp.setVisible(true); 
    } 

    private void validationForInteger(JTextComponent comp){ 
     String temp = comp.getText(); 
     if(temp.matches("^[1-9]\\d*$")){ 
      setGreen(comp); 
     } else { 
      setRed(comp); 
     } 
    } 

    private void validationForText(JTextComponent comp) { 
     System.out.println("In validation for text " + textAreaDesription.getY()); 
     String temp = comp.getText(); 
     if (temp.matches("^[a-zA-Z0-9][a-zA-Z0-9\\s_/-]+$")) { 
      setGreen(comp); 
     } else { 
      setRed(comp); 
     } 
    } 

    private void setRed(JTextComponent comp) { 
     comp.setBorder(BorderFactory.createLineBorder(Color.red)); 
     gvp.showWarningIcon(comp); 
    } 

    private void setGreen(JTextComponent comp) { 
     comp.setBorder(BorderFactory.createLineBorder(Color.green)); 
     gvp.removeWarningIcon(comp); 

    } 

} 

따라서 초점 listners가 검증을 요구하며 거기 classValidaionPane가 호출되어야한다 여기에 0는 클래스 ClaxxValidationPane를 호출하는 코드입니다. 그것이 호출되면 그것은 잘못되었다 (하지만 textArea의 경우에만 textField가 아닌) 누군가가 나를 도와 줄 수 있을까?

+0

[여기] (http://stackoverflow.com/q/8715807/714968) 및 [여기] (http://stackoverflow.com/a/9734016/714968)를 참조하십시오. JViewport에서도 마찬가지입니다. (JScrollPane의 가시 사각형) – mKorbel

답변

2

JTextArea의 부모 구성 요소가 JTextField의 부모 구성 요소와 같지 않습니다. JTextArea는 JScrollPane 내부에 있으므로. 게시 한 모든 코드를 읽고 이해하는 데 시간을 할애하지는 않았지만 JTextArea의 위치가 아니라 JScrollPane의 위치를 ​​기준으로 레이블을 배치해야합니다.

+0

감사합니다. 이제는 실제로 논리적입니다. – Bjorn

관련 문제