Der Vollständigkeit halber alle drei Klassen
1.code
2.GUI
3.Handler
(ich entschuldige mich jetzt schonmal fürs naming, habs noch nicht überarbeitet...
1.code
2.GUI
3.Handler
(ich entschuldige mich jetzt schonmal fürs naming, habs noch nicht überarbeitet...
Java:
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import java.sql.*;
import GUI.Frame;
public class code
{
public static char[] matrix = new char[20];
public static int date = 0;
public static char show[] = new char[10];
public static boolean wrong = false;
public static String[][] rowData = new String[50][3];
public static String[] columnNames = {" ", " ", " "};
public static code main;
public static void main ( String[] args )
{
final GUI.Frame Frame = new GUI.Frame();
// src.Frame.run.inst.update();
String driver = "org.hsqldb.jdbcDriver";
String URL = "jdbc:hsqldb:hsql://localhost/xdb";
String tbstring = new String("CUSTOMER");
String heads[] = new String[50];
heads[0] = "ID";
heads[1] = "FIRSTNAME";
heads[2] = "LASTNAME";
TableModel jTable1Model = new DefaultTableModel( rowData, columnNames);
GUI.Frame.table.setModel(jTable1Model);
loaddriver(driver);
Connection con = connect(URL); //liefert Connection aus URL
Spaltenbeschriftung(con,GUI.Frame.table, tbstring, heads); //Liefert Spaltenbeschriftung für Tabel der Tabelle tbstring der Spalten heads
showdb(tbstring, GUI.Frame.table, con); //zeige tabelle tbstring in table
disconnect(con); //beendet Verbindung con
Frame.runme(main);
//UPDATE customer Set FIRSTNAME = 'Laura2' WHERE FIRSTNAME = 'Laura'
while(true){}
}
public char[] getShow()
{
return show;
}
public void setWrong(boolean b)
{
wrong = b;
}
public boolean getWrong()
{
return wrong;
}
public void setMatrix(char[] chars)
{
matrix = chars;
}
public char[] getMatrix()
{
return matrix;
}
public static void showdb(String tbstring, JTable table, Connection con) //liefert gesamten inhalt der Tabelle tbstring für tabelle table
{
try
{
String query = "SELECT * FROM ";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery( query.concat(tbstring));
int count = 0;
while ( rs.next() )
{
Frame.table.setValueAt(rs.getString(1),count,0);
table.setValueAt(rs.getString(2),count,1);
table.setValueAt(rs.getString(3),count,2);
count++;
}
}
catch(SQLException e)
{ e.printStackTrace(); }
finally
{
if ( con != null )
try { con.close(); }
catch ( SQLException e ) { e.printStackTrace(); }
}
}
public static void Spaltenbeschriftung(Connection con, JTable table, String tbstring, String heads[]) //Liefert Spaltenbeschriftung für Tabel der Tabelle tbstring der Spalten heads
{
try
{
DatabaseMetaData dbMetaData = con.getMetaData();
ResultSet columnsRS = dbMetaData.getColumns(con.getCatalog(), "%", tbstring, "%");
int i = 0;
while(columnsRS.next())
{
if(i!=(table.getColumnCount()+1))
{
if(columnsRS.getString("COLUMN_NAME").equals(heads[i]))
Frame.table.getColumnModel().getColumn(i++).setHeaderValue(columnsRS.getString("COLUMN_NAME"));
}
}
columnsRS.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
}
public static Connection connect(String URL) //verbindet mit Datenbank
{
Connection con = null;
try
{
con = DriverManager.getConnection(URL,"sa","");
}
catch(SQLException e)
{
e.printStackTrace();
}
return con;
}
public static void disconnect(Connection con) //beendet Verbindung
{
{
if(con != null)
try{con.close();} catch( SQLException e) {e.printStackTrace();}
}
}
public static void loaddriver(String driver) // Läd Treiber
{
try{ Class.forName( driver );}
catch ( ClassNotFoundException e ){ // Blöd: Treiber konnte nicht geladen werden.
System.err.println( "Keine Treiber-Klasse!");
}
}
public static void wandle(int day, int year) //wandelt aus day year in day month year DD MM YYYY
{
int month = 1;
boolean schaltjahr = false;
boolean hit = false;
int store = year;
//filtere Datum aus Datamatrix Code -> Schreibe in date / year
//Schaltjahres filter (-4 => 0 Schaltjahr)
while(year>0)
year = year - 4;
if(year==0)
schaltjahr = true;
year = store; //year zurücksetzen
if(day > 31) //januar
{
day = day - 31;
month++;
}
else
hit = true;
if(day > 28 && schaltjahr == false && hit == false)
{
day = day - 28;
month++;
}
else
{
if(schaltjahr == false)
hit = true;
if(day > 29 && schaltjahr == true && hit == false)
{
day = day - 29;
month++; //februar
}
else
{
hit = true;
}
}
if(day > 31 && hit == false) //märz
{
day = day - 31;
month++;
}
else
hit = true;
if(day > 30 && hit == false) //april
{
day = day - 30;
month++;
}
else
hit = true;
if(day > 31 && hit == false) // mai
{
day = day - 31;
month++;
}
else
hit = true;
if(day > 30 && hit == false) // juni
{
day = day - 30;
month++;
}
else
hit = true;
if(day > 31 && hit == false) // juli
{
day = day - 31;
month++;
}
else
hit = true;
if(day > 31 && hit == false)
{
day = day - 31; //august
month++;
}
else
hit = true;
if(day > 30 && hit == false)
{
day = day - 30; //september
month++;
}
else
hit = true;
if(day > 31 && hit == false)
{
day = day - 31; //oktober
month++;
}
else
hit = true;
if(day > 30 && hit == false)
{
day = day - 30; //november
month++;
}
else
hit = true;
if(day <= 31 && hit == false)
hit = true;
if(day > 31 || month > 12 || year > 9999 || hit == false)
{
wrongformat();
}
else
format(day, month, year);
};
public static void format(int day, int month, int year ) // Wandelt aus day month year in DD.MM.YYYY in show[]
{
int store;
store = day;
day = (day % 10);
store = (store - day)/10 ;
store = store +48;
day = day + 48;
show[0]= (char)store;
show[1]= (char)day;
show[2]= '.';
store = month;
month = (month % 10);
store = (store - month)/10 ;
store = store +48;
month = month + 48;
show[3]= (char)store;
show[4]= (char)month;
show[5]= '.';
store = year;
year = (year % 10);
store = (store - year)/10 ;
store = store +48;
year = year + 48;
show[6] = '2';
show[7] = '0';
show[8] = (char)store;
show[9] = (char)year;
}
public static void wrongformat() //ausgabe bei falscheingabe!
{
Frame.result.setText("WRONG FORMAT");
wrong = true;
}
}
Java:
package GUI;
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import handling.Handler;
/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class Frame extends javax.swing.JFrame {
private static final long serialVersionUID = 1L;
public JTabbedPane sides;
public JPanel tools;
public static JButton dateconvknopf;
public static JTextField result;
private JScrollBar scroll;
public JPanel DB;
public static JButton dmconvknopf;
public static JTextField dmconvtf;
private JLabel dmconvlabel;
public static JTextField dateconvtf;
private JLabel dateconvlabel;
public JPanel jPanel3;
public JPanel panel1;
public static JTable table = new JTable();
public static Object main;
/**
* Auto-generated main method to display this JFrame
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Frame inst = new Frame();
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}
});
}
public Frame(){
super();
initGUI();
}
public void runme(Object me)
{
main = me;
Frame inst = new Frame();
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}
public void initGUI() {
try {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
{
sides = new JTabbedPane();
getContentPane().add(sides, BorderLayout.CENTER);
{
tools = new JPanel();
GridLayout toolsLayout = new GridLayout(1, 1);
toolsLayout.setHgap(5);
toolsLayout.setVgap(5);
toolsLayout.setColumns(1);
tools.setLayout(toolsLayout);
sides.addTab("Tools", null, tools, null);
{
panel1 = new JPanel();
GridBagLayout panel1Layout = new GridBagLayout();
panel1Layout.rowWeights = new double[] {0.0, 0.0, 0.0, 0.1};
panel1Layout.rowHeights = new int[] {40, 40, 40, 7};
panel1Layout.columnWeights = new double[] {0.0, 0.0, 0.1};
panel1Layout.columnWidths = new int[] {185, 240, 7};
panel1.setLayout(panel1Layout);
/*GridLayout panel1Layout = new GridLayout(3, 1);
panel1Layout.setHgap(5);
panel1Layout.setVgap(5);
panel1Layout.setColumns(1);
panel1.setLayout(panel1Layout);*/
tools.add(panel1);
{
dateconvlabel = new JLabel();
panel1.add(dateconvlabel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
dateconvlabel.setText("DDDYY");
}
{
result = new JTextField("",21);
panel1.add(result, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
}
{
dmconvknopf = new JButton();
panel1.add(dmconvknopf, new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
dmconvknopf.setText("CONVERT DMX ");
dmconvknopf.addActionListener(new Handler(1, main));
}
{
dateconvknopf = new JButton();
panel1.add(dateconvknopf, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
dateconvknopf.setText("CONVERT DATE");
dateconvknopf.addActionListener(new Handler(2, main));
}
{
dateconvtf = new JTextField(" ", 21);
panel1.add(dateconvtf, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
}
{
dmconvtf = new JTextField("",21);
panel1.add(dmconvtf, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
}
{
dmconvlabel = new JLabel();
panel1.add(dmconvlabel, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
dmconvlabel.setText("CONVERT DMX (Injektor)");
}
}
}
{
DB = new JPanel();
sides.addTab("DB", null, DB, null);
{
scroll = new JScrollBar();
table.add(scroll);
DB.add(table);
}
}
}
pack();
this.setSize(800, 400);
} catch (Exception e) {
//add your error handling code here
e.printStackTrace();
}
}
}
Java:
package handling;
import java.awt.event.*;
public class Handler implements ActionListener
{
//private static final long serialVersionUID = 1L;
public int id;
public static Handler main;
public Handler(int i, Object me)
{
id = i;
main = me;
}
public void actionPerformed(ActionEvent ev)
{
switch(id)
{
case 1:
{
main.setWrong(false);
main.setMatrix((GUI.Frame.dmconvtf.getText()).toCharArray());
main.wandle(1,2);//(matrix[6]-48)*100+(matrix[7]-48)*10+(matrix[8]-48), (matrix[10]-48)*10+(matrix[11]-48));
if(wrong!=true)
{
String str = new String(show);
GUI.Frame.result.setText(str);
}
}
case 2:
{
wrong = false;
matrix = (GUI.Frame.dateconvtf.getText()).toCharArray();
wandle((matrix[0]-48)*100+(matrix[1]-48)*10+(matrix[2]-48), (matrix[3]-48)*10+(matrix[4]-48));
if(wrong!=true)
{
String str = new String(show);
GUI.Frame.result.setText(str);
}
}
}
}
}