Hi,
ich habe (schonwieder) ein Problem. Und zwar versuche ich aus dem Hauptfenster (mainFrame) ein extra Fenster mit einem Kalender zu öffnen, der dann ein angewähltes Datum zum Hauptfenster zurück gibt. Dazu habe ich den Kalender (nachoCalendar) in eine Ok / Cancel Dialog Sample Form von Netbeans gepackt.
Mein Ansatz war, beim anklicken des OK Buttons eine Methode im Hauptfenster aufzurufen und dabei den angewählten Wert aus dem Kalender zu übergeben. Der Compiler meckert aber, dass er das Objekt des Hauptfensters genannt "gui" nicht finden kann, obwohl die Klasse mainFrame bzw. das Objekt gui public und im selben Package ist. Wie kann ich also von einem Objekt auf die Methode eines anderen zugreifen bzw. einfach den Wert übergeben? Kling doch eigentlich ganz simpel... ??? :L
calendar Klasse:
Der Fehler tritt in Zeile 119 auf.
mainFrame Klasse:
Die angesprochene Methode ist in Zeile 25.
Ich bin dankbar für jegliche Hilfe!
ich habe (schonwieder) ein Problem. Und zwar versuche ich aus dem Hauptfenster (mainFrame) ein extra Fenster mit einem Kalender zu öffnen, der dann ein angewähltes Datum zum Hauptfenster zurück gibt. Dazu habe ich den Kalender (nachoCalendar) in eine Ok / Cancel Dialog Sample Form von Netbeans gepackt.
Mein Ansatz war, beim anklicken des OK Buttons eine Methode im Hauptfenster aufzurufen und dabei den angewählten Wert aus dem Kalender zu übergeben. Der Compiler meckert aber, dass er das Objekt des Hauptfensters genannt "gui" nicht finden kann, obwohl die Klasse mainFrame bzw. das Objekt gui public und im selben Package ist. Wie kann ich also von einem Objekt auf die Methode eines anderen zugreifen bzw. einfach den Wert übergeben? Kling doch eigentlich ganz simpel... ??? :L
calendar Klasse:
Java:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* calendar.java
*
* Created on 07.09.2011, 21:25:15
*/
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.KeyStroke;
import java.util.Date;
import net.sf.nachocalendar.CalendarFactory;
import net.sf.nachocalendar.components.DatePanel;
/**
*
* @author IndeedPete
*/
public class calendar extends javax.swing.JDialog {
/** A return status code - returned if Cancel button has been pressed */
public static final int RET_CANCEL = 0;
/** A return status code - returned if OK button has been pressed */
public static final int RET_OK = 1;
/** Creates new form calendar */
public calendar(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
// Close the dialog when Esc is pressed
String cancelName = "cancel";
InputMap inputMap = getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), cancelName);
ActionMap actionMap = getRootPane().getActionMap();
actionMap.put(cancelName, new AbstractAction() {
public void actionPerformed(ActionEvent e) {
doClose(RET_CANCEL);
}
});
}
/** @return the return status of this dialog - one of RET_OK or RET_CANCEL */
public int getReturnStatus() {
return returnStatus;
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
okButton = new javax.swing.JButton();
cancelButton = new javax.swing.JButton();
calendarPanel = new net.sf.nachocalendar.components.CalendarPanel();
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
closeDialog(evt);
}
});
okButton.setText("OK");
okButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
okButtonActionPerformed(evt);
}
});
cancelButton.setText("Cancel");
cancelButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cancelButtonActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(calendarPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(89, Short.MAX_VALUE)
.addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(cancelButton))
);
layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {cancelButton, okButton});
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(calendarPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(cancelButton)
.addComponent(okButton)))
);
getRootPane().setDefaultButton(okButton);
pack();
}// </editor-fold>
private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {
gui.setButtonText(String.valueOf((Date) calendarPanel.getValue()));
doClose(RET_OK);
}
private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {
doClose(RET_CANCEL);
}
/** Closes the dialog */
private void closeDialog(java.awt.event.WindowEvent evt) {
doClose(RET_CANCEL);
}
private void doClose(int retStatus) {
returnStatus = retStatus;
setVisible(false);
dispose();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see [url=http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html]How to Set the Look and Feel (The Java™ Tutorials > Creating a GUI With JFC/Swing > Modifying the Look and Feel)[/url]
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(calendar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(calendar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(calendar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(calendar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the dialog */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
calendar dialog = new calendar(new javax.swing.JFrame(), true);
dialog.addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}
});
dialog.setVisible(true);
}
});
}
// Variables declaration - do not modify
private net.sf.nachocalendar.components.CalendarPanel calendarPanel;
private javax.swing.JButton cancelButton;
private javax.swing.JButton okButton;
// End of variables declaration
private int returnStatus = RET_CANCEL;
}
mainFrame Klasse:
Java:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* mainFrame.java
*
* Created on 06.09.2011, 19:40:22
*/
/**
*
* @author Bender
*/
import java.awt.Color; // Bibliothek, um die Hintergrundfarbe zu ändern
public class mainFrame extends javax.swing.JFrame {
/** Creates new form mainFrame */
public mainFrame() {
initComponents();
this.getContentPane().setBackground(Color.WHITE); // Hintergrundfarbe auf weiss ändern
}
public void setButtonText(String text) {
bestellungenCalendarButtonFrom.setText(text);
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jSeparator1 = new javax.swing.JSeparator();
statusLabel = new javax.swing.JLabel();
versionLabel = new javax.swing.JLabel();
mainPane = new javax.swing.JTabbedPane();
bestellungenPanel = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
bestellungenCalendarButtonFrom = new javax.swing.JButton();
bestellungenCalendarButtonTo = new javax.swing.JButton();
jLabel3 = new javax.swing.JLabel();
bestellungenSearchTextfield = new javax.swing.JTextField();
bestellungenFilterCombobox = new javax.swing.JComboBox();
jLabel4 = new javax.swing.JLabel();
kundenPanel = new javax.swing.JPanel();
veranstaltungenPanel = new javax.swing.JPanel();
menuBar = new javax.swing.JMenuBar();
fileMenu = new javax.swing.JMenu();
endMenuButton = new javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Filmtheater");
setBackground(new java.awt.Color(255, 255, 255));
statusLabel.setText("<dummy>");
statusLabel.setName("statusLabel"); // NOI18N
versionLabel.setText("Version 0.0");
mainPane.setBackground(new java.awt.Color(255, 86, 106));
mainPane.setForeground(new java.awt.Color(255, 255, 255));
bestellungenPanel.setBackground(new java.awt.Color(255, 255, 255));
jLabel1.setText("Datum von:");
jLabel2.setText("Datum bis:");
bestellungenCalendarButtonFrom.setText("Wählen...");
bestellungenCalendarButtonFrom.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bestellungenCalendarButtonFromActionPerformed(evt);
}
});
bestellungenCalendarButtonTo.setText("Wählen...");
bestellungenCalendarButtonTo.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bestellungenCalendarButtonToActionPerformed(evt);
}
});
jLabel3.setText("Suche:");
bestellungenFilterCombobox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Alle", "Nicht Bezahlt", "Bezahlt", "Storniert" }));
jLabel4.setText("Filter:");
javax.swing.GroupLayout bestellungenPanelLayout = new javax.swing.GroupLayout(bestellungenPanel);
bestellungenPanel.setLayout(bestellungenPanelLayout);
bestellungenPanelLayout.setHorizontalGroup(
bestellungenPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(bestellungenPanelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(bestellungenCalendarButtonFrom)
.addGap(18, 18, 18)
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(bestellungenCalendarButtonTo)
.addGap(18, 18, 18)
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(bestellungenSearchTextfield, javax.swing.GroupLayout.PREFERRED_SIZE, 225, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jLabel4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(bestellungenFilterCombobox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(113, Short.MAX_VALUE))
);
bestellungenPanelLayout.setVerticalGroup(
bestellungenPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(bestellungenPanelLayout.createSequentialGroup()
.addContainerGap()
.addGroup(bestellungenPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(bestellungenCalendarButtonFrom)
.addComponent(jLabel2)
.addComponent(bestellungenCalendarButtonTo)
.addComponent(jLabel3)
.addComponent(bestellungenSearchTextfield, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel4)
.addComponent(bestellungenFilterCombobox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(457, Short.MAX_VALUE))
);
mainPane.addTab("Bestellungen", bestellungenPanel);
kundenPanel.setBackground(new java.awt.Color(255, 255, 255));
javax.swing.GroupLayout kundenPanelLayout = new javax.swing.GroupLayout(kundenPanel);
kundenPanel.setLayout(kundenPanelLayout);
kundenPanelLayout.setHorizontalGroup(
kundenPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 835, Short.MAX_VALUE)
);
kundenPanelLayout.setVerticalGroup(
kundenPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 491, Short.MAX_VALUE)
);
mainPane.addTab("Kunden", kundenPanel);
veranstaltungenPanel.setBackground(new java.awt.Color(255, 255, 255));
javax.swing.GroupLayout veranstaltungenPanelLayout = new javax.swing.GroupLayout(veranstaltungenPanel);
veranstaltungenPanel.setLayout(veranstaltungenPanelLayout);
veranstaltungenPanelLayout.setHorizontalGroup(
veranstaltungenPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 835, Short.MAX_VALUE)
);
veranstaltungenPanelLayout.setVerticalGroup(
veranstaltungenPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 491, Short.MAX_VALUE)
);
mainPane.addTab("Veranstaltungen", veranstaltungenPanel);
menuBar.setBackground(new java.awt.Color(255, 86, 106));
fileMenu.setBackground(new java.awt.Color(255, 255, 255));
fileMenu.setForeground(new java.awt.Color(255, 255, 255));
fileMenu.setText("Datei");
endMenuButton.setBackground(new java.awt.Color(255, 255, 255));
endMenuButton.setText("Beenden");
endMenuButton.setName("endMenuButton"); // NOI18N
endMenuButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
endMenuButtonActionPerformed(evt);
}
});
fileMenu.add(endMenuButton);
endMenuButton.getAccessibleContext().setAccessibleName("");
menuBar.add(fileMenu);
setJMenuBar(menuBar);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 840, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(statusLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 716, Short.MAX_VALUE)
.addComponent(versionLabel)
.addContainerGap())
.addComponent(mainPane, javax.swing.GroupLayout.DEFAULT_SIZE, 840, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(mainPane, javax.swing.GroupLayout.DEFAULT_SIZE, 519, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(statusLabel)
.addComponent(versionLabel))
.addContainerGap())
);
statusLabel.getAccessibleContext().setAccessibleName("");
pack();
}// </editor-fold>
private void endMenuButtonActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
private void bestellungenCalendarButtonFromActionPerformed(java.awt.event.ActionEvent evt) {
calendar calendarFrame = new calendar(this, true);
calendarFrame.setVisible(true);
}
private void bestellungenCalendarButtonToActionPerformed(java.awt.event.ActionEvent evt) {
calendar calendarFrame = new calendar(this, true);
calendarFrame.setVisible(true);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see [url=http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html]How to Set the Look and Feel (The Java™ Tutorials > Creating a GUI With JFC/Swing > Modifying the Look and Feel)[/url]
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(mainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(mainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(mainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(mainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new mainFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton bestellungenCalendarButtonFrom;
private javax.swing.JButton bestellungenCalendarButtonTo;
private javax.swing.JComboBox bestellungenFilterCombobox;
private javax.swing.JPanel bestellungenPanel;
private javax.swing.JTextField bestellungenSearchTextfield;
private javax.swing.JMenuItem endMenuButton;
private javax.swing.JMenu fileMenu;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JSeparator jSeparator1;
private javax.swing.JPanel kundenPanel;
private javax.swing.JTabbedPane mainPane;
private javax.swing.JMenuBar menuBar;
private javax.swing.JLabel statusLabel;
private javax.swing.JPanel veranstaltungenPanel;
private javax.swing.JLabel versionLabel;
// End of variables declaration
}
Ich bin dankbar für jegliche Hilfe!