Seid gegrüsst
Ich habe JTextField gelöscht und wollte stattdessen eine JComboBox einsetzen.
Der Wert der hier zurückgeliefert wird ist ja ein Objekt und muss gemäss meinen Recherchen in einen String konvertiert werden damit ich schliesslich den String "media" verwenden kann.
eclipse bringt mir jedoch immer eine Fehlermeldung auf dieser zeile:
Ich krieg immer eine Nullpointerexeption, was mir jedoch nicht ganz klar ist da ich nicht verstehe wieso das objekt noch nicht existieren soll. Dieses wird doch hier erstellt oder etwa nicht?
Ich habe JTextField gelöscht und wollte stattdessen eine JComboBox einsetzen.
Der Wert der hier zurückgeliefert wird ist ja ein Objekt und muss gemäss meinen Recherchen in einen String konvertiert werden damit ich schliesslich den String "media" verwenden kann.
eclipse bringt mir jedoch immer eine Fehlermeldung auf dieser zeile:
Java:
media = (String) getMedia.getSelectedItem();
Java:
JComboBox getMedia = new JComboBox(auswahl);
Java:
import javax.swing.*;
public class MainGUI // implements ActionListener
{
// int id, year;
String id, year, title, genre, media, stopper; // Strings
JButton button;
private JTextField getID;
private JTextField getTitle;
private JTextField getGenre;
private JTextField getYear;
private JTextField getFilter;
private JComboBox getMedia;
private JTable tableMovies;
private TableRowSorter<TableModel> movieSorter;
private String[] auswahl = {"DVD", "BluRay"};
/**
* @wbp.parser.entryPoint
*/
public static void main(String[] args) throws InstantiationException,
IllegalAccessException, SQLException
{
MainGUI gui = new MainGUI();
gui.los();
}
public void los() throws InstantiationException, IllegalAccessException,
SQLException
{
JFrame frame = new JFrame("Filmverwaltung");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
frame.setBounds(100, 100, 640, 360);
getID = new JTextField();
getID.setBounds(100, 66, 114, 20);
frame.getContentPane().add(getID);
getID.setColumns(10);
getTitle = new JTextField();
getTitle.setBounds(100, 98, 114, 20);
frame.getContentPane().add(getTitle);
getTitle.setColumns(10);
getGenre = new JTextField();
getGenre.setBounds(100, 130, 114, 20);
frame.getContentPane().add(getGenre);
getGenre.setColumns(10);
getYear = new JTextField();
getYear.setBounds(100, 162, 114, 20);
frame.getContentPane().add(getYear);
getYear.setColumns(10);
JComboBox getMedia = new JComboBox(auswahl);
getMedia.setMaximumRowCount(2);
getMedia.setBounds(100, 194, 114, 20);
frame.getContentPane().add(getMedia);
JLabel lblID = new JLabel("Nummer");
lblID.setBounds(12, 68, 55, 16);
frame.getContentPane().add(lblID);
JLabel lblTitel = new JLabel("Titel");
lblTitel.setBounds(12, 100, 55, 16);
frame.getContentPane().add(lblTitel);
JLabel lblGenre = new JLabel("Genre");
lblGenre.setBounds(12, 132, 55, 16);
frame.getContentPane().add(lblGenre);
JLabel lblJahr = new JLabel("Jahr");
lblJahr.setBounds(12, 164, 55, 16);
frame.getContentPane().add(lblJahr);
JLabel lblMedium = new JLabel("Medium");
lblMedium.setBounds(12, 196, 55, 16);
frame.getContentPane().add(lblMedium);
JButton btnSpeichern = new JButton("speichern");
btnSpeichern.setBounds(100, 229, 114, 20);
frame.getContentPane().add(btnSpeichern);
btnSpeichern.addActionListener(new SpeichernListener());
JButton btnLoeschen = new JButton("l\u00F6schen");
btnLoeschen.setBounds(100, 261, 114, 20);
frame.getContentPane().add(btnLoeschen);
btnLoeschen.addActionListener(new LoeschenListener());
JSeparator separator = new JSeparator();
separator.setOrientation(SwingConstants.VERTICAL);
separator.setBackground(Color.BLACK);
separator.setForeground(Color.BLACK);
separator.setBounds(226, 49, 15, 241);
frame.getContentPane().add(separator);
JLabel lblNewLabel = new JLabel("Film: Erfassung & Editierung");
lblNewLabel.setFont(new Font("Dialog", Font.BOLD, 14));
lblNewLabel.setBounds(12, 12, 202, 16);
frame.getContentPane().add(lblNewLabel);
JLabel Aktionen = new JLabel("weitere Aktionen");
Aktionen.setFont(new Font("Dialog", Font.BOLD, 14));
Aktionen.setBounds(244, 13, 141, 16);
frame.getContentPane().add(Aktionen);
JButton btnFilmliste = new JButton("Filmliste - neues Fenster");
btnFilmliste.setBounds(253, 290, 280, 20);
frame.getContentPane().add(btnFilmliste);
getFilter = new JTextField();
getFilter.setBounds(253, 66, 170, 20);
frame.getContentPane().add(getFilter);
getFilter.setColumns(10);
JButton btnFilter = new JButton("Filter");
btnFilter.setBounds(435, 66, 98, 20);
frame.getContentPane().add(btnFilter);
btnFilter.addActionListener(new FilterActionListener());
JScrollPane scrollPaneMovies = new JScrollPane();
scrollPaneMovies.setBounds(253, 98, 280, 180);
frame.getContentPane().add(scrollPaneMovies);
tableMovies = new JTable();
TableModel MovieTableModel = new MovieTableModel();
((ch.haag.Start.MovieTableModel) MovieTableModel).dataselect();
tableMovies.setModel(MovieTableModel);
movieSorter = new TableRowSorter<TableModel>(MovieTableModel);
tableMovies.setRowSorter(movieSorter);
scrollPaneMovies.setViewportView(tableMovies);
frame.setVisible(true);
btnFilmliste.addActionListener(new FilmlisteListener());
//copy
//JScrollPane scrollPaneContacts = new JScrollPane();
//contentPane.add(scrollPaneContacts, BorderLayout.CENTER);
//tableContacts = new JTable();
//TableModel MovieTableModel = new MovieTableModel();
//((ch.haag.Start.MovieTableModel) MovieTableModel).dataselect();
//tableContacts.setModel(MovieTableModel);
//contactSorter = new TableRowSorter<TableModel>(MovieTableModel);
//tableContacts.setRowSorter(contactSorter);
//scrollPaneContacts.setViewportView(tableContacts);
//panelFilter = new JPanel();
//contentPane.add(panelFilter, BorderLayout.NORTH);
//panelFilter.setLayout(new MigLayout("", "[][5][grow][]", "[][5]"));
//copy
}
class SpeichernListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
Connection con = null;
try
{
Statement stmt;
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection(
"jdbc:mysql://localhost/movies", "root", "");
stmt = con.createStatement(); // Generiert ein Object vom
// Typ
// Statement
id = getID.getText();
title = getTitle.getText();
genre = getGenre.getText();
year = getYear.getText();
media = (String) getMedia.getSelectedItem();
stmt.executeUpdate("INSERT INTO movies(id,title,genre,year,media) VALUES('"
+ id
+ "','"
+ title
+ "','"
+ genre
+ "','"
+ year
+ "','" + media + "')");
} catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} finally
{
try
{
if (con != null)
con.close();
} catch (SQLException e)
{
}
}
}
}
class LoeschenListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
Connection con = null;
try
{
Statement stmt;
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection(
"jdbc:mysql://localhost/movies", "root", "");
stmt = con.createStatement(); // Generiert ein Object vom
// Typ
// Statement
id = getID.getText();
stmt.executeUpdate("DELETE FROM movies WHERE id = '" + id
+ "'");
} catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} finally
{
try
{
if (con != null)
con.close();
} catch (SQLException e)
{
}
}
}
}
class FilmlisteListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
// block new listenansicht
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try
{
MovieListNewFrame frame = new MovieListNewFrame();
frame.setVisible(true);
} catch (Exception e)
{
e.printStackTrace();
}
}
});
}
// block fertig
}
}
class FilterActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e)
{
String filterText = getFilter.getText();
if(filterText == null || filterText.length() == 0) {
movieSorter.setRowFilter(null);
}
try {
movieSorter.setRowFilter(RowFilter.regexFilter(filterText));
} catch(PatternSyntaxException ex) {
getFilter.setText("Falsche Eingabe");
}
}
}
}