Hallo Leute,
ich habe ein weiteres Problem, bei dem ich nicht mehr weiter weiß.
Ich möchte die Eingaben aus den Textfeldern der JTextFields „abholen" können, sodass ich diese mit den Klickkoordinaten der Maus zusammenführen bzw. ausgeben kann (und für weitere Zwecke verwenden kann).
Die Frage, die sich für mich jetzt stellt ist: Wo und wie?
Der betreffende Code:
Probiert habe ich, die geklickten Koordinaten abzuholen, doch mir gelingt es immer nur beim Einmaligen Ausführen des Codes und da sind die Koordinaten natürlich immer „null".
Die Ausgabe in der HandlerClass selbst funktioniert (der geklickte Punkt wird immer wieder neu auf der Konsole ausgegeben - bei entsprechendem Sysout in der mousePressed-Methode).
Also war die Idee nun, die Textfeldeingaben in der mousePressed-Methode abzuholen.
Ich weiß leider wirklich nicht mehr weiter, vielen Dank schonmal im voraus.
Gruß
ich habe ein weiteres Problem, bei dem ich nicht mehr weiter weiß.
Ich möchte die Eingaben aus den Textfeldern der JTextFields „abholen" können, sodass ich diese mit den Klickkoordinaten der Maus zusammenführen bzw. ausgeben kann (und für weitere Zwecke verwenden kann).
Die Frage, die sich für mich jetzt stellt ist: Wo und wie?
Der betreffende Code:
Java:
public class MenuBar extends JFrame{
private JLabel statusBar;
private JPanel mousePanel;
private static Point mousePoint;
private class HandlerClass implements MouseListener, MouseMotionListener {
// Mouse events for MouseListener
public void mouseClicked(MouseEvent event) {
statusBar.setText(String.format("Clicked at %d %d", event.getX(), event.getY()));
}
public void mousePressed(MouseEvent event) {
statusBar.setText("You pressed down the mouse");
creationPanel(null);
Point clickPoint = new Point(event.getX(), event.getY());
mousePoint = clickPoint;
}
public Point getMousePoint() {
return mousePoint;
}
public void mouseReleased(MouseEvent event) {
statusBar.setText("You released the mouse button");
}
public void mouseEntered(MouseEvent event) {
statusBar.setText("You entered the area");
mousePanel.setBackground(Color.RED);
}
public void mouseExited(MouseEvent event) {
statusBar.setText("The mouse has left the window");
mousePanel.setBackground(Color.WHITE);
}
// Mouse events for MouseMotionListener
public void mouseDragged(MouseEvent event) {
statusBar.setText("You are dragging the mouse");
}
public void mouseMoved(MouseEvent event) {
statusBar.setText("X: " + event.getX() + "\t Y: " + event.getY());
}
}
private void creationPanel(JFrame frame) {
JPanel panelThis = new JPanel(new BorderLayout(5, 5));
JPanel labels = new JPanel(new GridLayout(0, 1, 2, 2));
labels.add(new JLabel("Station Name", SwingConstants.RIGHT));
labels.add(new JLabel("Value", SwingConstants.RIGHT));
panelThis.add(labels, BorderLayout.WEST);
JPanel controls = new JPanel(new GridLayout(0, 1, 2, 2));
JTextField stationName = new JTextField("Enter Station Name");
controls.add(stationName);
JTextField values = new JTextField("Enter Value");
controls.add(values);
panelThis.add(controls, BorderLayout.CENTER);
JOptionPane.showMessageDialog(null, panelThis, "Create Station", JOptionPane.QUESTION_MESSAGE);
}
public MenuBar() {
//Set frame
setTitle("Menu");
setSize(700, 700);
//Create the mouse panel
mousePanel = new JPanel();
mousePanel.setBackground(Color.WHITE);
add(mousePanel, BorderLayout.CENTER);
//Create status bar
statusBar = new JLabel("default");
add(statusBar, BorderLayout.SOUTH);
//Create menu bar
JMenuBar menuBar = new JMenuBar();
//Add menu bar to the frame
setJMenuBar(menuBar);
//Define and add two drop down menus to the frame
JMenu fileMenu = new JMenu("Data");
JMenu editMenu = new JMenu("Edit");
menuBar.add(fileMenu);
menuBar.add(editMenu);
//Create and add simple menu items to the drop down menu
JMenuItem openItem = new JMenuItem("Open CSV");
JMenuItem newItem = new JMenuItem("New");
JMenuItem exitItem = new JMenuItem("Exit");
JMenuItem copyItem = new JMenuItem("Copy");
JCheckBoxMenuItem clickItem = new JCheckBoxMenuItem("Mouse GPS");
fileMenu.add(openItem);
fileMenu.add(newItem);
fileMenu.add(exitItem);
editMenu.add(copyItem);
editMenu.add(clickItem);
// Add a dialog box
//Object dialogBox = JOptionPane.showInputDialog(parentComponent, "This is a test");
/* Add Listener to the menu bar
* PerformedAction will be invoked if button is clicked
*/
// File chooser pops up if "Open" is clicked
openItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Creates a file filter to the pop up menu, so that only CSV files are choosable
FileFilter csvFilter = new FileNameExtensionFilter("CSV", "csv");
JFileChooser fileJuice = new JFileChooser();
// Adds and sets the created file filter to default
fileJuice.addChoosableFileFilter(csvFilter);
fileJuice.setFileFilter(csvFilter);
fileJuice.showOpenDialog(null);
//Adds "Open" to the menu dialog
fileJuice.setDialogType(JFileChooser.OPEN_DIALOG);
//Opens selected File
String thatFilePath = fileJuice.getSelectedFile().getAbsolutePath();
CSVReader csvOne = new CSVReader(thatFilePath);
csvOne.CSV();
}
});
exitItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
//Add new handler
HandlerClass mouseHandler = new HandlerClass();
Point p1 = mouseHandler.getMousePoint();
clickItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (clickItem.getState() == true) {
mousePanel.addMouseListener(mouseHandler);
mousePanel.addMouseMotionListener(mouseHandler);
}
else if (clickItem.getState() == false) {
mousePanel.removeMouseListener(mouseHandler);
mousePanel.removeMouseMotionListener(mouseHandler);
statusBar.setText("Mouse Mode OFF");
}
}
});
}
public static void main(String[] args) {
MenuBar me = new MenuBar();
me.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
me.setVisible(true);
}
}
Probiert habe ich, die geklickten Koordinaten abzuholen, doch mir gelingt es immer nur beim Einmaligen Ausführen des Codes und da sind die Koordinaten natürlich immer „null".
Die Ausgabe in der HandlerClass selbst funktioniert (der geklickte Punkt wird immer wieder neu auf der Konsole ausgegeben - bei entsprechendem Sysout in der mousePressed-Methode).
Also war die Idee nun, die Textfeldeingaben in der mousePressed-Methode abzuholen.
Ich weiß leider wirklich nicht mehr weiter, vielen Dank schonmal im voraus.
Gruß