[dúvida] tabela

Salve a todos!

Estou com uma dúvida aqui em uma tabela que estou criando
para utilizar com banco de dados.

Não consigo fazer com que a coluna para se colocar o nome
de uma vaca (é um projeto agrícola, mas para fazendeiros, é claro) , fique com valor de uma string.

Segue o código:

[code]//http://www.crionics.com/products/opensource/faq/swing_ex/SwingExamples.html

/* (swing1.1) */

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Enumeration;
import java.util.Hashtable;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableModel;

public class TableExample extends JPanel {

public TableExample() {

setLayout(new BorderLayout());

DefaultTableModel dm = new DefaultTableModel() {
  public Class getColumnClass(int col) {
      
 switch (col) {  
    case 0:
      return Integer.class;
      
    case 1:
      return String.class;
      
    case 2:
      return Object.class;
      
    case 3:
      return Integer.class;
      
    case 4:
      return Integer.class;
      
    case 5:
      return Integer.class;
      
    case 6:
      return Integer.class;
      
    case 7:
      return Integer.class;
      
    default:
      return Object.class; 
      
    }
  }

public boolean isCellEditable(int row, int col) {
switch (col) {
case 2:
return false;

    default:
      return true;
    }
  }

public void setValueAt(Object obj, int row, int col) {
if (col != 1) {
super.setValueAt(obj, row, col);
return;
}
try {
Integer integer = new Integer(obj.toString());
// super.setValueAt(checkMinMax(integer), row, col);
}
catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(null, “Valor inválido!”,
“”, JOptionPane.ERROR_MESSAGE);
//ex.printStackTrace();
}

  catch (NullPointerException e) {
      JOptionPane.showMessageDialog(null, "Por favor, preencha este campo!",
      "", JOptionPane.INFORMATION_MESSAGE);
        //e.printStackTrace();
    }
    
    
  }
};
dm.setDataVector(new Object[][] {
    { "0", "0", "0", "0", "0", "0", "0", "0"},
    { "0", "0", "0", "0", "0", "0", "0", "0"},
    { "0", "0", "0", "0", "0", "0", "0", "0"},
    { "0", "0", "0", "0", "0", "0", "0", "0"},
    { "0", "0", "0", "0", "0", "0", "0", "0"},
    { "0", "0", "0", "0", "0", "0", "0", "0"},
    { "0", "0", "0", "0", "0", "0", "0", "0"},
    { "0", "0", "0", "0", "0", "0", "0", "0"},
    { "0", "0", "0", "0", "0", "0", "0", "0"},
    { "0", "0", "0", "0", "0", "0", "0", "0"},
    { "0", "0", "0", "0", "0", "0", "0", "0"},
    { "0", "0", "0", "0", "0", "0", "0", "0"},
    { "0", "0", "0", "0", "0", "0", "0", "0"},
    { "0", "0", "0", "0", "0", "0", "0", "0"},
    { "0", "0", "0", "0", "0", "0", "0", "0"},
    { "0", "0", "0", "0", "0", "0", "0", "0"},
    { "0", "0", "0", "0", "0", "0", "0", "0"},
    { "0", "0", "0", "0", "0", "0", "0", "0"},
    { "0", "0", "0", "0", "0", "0", "0", "0"} }, 

/*ou então assim:

  • { “0”, new Integer(0), new Integer(0), new Integer(0), new Integer(0) },
    */

    new Object[] { “Nº”, “Nome”, “Idade”, “Data montagem”, “Data parto”,
    “Data de secagem”, “Sexo da cria”, “Data vacina”});

JTable table = new JTable(dm);

table.getModel().addTableModelListener(new TableModelListener() {
  public void tableChanged(TableModelEvent e) {
    if (e.getType() == TableModelEvent.UPDATE) {
      int col = e.getColumn();
      if (col == 1) {
        int row = e.getFirstRow();
        TableModel model = (TableModel) e.getSource();
        Integer value = (Integer) model.getValueAt(row, col);
      }
      
    }
  }
});

JScrollPane pane = new JScrollPane(table);
add(pane, BorderLayout.CENTER);

}

public static void main(String[] args) {
JFrame f = new JFrame(“Projeto Agrícola”);
f.getContentPane()
.add(new TableExample(), BorderLayout.CENTER);
f.setSize(900, 380);
f.setLocation(60,50);
f.setVisible(true);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}

}

class IndicatorCellRenderer extends JProgressBar implements TableCellRenderer {
private Hashtable limitColors;

private int[] limitValues;

public IndicatorCellRenderer() {
super(JProgressBar.HORIZONTAL);
setBorderPainted(false);
}

public IndicatorCellRenderer(int min, int max) {
super(JProgressBar.HORIZONTAL, min, max);
setBorderPainted(false);
}

public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
int n = 0;
if (!(value instanceof Number)) {
String str;
if (value instanceof String) {
str = (String) value;
} else {
str = value.toString();
}
try {
n = Integer.valueOf(str).intValue();
} catch (NumberFormatException ex) {
}
} else {
n = ((Number) value).intValue();
}
Color color = getColor(n);
if (color != null) {
setForeground(color);
}
setValue(n);
return this;
}

public void setLimits(Hashtable limitColors) {
this.limitColors = limitColors;
int i = 0;
int n = limitColors.size();
limitValues = new int[n];
Enumeration e = limitColors.keys();
while (e.hasMoreElements()) {
limitValues[i++] = ((Integer) e.nextElement()).intValue();
}
sort(limitValues);
}

private Color getColor(int value) {
Color color = null;
if (limitValues != null) {
int i;
for (i = 0; i < limitValues.length; i++) {
if (limitValues[i] < value) {
color = (Color) limitColors
.get(new Integer(limitValues[i]));
}
}
}
return color;
}

private void sort(int[] a) {
int n = a.length;
for (int i = 0; i < n - 1; i++) {
int k = i;
for (int j = i + 1; j < n; j++) {
if (a[j] < a[k]) {
k = j;
}
}
int tmp = a[i];
a[i] = a[k];
a[k] = tmp;
}
}
}[/code]

O problema é que alguns campos não querem receber String, mesmo eu definindo
até mesmo todos para receber.

Se precisarem de qualquer coisa, estou por aqui uma vez por semana :wink:

Obrigado a todos pela atenção.

bye!

Dae dark123.

Aqui tem alguns exemplos que poderão te ajudar.

Se continuares sem conseguir posta ai.

http://java.sun.com/docs/books/tutorial/uiswing/components/table.html

Flw.

Acho que vai resolver sim.

Obrigado.

Olha eu de novo.

Como eu faço para colocar abas nesse código?

Tentei de diversas formas e não consegui.
É que eu quero fazer duas tabelas, separadas por abas, e todas com acesso em banco de dados diferentes.

Será um grande projeto pra mim.

Obrigado pela ajuda.

Oi,

Desculpa, mãs não tenho nenhum ferramenta aqui para postar um código exemplo…
mas de uma olhadinha no JTabbedPane…

Tchauzin!

Ok, já resolvi também.

Obrigado.