package Korrelation;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
public class Fenster2 extends JFrame implements ActionListener
{
/**
*
*/
private static final long serialVersionUID = (long) 1.0;
//---------------------------------------------------------------------------------------
private int inputValue = 0;
private int rowCount = 0;
private String description[] = new String[]{"x", "y", "xi-xq","yi-yq","(xi-xq)²", "(yi-yq)²", "xi-xq * yi-yq"};
private Object values[][] ={};
private double xq= 0;
private double yq = 0;
//---------------------------------------------------------------------------------------
private JFrame frame;
//---------------------------------------------------------------------------------------
private JPanel panelNorth;
private JPanel panelSouth;
private JPanel panelCenter;
private JPanel panelNorthNorth;
private JPanel panelNorthCenter;
//---------------------------------------------------------------------------------------
private JTextField txtRow;
//---------------------------------------------------------------------------------------
private JLabel lblAnzRow;
//---------------------------------------------------------------------------------------
private JButton btnInit;
private JButton btnCheck;
private JButton btnCalculate;
private JButton btnNew;
//---------------------------------------------------------------------------------------
private JScrollPane scrollPane;
private JTable table;
private DefaultTableModel model;
//---------------------------------------------------------------------------------------
private JMenuBar menuBar;
private JMenu menuOptions;
private JMenu menuHelp;
private JMenuItem menuItemExit;
private JMenuItem menuItemAbout;
public Fenster2()
{
initComponents();
addComponents();
initFrame();
}
public void initFrame()
{
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setSize(532, 300);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
}
public void drawWindow()
{
frame.setVisible(true);
}
public void initComponents()
{
frame = new JFrame();
//---------------------------------------------------------------------------------------
panelNorth = new JPanel();//border
panelSouth = new JPanel();//border
panelCenter = new JPanel();
panelNorthNorth = new JPanel();//border
panelNorthCenter = new JPanel();//flow
//---------------------------------------------------------------------------------------
txtRow = new JTextField(5);
//---------------------------------------------------------------------------------------
lblAnzRow = new JLabel("Anzahl der Zeilen");
//---------------------------------------------------------------------------------------
model = new DefaultTableModel(values, description);
table = new JTable(model);
initTable();
//---------------------------------------------------------------------------------------
scrollPane = new JScrollPane(table);
//---------------------------------------------------------------------------------------
menuBar = new JMenuBar();
menuOptions = new JMenu("Options");
menuHelp = new JMenu("Help");
//---------------------------------------------------------------------------------------
menuItemAbout = new JMenuItem("About");
menuItemAbout.setActionCommand("About");
menuItemAbout.addActionListener(this);
menuItemExit = new JMenuItem("Exit");
menuItemExit.setActionCommand("Exit");
menuItemExit.addActionListener(this);
//---------------------------------------------------------------------------------------
btnInit = new JButton("Initalisieren");
btnInit.setActionCommand("btnInit");
btnInit.addActionListener( this);
btnCheck = new JButton("Check");
btnCheck.setActionCommand("btnCheck");
btnCheck.addActionListener(this);
btnCheck.setEnabled(false);
btnCalculate = new JButton("Calculate");
btnCalculate.setActionCommand("btnCalculate");
btnCalculate.addActionListener(this);
btnCalculate.setEnabled(false);
btnNew =new JButton("New");
btnNew.setActionCommand("btnNew");
btnNew.addActionListener(this);
//---------------------------------------------------------------------------------------
//definition of layout
frame.getContentPane().setLayout(new BorderLayout());
panelNorth.setLayout(new BorderLayout());
panelSouth.setLayout(new FlowLayout());
panelCenter.setLayout(new BorderLayout());
panelNorthNorth.setLayout(new BorderLayout());
panelNorthCenter.setLayout(new FlowLayout(FlowLayout.LEFT,5,5));
}
public void addComponents()
{
frame.getContentPane().add(panelNorth, BorderLayout.NORTH);
frame.getContentPane().add(panelSouth,BorderLayout.SOUTH);
//---------------------------------------------------------------------------------------
panelNorth.add(panelNorthNorth, BorderLayout.NORTH);
panelNorth.add(panelNorthCenter,BorderLayout.CENTER);
//---------------------------------------------------------------------------------------
panelNorthNorth.add(menuBar, BorderLayout.NORTH);
menuBar.add(menuOptions);
menuBar.add(menuHelp);
//---------------------------------------------------------------------------------------
menuHelp.add(menuItemAbout);
menuOptions.add(menuItemExit);
//---------------------------------------------------------------------------------------
panelNorthCenter.add(lblAnzRow);
panelNorthCenter.add(txtRow);
panelNorthCenter.add(btnInit);
panelNorthCenter.add(btnCheck);
panelNorthCenter.add(btnCalculate);
panelNorthCenter.add(btnNew);
//---------------------------------------------------------------------------------------
panelCenter.add(scrollPane);
frame.getContentPane().add(panelCenter,BorderLayout.CENTER);
}
@Override
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals(menuItemAbout.getActionCommand()))
{
JOptionPane.showMessageDialog(frame, "Korrelator v 1.0 \n made by Hans Vocke","About", JOptionPane.INFORMATION_MESSAGE );
}
else if(e.getActionCommand().equals(menuItemExit.getActionCommand()))
{
System.exit(0);
}
else if(e.getActionCommand().equals(btnInit.getActionCommand()))
{
inputValue = Einlesen.einlInt();
rowCount = table.getRowCount();
DefaultTableModel model2 = ((DefaultTableModel)table.getModel());
if ((rowCount < inputValue) && inputValue >0)
{
for(int count = inputValue ;count >rowCount ;count--)
{
model2.addRow(new Object[]{});
btnCheck.setEnabled(true);
}
}
else if ((rowCount > inputValue) && inputValue >0)
{
for (int count = rowCount; count > inputValue; count-- )
{
model2.removeRow(1);
}
}
}
else if(e.getActionCommand().equals(btnCheck.getActionCommand()))
{
System.out.println("check");
}
else if(e.getActionCommand().equals(btnCalculate.getActionCommand()))
{
}
else if(e.getActionCommand().equals(btnNew.getActionCommand()))
{
System.out.println("new");
}
}
public static String getJTextFieldText()
{
Fenster2 instanz = new Fenster2();
String txt = instanz.txtRow.getText();
return txt;
}
public void initTable()
{
table.setAutoResizeMode(table.AUTO_RESIZE_OFF);//Feste zellengröße bei mehreren spalten (scrollt seitlich)
table.setRowSelectionAllowed(false); //blaumarkierung über die reihe ausschalten
table.getTableHeader().setReorderingAllowed(false);// spalten unverschiebbar
table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);//zell editierung beenden
}
}