HashSet.add(KeyStroke)?

levinw98

Mitglied
Hallo,

ich weiß zwar nicht, ob ich hier richtig bin, aber:

Ich habe nen kleinen Code geschrieben, bei dem der Tabulator Taste zugewiesen wird, ein JTextField zu leeren, nach dem auslösen der Taste soll das ganze wieder rückgängig gemacht werden, und die Tabulator Taste wieder ganz normal funktionieren.

Java:
public void keyPressed(KeyEvent e) {
				FocusManager fm = FocusManager.getCurrentManager();
				Set<?> newKeys = new HashSet<Object>(
						fm.getDefaultFocusTraversalKeys(FocusManager.FORWARD_TRAVERSAL_KEYS));
				newKeys.remove(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0));
				fm.setDefaultFocusTraversalKeys(FocusManager.FORWARD_TRAVERSAL_KEYS,
						(Set<? extends AWTKeyStroke>) newKeys);
				if (e.getKeyCode() == KeyEvent.VK_TAB) {
					txtcode1.grabFocus();
					txtcode1.setText(null);
					txttext.removeKeyListener(FirstTab);
					txtcode1.removeMouseListener(FirstMouse);
					fm.getDefaultFocusTraversalKeys(FocusManager.FORWARD_TRAVERSAL_KEYS);
					newKeys.add(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0));
					fm.setDefaultFocusTraversalKeys(FocusManager.FORWARD_TRAVERSAL_KEYS,
							(Set<? extends AWTKeyStroke>) newKeys);	
				}
			}

Wie soll ich das machen, dass ich die Tabulator Taste wieder hinzufüge? Mit .add geht es nur, wenn man am Ende noch ein Komma und irgendwas dahinter schreibt. Aber was? Google weiß auch nicht weiter :(
 

knilch

Bekanntes Mitglied
Hi,
Kannst du mal folgendes Versuchen?
Java:
public void keyPressed(KeyEvent e) {
    FocusManager fm = FocusManager.getCurrentManager();
    Set<?> newKeys = new HashSet<Object>(fm.getDefaultFocusTraversalKeys(FocusManager.FORWARD_TRAVERSAL_KEYS));
    newKeys.remove(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0));
    fm.setDefaultFocusTraversalKeys(FocusManager.FORWARD_TRAVERSAL_KEYS, (Set<? extends AWTKeyStroke>) newKeys);
    if (e.getKeyCode() == KeyEvent.VK_TAB) {
        txtcode1.grabFocus();
        txtcode1.setText(null);
        txttext.removeKeyListener(FirstTab);
        txtcode1.removeMouseListener(FirstMouse);
        fm.getDefaultFocusTraversalKeys(FocusManager.FORWARD_TRAVERSAL_KEYS).add((AWTKeyStroke)KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0));
    }
}
Ich weiss aber nicht, ob es funktioniert, da ich das nicht testen konnte....
Wenns nicht geht, kannst du mal den Code posten, damit man sehen kann, was txtCode1 und txttext für instanzen sind?
 
Zuletzt bearbeitet:

levinw98

Mitglied
Die Antwort :)
Exception in thread "AWT-EventQueue-0" java.lang.UnsupportedOperationException
at java.util.Collections$UnmodifiableCollection.add(Unknown Source)
at prinzipp.Decodeframe$2.keyPressed(Decodeframe.java:104)
at java.awt.AWTEventMulticaster.keyPressed(Unknown Source)
at java.awt.Component.processKeyEvent(Unknown Source)
at javax.swing.JComponent.processKeyEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

txtcode1 und txttext sind JTextFields.

Java:
package prinzipp;

import java.awt.*;
import java.awt.event.*;
import java.util.*;

import javax.swing.*;
import javax.swing.FocusManager;

@SuppressWarnings("serial")
public class Decodeframe extends JFrame implements ActionListener {

	private static MouseListener SecondMouse = null;
	private static MouseListener FirstMouse = null;
	private static KeyListener SecondTab = null;
	private static KeyListener FirstTab = null;
	private static JTextField txttext;
	private static JTextField txtcode1;
	private static JTextField txtcode2;
	private JButton ready;
	private JButton delete;
	private JButton cancel;
	public static String output;
	public static String firstDecodeOutput;
	public static String secondDecodeOutput;

	@SuppressWarnings("unchecked")
	public Decodeframe() {

		super();
		
		getContentPane().setLayout(new BorderLayout());

		JPanel mainpanel = new JPanel();
		mainpanel.setLayout(new BoxLayout(mainpanel, BoxLayout.Y_AXIS));

		JLabel lbltext = new JLabel("Zu entschlüsselnder Text:");
		JLabel lblcode1 = new JLabel("Erstes Schlüsselwort: ");
		JLabel lblcode2 = new JLabel("Zweites Schlüsselwort: ");
		JLabel lblinfo = new JLabel(
				"In den Schlüsselwörtern dürfen nur Buchstaben verwendet werden!");
		JLabel lblcodeinfo1 = new JLabel("(Min. 2 Buchstaben)");
		lblcodeinfo1.setFont(new Font("Arial", Font.PLAIN, 10));
		JLabel lblcodeinfo2 = new JLabel("(Min. 2 Buchstaben)");
		lblcodeinfo2.setFont(new Font("Arial", Font.PLAIN, 10));
		JLabel lbltextinfo = new JLabel("(Min. 2 Buchstaben)");
		lbltextinfo.setFont(new Font("Arial", Font.PLAIN, 10));

		FirstTab = new KeyListener() {

			@Override
			public void keyPressed(KeyEvent e) {
				FocusManager fm = FocusManager.getCurrentManager();
				Set<?> newKeys = new HashSet<Object>(
						fm.getDefaultFocusTraversalKeys(FocusManager.FORWARD_TRAVERSAL_KEYS));
				newKeys.remove(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0));
				fm.setDefaultFocusTraversalKeys(FocusManager.FORWARD_TRAVERSAL_KEYS,
						(Set<? extends AWTKeyStroke>) newKeys);
				if (e.getKeyCode() == KeyEvent.VK_TAB) {
					txtcode1.grabFocus();
					txtcode1.setText(null);
					txttext.removeKeyListener(FirstTab);
					txtcode1.removeMouseListener(FirstMouse);
					fm.getDefaultFocusTraversalKeys(FocusManager.FORWARD_TRAVERSAL_KEYS).add((AWTKeyStroke)KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0));
				}
			}

			@Override
			public void keyReleased(KeyEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void keyTyped(KeyEvent e) {
				// TODO Auto-generated method stub

			}

		};

		SecondTab = new KeyListener() {

			@Override
			public void keyPressed(KeyEvent e) {
				FocusManager fm = FocusManager.getCurrentManager();
				Set<?> newKeys = new HashSet<Object>(
						fm.getDefaultFocusTraversalKeys(FocusManager.FORWARD_TRAVERSAL_KEYS));
				newKeys.remove(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0));
				fm.setDefaultFocusTraversalKeys(FocusManager.FORWARD_TRAVERSAL_KEYS,
						(Set<? extends AWTKeyStroke>) newKeys);
				if (e.getKeyCode() == KeyEvent.VK_TAB) {
					txtcode2.grabFocus();
					txtcode2.setText(null);
					txtcode1.removeKeyListener(SecondTab);
					txtcode2.removeMouseListener(SecondMouse);
					fm.getDefaultFocusTraversalKeys(FocusManager.FORWARD_TRAVERSAL_KEYS).add((AWTKeyStroke)KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0));
				}
			}

			@Override
			public void keyReleased(KeyEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void keyTyped(KeyEvent e) {
				// TODO Auto-generated method stub

			}
		};

		FirstMouse = new MouseListener() {

			@Override
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 1) {
					txtcode1.setText(null);
					txtcode1.removeMouseListener(FirstMouse);
					txttext.removeKeyListener(FirstTab);
				}
			}

			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub

			}

		};

		SecondMouse = new MouseListener() {

			@Override
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 1) {
					txtcode2.setText(null);
					txtcode2.removeMouseListener(SecondMouse);
					txtcode1.removeKeyListener(SecondTab);
				}
			}

			@Override
			public void mouseEntered(MouseEvent arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseExited(MouseEvent arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mousePressed(MouseEvent arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseReleased(MouseEvent arg0) {
				// TODO Auto-generated method stub

			}

		};
		
		txttext = new JTextField();
		txttext.addKeyListener(new AL());
		txttext.addKeyListener(FirstTab);
		txtcode1 = new JTextField(
				"Entspircht dem zweiten Schlüsselwort vom Verschlüsseln!");
		txtcode1.addKeyListener(new AL());
		txtcode1.addKeyListener(SecondTab);
		txtcode1.addMouseListener(FirstMouse);
		txtcode2 = new JTextField(
				"Entspircht dem ersten Schlüsselwort vom Verschlüsseln!");
		txtcode2.addKeyListener(new AL());
		txtcode2.addMouseListener(SecondMouse);

		JPanel txtpanel = new JPanel();
		txtpanel.setLayout(new BoxLayout(txtpanel, BoxLayout.X_AXIS));

		txtpanel.add(lbltext);
		txtpanel.add(Box.createRigidArea(new Dimension(5, 0)));
		txtpanel.add(lbltextinfo);

		mainpanel.add(txtpanel);

		JPanel panel1 = new JPanel();
		panel1.setLayout(new BoxLayout(panel1, BoxLayout.X_AXIS));

		panel1.add(Box.createRigidArea(new Dimension(0, 5)));
		panel1.add(txttext);
		panel1.add(Box.createRigidArea(new Dimension(0, 5)));

		mainpanel.add(panel1);

		JPanel c1panel = new JPanel();
		c1panel.setLayout(new BoxLayout(c1panel, BoxLayout.X_AXIS));

		c1panel.add(lblcode1);
		c1panel.add(Box.createRigidArea(new Dimension(5, 0)));
		c1panel.add(lblcodeinfo1);

		mainpanel.add(c1panel);

		JPanel panel2 = new JPanel();
		panel2.setLayout(new BoxLayout(panel2, BoxLayout.X_AXIS));

		panel2.add(Box.createRigidArea(new Dimension(0, 5)));
		panel2.add(txtcode1);
		panel2.add(Box.createRigidArea(new Dimension(0, 5)));

		mainpanel.add(panel2);

		JPanel c2panel = new JPanel();
		c2panel.setLayout(new BoxLayout(c2panel, BoxLayout.X_AXIS));

		c2panel.add(lblcode2);
		c2panel.add(Box.createRigidArea(new Dimension(0, 5)));
		c2panel.add(lblcodeinfo2);

		mainpanel.add(c2panel);

		JPanel panel3 = new JPanel();
		panel3.setLayout(new BoxLayout(panel3, BoxLayout.X_AXIS));

		panel3.add(Box.createRigidArea(new Dimension(0, 5)));
		panel3.add(txtcode2);

		mainpanel.add(panel3);

		JPanel btnpanel = new JPanel();

		btnpanel.setLayout(new BoxLayout(btnpanel, BoxLayout.LINE_AXIS));
		btnpanel.setBorder(BorderFactory.createEmptyBorder(5, 0, 5, 0));

		ready = new JButton("Fertig");
		ready.addActionListener(this);

		delete = new JButton("Löschen");
		delete.addActionListener(this);

		cancel = new JButton("Abbrechen");
		cancel.addActionListener(this);

		JPanel infopanel = new JPanel();
		infopanel.setLayout(new BoxLayout(infopanel, BoxLayout.Y_AXIS));

		infopanel.add(lblinfo);

		btnpanel.add(Box.createHorizontalGlue());
		btnpanel.add(infopanel);
		btnpanel.add(Box.createRigidArea(new Dimension(5, 0)));
		btnpanel.add(ready);
		btnpanel.add(Box.createRigidArea(new Dimension(5, 0)));
		btnpanel.add(delete);
		btnpanel.add(Box.createRigidArea(new Dimension(5, 0)));
		btnpanel.add(cancel);
		btnpanel.add(Box.createRigidArea(new Dimension(5, 0)));

		mainpanel.add(btnpanel);

		getContentPane().add(mainpanel);

		setTitle("Decode");
		setSize(665, 300);
		setLocationRelativeTo(null);
		setVisible(true);
		setResizable(true);
		setMinimumSize(new Dimension(665, 300));
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {

				setVisible(false);

				Menueframe.frame.setVisible(true);

			}
		});
	}

	public void actionPerformed(ActionEvent e) {

		if (e.getSource() == ready) {
			String testtext = txttext.getText();
			String testcode1 = txtcode1.getText();
			String testcode2 = txtcode2.getText();

			testcode1.replaceAll(" ", "");
			testcode2.replaceAll(" ", "");

			if (testtext.length() > 1 && testcode1.length() > 1
					&& testcode2.length() > 2) {

				output = testtext;
				firstDecodeOutput = testcode1;
				secondDecodeOutput = testcode2;

				new Decode();

			} else {
				Object[] options = { "OK" };
				JOptionPane
						.showOptionDialog(
								null,
								"Bitte alle Felder mit ausreichend Buchstaben ausfüllen!",
								"Warnung", JOptionPane.DEFAULT_OPTION,
								JOptionPane.ERROR_MESSAGE, null, options,
								options[0]);
			}

		}

		if (e.getSource() == cancel) {

			setVisible(false);

			Menueframe.frame.setVisible(true);

		}

		if (e.getSource() == delete) {

			txttext.setText(null);
			txtcode1.setText(null);
			txtcode2.setText(null);

		}
	}

	class AL implements KeyListener {
		public AL() {

		}

		public void keyPressed(KeyEvent e) {

			if (e.getKeyCode() == KeyEvent.VK_ENTER) {
				String testtext = txttext.getText();
				String testcode1 = txtcode1.getText();
				String testcode2 = txtcode2.getText();

				testcode1.replaceAll(" ", "");
				testcode2.replaceAll(" ", "");

				if (testtext.length() > 2 && testcode1.length() > 2
						&& testcode2.length() > 2) {
					output = testtext;
					firstDecodeOutput = testcode1;
					secondDecodeOutput = testcode2;

					new Code();
				} else {
					Object[] options = { "OK" };
					JOptionPane
							.showOptionDialog(
									null,
									"Bitte alle Felder mit ausreichend Buchstaben ausfüllen!",
									"Warnung", JOptionPane.DEFAULT_OPTION,
									JOptionPane.ERROR_MESSAGE, null, options,
									options[0]);

				}
			}
		}

		@Override
		public void keyReleased(KeyEvent e) {
			// TODO Auto-generated method stub

		}

		@Override
		public void keyTyped(KeyEvent e) {
			// TODO Auto-generated method stub

		}

	}
}
 
Zuletzt bearbeitet:

levinw98

Mitglied
Mit dem Code wird ein Fenster erzeugt, in dem drei JTextField (mit diversen JLabels) erschaffen werden.

AL() ist ein KeyListener, mit dem die Entertaste den JButton ready zugewiesen wird, wenn der Focus auf einem der JTextFields liegt.

FirstTab und
SecondTab sind KeyListener, die mit der Tabulator Taste verknüpft sind, und im nächsten JTextField den Text löschen, und den Focus hinein setzen. Der KeyListener wird dann entfernt(da der Text nur beim ersten Mal entfernt werden soll) und der MouseListener des nächsten JTextFields wird ebenfalls entfernt (weil da der Text ja dann schon weg ist, und der Text nur einmal in den Felder txtcode1 und txtcode2 entfernt werden soll).

FirstMouse und
SecondMouse sind MouseListener, die den Text im JTextField löschen und den MouseListener und den KeyListener des vorherigen (wegen der Tabulator Taste) JTextFields entfernen.
 

Ähnliche Java Themen


Oben