Zkus tohle - je to naklikane behem 5ti minut, takze si s tim muzes vyhrat lepe. Hodnoty se zadavaji do JTable, po stisku tlacitka se obsah matice vypise na konzoli. Rozmery matice jsou v konstantach na zacatku. Pokud je tam chybny vstup, doplni se nula.
Kód:
/*
* Qwert.java
*
* Created on 9. květen 2004, 10:24
*/
/**
*
* @author user
*/
public class Qwert extends javax.swing.JDialog {
private static final int COLS = 5;
private static final int ROWS = 10;
/** Creates new form Qwert */
public Qwert(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
initComponents2();
}
private void initComponents2() {
setupTable(ROWS,COLS);
}
protected int[][] createArray() {
try {
int rows = jTable1.getRowCount();
int cols = jTable1.getColumnCount();
int[][] ret = new int[rows][cols];
for(int i=0; i<rows; i++)
for(int j=0; j<cols; j++) {
int value;
Object oValue = jTable1.getValueAt(i,j);
try {
value = (oValue!=null) ? Integer.parseInt(jTable1.getValueAt(i,j).toString()) : 0;
} catch (NumberFormatException nfe) {
value = 0;
}
ret[i][j] = value;
}
return ret;
} catch (Exception e ) {
e.printStackTrace();
return null;
}
}
protected void test() {
int[][] matice = createArray();
for(int i=0; i<matice.length; i++) {
int[] row = matice[i];
for(int j=0; j<row.length; j++) {
System.out.print(row[j]+"|\t");
}
System.out.println("");
}
}
protected void setupTable(int _rows, int _cols) {
Object[][] aoData = new Object[_rows][_cols];
String[] aCols = new String[_cols];
for(int i=0; i<_cols; i++) aCols[i] = Integer.toString(i);
jTable1.setModel(new javax.swing.table.DefaultTableModel(
aoData,
aCols
));
}
/** 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.
*/
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jButton1 = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
closeDialog(evt);
}
});
jPanel1.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));
jButton1.setText("nacti");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jPanel1.add(jButton1);
getContentPane().add(jPanel1, java.awt.BorderLayout.NORTH);
jScrollPane1.setViewportView(jTable1);
getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);
java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
setBounds((screenSize.width-200)/2, (screenSize.height-250)/2, 200, 250);
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// Add your handling code here:
test();
}
/** Closes the dialog */
private void closeDialog(java.awt.event.WindowEvent evt) {
//setVisible(false);
System.exit(0);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
new Qwert(new javax.swing.JFrame(), true).show();
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
// End of variables declaration
}