Hogyan adjak oszlopokat a táblázatomhoz?
Van egy programom aminek a feladata h tudjak bele egy névsort irni meg jegyeket és hiányzást ez meg is van de nem tudom növelni az oszlopok számát. Ha valakinek van valamiötlete az jól jönne
Itt a kód :
import java.util.Vector;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
/**
*
* @author Sabolch
*/
public class Uchet extends javax.swing.JFrame {
int j=1;
JComboBox combo = new JComboBox();
Vector vec = new Vector();
private Object[] value = {"H","", "1", "2", "3", "4","5"};
//int count = 2;
public Uchet() {
initComponents();
tb.getColumnModel().getColumn(0).setPreferredWidth(40);
tb.getColumnModel().getColumn(1).setPreferredWidth(600);
DefaultTableModel model = (DefaultTableModel) tb.getModel();
DefaultComboBoxModel comboModel = new DefaultComboBoxModel( value );
combo.setModel( comboModel );
for(int i = 2; i < model.getColumnCount(); i++){
//count++;
TableColumn count = tb.getColumnModel().getColumn( i );
count.setCellEditor( new DefaultCellEditor( combo ) );
}
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
tb = new javax.swing.JTable();
btnAdd = new javax.swing.JButton();
btnDel = new javax.swing.JButton();
jButton1 = new javax.swing.JButton();
jScrollPane2 = new javax.swing.JScrollPane();
tbocinki = new javax.swing.JTable();
jButton2 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
tb.setFont(new java.awt.Font("Times New Roman", 0, 12)); // NOI18N
tb.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
"№", "ПІБ", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20"
}
) {
Class[] types = new Class [] {
java.lang.Integer.class, java.lang.String.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class
};
public Class getColumnClass(int columnIndex) {
return types [columnIndex];
}
});
tb.setFocusable(false);
tb.setIntercellSpacing(new java.awt.Dimension(2, 2));
tb.setRowSelectionAllowed(false);
tb.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
tbMouseClicked(evt);
}
});
jScrollPane1.setViewportView(tb);
if (tb.getColumnModel().getColumnCount() > 0) {
tb.getColumnModel().getColumn(0).setResizable(false);
tb.getColumnModel().getColumn(1).setResizable(false);
tb.getColumnModel().getColumn(2).setResizable(false);
tb.getColumnModel().getColumn(3).setResizable(false);
tb.getColumnModel().getColumn(4).setResizable(false);
tb.getColumnModel().getColumn(5).setResizable(false);
tb.getColumnModel().getColumn(6).setResizable(false);
tb.getColumnModel().getColumn(7).setResizable(false);
tb.getColumnModel().getColumn(8).setResizable(false);
tb.getColumnModel().getColumn(9).setResizable(false);
tb.getColumnModel().getColumn(10).setResizable(false);
tb.getColumnModel().getColumn(11).setResizable(false);
tb.getColumnModel().getColumn(12).setResizable(false);
tb.getColumnModel().getColumn(13).setResizable(false);
tb.getColumnModel().getColumn(14).setResizable(false);
tb.getColumnModel().getColumn(15).setResizable(false);
tb.getColumnModel().getColumn(16).setResizable(false);
tb.getColumnModel().getColumn(17).setResizable(false);
tb.getColumnModel().getColumn(18).setResizable(false);
tb.getColumnModel().getColumn(19).setResizable(false);
tb.getColumnModel().getColumn(20).setResizable(false);
tb.getColumnModel().getColumn(21).setResizable(false);
}
btnAdd.setText("Add");
btnAdd.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnAddActionPerformed(evt);
}
});
btnDel.setText("Delete");
btnDel.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnDelActionPerformed(evt);
}
});
jButton1.setText("Atlag");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
tbocinki.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
}
));
jScrollPane2.setViewportView(tbocinki);
jButton2.setText("Oszlop hozzáad");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(btnAdd)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnDel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton2))
.addGroup(layout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 1124, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jScrollPane2)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 508, Short.MAX_VALUE))
.addGap(35, 35, 35)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnAdd)
.addComponent(btnDel)
.addComponent(jButton1)
.addComponent(jButton2))
.addContainerGap(24, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void tbMouseClicked(java.awt.event.MouseEvent evt) {
/*int r = tb.rowAtPoint(evt.getPoint());
int col = tb.columnAtPoint(evt.getPoint());
if (r >= 0 && r < tb.getRowCount()) {
tb.setRowSelectionInterval(r, r);
} else {
tb.clearSelection();
}
int rowindex = tb.getSelectedRow();
if (rowindex < 0)
return;
if (evt.getComponent() instanceof JTable ) {
if(tb.getSelectedColumn()==0||tb.getSelectedColumn()==1){
}
else{
tb.getColumnModel().getColumn(col).setHeaderValue("00.00");
}
}*/
}
private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {
DefaultTableModel model = (DefaultTableModel) tb.getModel();
if(j<51){
Object [] sorszam = {j++};
model.addRow(sorszam);}
}
private void btnDelActionPerformed(java.awt.event.ActionEvent evt) {
DefaultTableModel model = (DefaultTableModel) tb.getModel();
if(j>1){
model.removeRow(model.getRowCount()-1);
j--;}
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
DefaultTableModel modeloc = (DefaultTableModel) tbocinki.getModel();
int jegy=0;
int counter = 0;
int hianyzas =0;
double ocinka = 0;
for(int l=0;l<tb.getRowCount(); l++){
for(int j=2; j<21; j++){
Object obj = tb.getValueAt(l, j);
if(obj != "H" && obj != null && obj != ""){
jegy += Integer.valueOf((String) obj);
counter++;
}
if (obj=="H"){
hianyzas++;
}
}
if(jegy !=0){ocinka = Math.rint(jegy/counter);
if(ocinka > 2 && hianyzas < 8){
vec.add(ocinka);
jegy=0;
counter=0;
hianyzas=0;
}else {vec.add("Н/А");
jegy=0;
counter=0;
hianyzas=0;}}
else {
vec.add("Н/А");
jegy=0;
counter=0;
hianyzas=0;
}
}
for( int i = modeloc.getRowCount() - 1; i >= 0; i-- )
{
modeloc.removeRow(i);
}
modeloc.setColumnCount(0);
modeloc.addColumn("Ocinki",vec);
vec.clear();
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
DefaultTableModel model = (DefaultTableModel) tb.getModel();
model.addColumn("kkkk");
}
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 [link]
*/
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(Uchet.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Uchet.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Uchet.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Uchet.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 Uchet().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btnAdd;
private javax.swing.JButton btnDel;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTable tb;
private javax.swing.JTable tbocinki;
// End of variables declaration
}
Te lehetsz az első, aki segít a kérdezőnek!
Kapcsolódó kérdések:
Minden jog fenntartva © 2024, www.gyakorikerdesek.hu
GYIK | Szabályzat | Jogi nyilatkozat | Adatvédelem | Cookie beállítások | WebMinute Kft. | Facebook | Kapcsolat: info(kukac)gyakorikerdesek.hu
Ha kifogással szeretne élni valamely tartalommal kapcsolatban, kérjük jelezze e-mailes elérhetőségünkön!