public void paint(Graphics g) { imageLabel.setLocation(textField.getX() - 8, 2); super.paint(g); // System.out.println( "panel paint" ) ; }
// msg from JTextfield public void focusGained(FocusEvent e) { if (e.getSource() == textField) { this.fireFocusGained(e.isTemporary()); } else // if the label get's the focus then delegate it to textfield { textField.grabFocus(); // !!! field could be not visible (scrollpane!) // insert mechanism to ensure visibility } textField.setBackground(GUIGlobals.theme.ActiveTextField); }
// msg from JTextfield public void focusLost(FocusEvent e) { if (e.getSource() == textField) { textField.setBackground(GUIGlobals.theme.InactiveTextField); this.fireFocusLost(e.isTemporary()); this.repaint(); } }
public void keyTyped(KeyEvent e) { char code = e.getKeyChar(); if (code == e.VK_ENTER) { // if (dataField != null) { e.setKeyChar(e.CHAR_UNDEFINED); e.setKeyCode(-1); // only fire this event, if the data has been changed if (textField.isDataChanged()) { fireActionPerformed("enter"); } // not shift + enter => focus to next if ((e.getModifiers() & e.SHIFT_MASK) != e.SHIFT_MASK) { this.changeFocusToNext(); } } } else { if (!TKeyLock.keys.isLocked()) { textField.setDataChanged(true); } } }
// icon handling ------------------------------------------------------------ // setzt anhand des Status (fieldState) das entsprechende Icon private final void setImage() { switch (fieldState) { case ERROR_STATE: currentImage = errorIcon.getImage(); imageLabel.setIcon(errorIcon); imageLabel.setToolTipText("failure"); textField.setMargin(bildInsets); break; case REQUIRE_STATE: currentImage = requireIcon.getImage(); imageLabel.setIcon(requireIcon); imageLabel.setToolTipText("required"); textField.setMargin(bildInsets); break; case WARNING_STATE: currentImage = warningIcon.getImage(); imageLabel.setIcon(warningIcon); imageLabel.setToolTipText("warning"); textField.setMargin(bildInsets); break; case CHECKED_STATE: currentImage = checkedIcon.getImage(); imageLabel.setIcon(checkedIcon); imageLabel.setToolTipText("ok"); textField.setMargin(bildInsets); break; case USER_STATE: currentImage = userIcon.getImage(); imageLabel.setIcon(userIcon); imageLabel.setToolTipText("user message"); textField.setMargin(bildInsets); break; default: // case NOTHING_STATE : currentImage = null; imageLabel.setIcon(null); imageLabel.setToolTipText(""); textField.setMargin(leerInsets); } repaint(); }
/** creates a JTextfield with JLabel (contains labelText) and an identifier id */ public TLangTextField(String labelText, int id, FieldPopup popup) { super(); // this.setOpaque( false ) ; this.setLayout(new OverlayLayout(this)); // this.setBorder( BorderFactory.createEtchedBorder()); listenerList = new EventListenerList(); // build the labeled textfield -------------------------------------------- textField = new IDTextField(id, popup) { public void paint(Graphics g) { super.paint(g); if (currentImage != null) { Graphics2D g2 = (Graphics2D) g; g2.drawImage(currentImage, -8, -2, this); } // System.out.println( "paint " +(counter++) ) ; } }; textField.addKeyListener(this); textField.addFocusListener(this); textField.setPopupName(labelText); textField.setMaximumSize(new Dimension(2000, 30)); textField.setFont(GUIGlobals.defaultLanguageFont); // charat position leerInsets = textField.getMargin(); label = new JLabel(labelText, JLabel.TRAILING); label.setLabelFor(textField); label.addFocusListener(this); // insert label and textfield layout = new SpringLayout(); bigPanel = new JPanel(layout); // bigPanel.setOpaque( false ) ; bigPanel.add(label); bigPanel.add(textField); // Adjust constraints for the label so it's at (5,5). layout.putConstraint(SpringLayout.WEST, label, 10, SpringLayout.WEST, bigPanel); layout.putConstraint(SpringLayout.NORTH, label, 6, SpringLayout.NORTH, bigPanel); // Adjust constraints for the text field so it's at // (<label's right edge> + 5, 5). layout.putConstraint(SpringLayout.WEST, textField, 10, SpringLayout.EAST, label); layout.putConstraint(SpringLayout.NORTH, textField, -2, SpringLayout.NORTH, label); // Adjust constraints for the content pane: Its right // edge should be 5 pixels beyond the text field's right // edge, and its bottom edge should be 5 pixels beyond // the bottom edge of the tallest component (which we'll // assume is textField). layout.putConstraint(SpringLayout.EAST, bigPanel, 5, SpringLayout.EAST, textField); layout.putConstraint( SpringLayout.SOUTH, bigPanel, // 8, // SpringLayout.SOUTH, label ) ; 5, SpringLayout.SOUTH, textField); // build image section ---------------------------------------------------- imageLabel = new JLabel(); imageLabel.setOpaque(false); fieldState = REQUIRE_STATE; setImage(); textField.other = imageLabel; // insert image and labeled-field ----------------------------------------- this.add(imageLabel); this.add(bigPanel); // pipe all focus events to local class methods super.addFocusListener(this); }
// -------------------------------------------------------------------------- // Key handling public void addKeyListener(KeyListener listener) { textField.addKeyListener(listener); }
public void markText(int start, int end) { try { textField.getHighlighter().addHighlight(start, end, new DefaultHighlightPainter(Color.RED)); } catch (Exception e) { } }
/** returns the status of data */ public boolean isDataChanged() { return textField.isDataChanged(); }
public void setID(int pMyID) { textField.setMyID(pMyID); }
// -------------------------------------------------------------------------- public int getID() { return textField.getMyID(); }
public void setText(String newText) { textField.setText(newText); }
// -------------------------------------------------------------------------- public String getText() { return textField.getText(); }