Hallo, weiß jemand wie man bei einem JTextfield mit ner TitledBorder bei einem Klick auf diese Border diesen Klick abfangen kann. Also was passiert wenn man auf diese Border klickt?
//Hier setze ich die Border mit den beiden Konstanten justification und position die Methode hat aber mit dem Caret eig nichts zu tun ,oder?
title = BorderFactory.createTitledBorder(blackline,s,TitledBorder.RIGHT,TitledBorder.DEFAULT_POSITION,title_font);
public FocusListener getFocusListener(JComponent c,String s,JLabel label) {
FocusListener f = new FocusListener() {
String temp = "";
@Override
public void focusGained(FocusEvent e) {
title = BorderFactory.createTitledBorder(blackline,s,TitledBorder.RIGHT,TitledBorder.ABOVE_TOP,title_font);
if (c instanceof JTextField) {
if(!((JTextField)c).getText().isBlank()) {
if(temp.isEmpty()) {
((JTextField)c).setText("");
}
else {
((JTextField)c).setText(temp);
}
}
((JTextField)c).setCaretPosition(((JTextField)c).getCaretPosition());
((JTextField)c).setBackground(Color.WHITE);
((JTextField)c).setBorder(title);
((JTextField)c).setCursor(new Cursor(Cursor.TEXT_CURSOR));
label.setBackground(Color.BLACK);
}
}
@Override
public void focusLost(FocusEvent e) {
if (c instanceof JTextField) {
temp = ((JTextField)c).getText();
if(((JTextField)c).getText().isBlank()) {
((JTextField)c).setText(s);
}
else {
((JTextField)c).setText(temp);
}
((JTextField)c).setBackground(Color.LIGHT_GRAY);
((JTextField)c).setBorder(null);
label.setBackground(Color.LIGHT_GRAY);
}
}
};
return f;
}
public DocumentListener getDocumentListener(JTextField textfield) {
DocumentListener documentListener = new DocumentListener() {
public void changedUpdate(DocumentEvent documentEvent) {
caret_position = getLength(documentEvent);
textfield.setCaretPosition(caret_position);
}
public void insertUpdate(DocumentEvent documentEvent) {
caret_position = getLength(documentEvent);
textfield.setCaretPosition(caret_position);
}
public void removeUpdate(DocumentEvent documentEvent) {
caret_position = getLength(documentEvent);
textfield.setCaretPosition(caret_position);
}
};
return documentListener;
}
private int getLength(DocumentEvent documentEvent) {
Document source = documentEvent.getDocument();
int length = source.getLength();
return length;
}
public MouseAdapter getMouseAdapter(JTextField textfield,JLabel label) {
MouseAdapter m = new MouseAdapter() {
Insets insets = textfield.getBorder().getBorderInsets(textfield);
public void mouseEntered(MouseEvent e) {
if(label.getBackground() == Color.LIGHT_GRAY) {
label.setBackground(Color.GRAY);
label.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
textfield.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
}
public void mouseExited(MouseEvent e) {
if(label.getBackground() == Color.GRAY) {
label.setBackground(Color.LIGHT_GRAY);
}
}
public void mouseClicked(MouseEvent e) {
if(textfield.contains(e.getX(),e.getY())) {
if(e.getY() >= insets.top || e.getY() <= insets.bottom || e.getX() >= insets.left || e.getX() <= insets.right) {
textfield.setCaretPosition(textfield.getCaretPosition());
}
}
}
public void mousePressed(MouseEvent e) {
if(textfield.contains(e.getX(),e.getY())) {
if(e.getY() >= insets.top || e.getY() <= insets.bottom || e.getX() >= insets.left || e.getX() <= insets.right) {
textfield.setCaretPosition(textfield.getCaretPosition());
}
}
}
public void mouseReleased(MouseEvent e) {
if(textfield.contains(e.getX(),e.getY())) {
if(e.getY() >= insets.top || e.getY() <= insets.bottom || e.getX() >= insets.left || e.getX() <= insets.right) {
textfield.setCaretPosition(textfield.getCaretPosition());
}
}
if(textfield.getSelectionStart()>0 && textfield.getSelectionEnd() > 0) {
textfield.setSelectionStart(0);
textfield.setSelectionEnd(caret_position);
textfield.setSelectionColor(Color.LIGHT_GRAY);
}
}
};
return m;
}
public MouseMotionListener getMouseMotionListener(JTextField textfield) {
MouseMotionListener m = new MouseMotionListener() {
@Override
public void mouseDragged(MouseEvent e) {
textfield.setCaretPosition(textfield.getCaretPosition());
textfield.setSelectionStart(0);
textfield.setSelectionEnd(caret_position);
textfield.setSelectionColor(Color.LIGHT_GRAY);
}
@Override
public void mouseMoved(MouseEvent e) {
}
};
return m;
}
public JTextField createTextField(String s) {
JTextField textfield = new JTextField();
textfield.setFont(font);
textfield.setSelectionColor(Color.LIGHT_GRAY);
textfield.setBackground(Color.LIGHT_GRAY);
textfield.setText(s);
textfield.setCaretPosition(0);
textfield.setBorder(empty);
textfield.setOpaque(true);
return textfield;
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class Test {
public void run() {
JPanel panel = new JPanel();
JTextField tf = new JTextField(30);
panel.add(tf);
tf.setBorder(null);
panel.setBackground(tf.getBackground());
panel.setBorder(createBorder("Test"));
panel.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
// Panel angeklickt...
}
});
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
private Border createBorder(String title) {
TitledBorder border = new TitledBorder(title);
return border;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new Test().run());
}
}