JTable

6 respostas
L

como faço para inserir linhas e colunas na tabela em tempo de execução

6 Respostas

R

use o método addColumn(TableColumn aColumn)

veja a API para mais detalhes

M

lula,

aqui tem o link de um tutorial de JTable mto bom!

http://www.guj.com.br/java.tutorialsobrejtabledoswing-partei.artigo.140.1.guj

flws

L

pessoal valeu pela dica

e marocos seu material foi bem util mas so que com array

tenho dois vetores um com o nome das colunas e a outra com os dados das linha…

não consegui armazena-los na tabela, ao inves de ficar com varias colunas, fica uma coluna com o nome dos dados que estão no vetor e na linha ela so pega os primeiros dados

os dados estao em um banco de dados e o resultado armazenei nos vetores

eu acho que com array posso conseguir mas queria fazer com vetor

R

Post Powered by Jeveaux

//Exibe em forma de tabela/listagem a tabela GG_STUDENT. 

package packs; 

import java.sql.*; 
import java.awt.*; 
import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
import java.util.*; 

public class TableDisplay extends JFrame { 
   private Connection connection; 
   private JTable table; 
    
   public TableDisplay() { 
      super("Listagem geral dos Pacientes"); 

      String url = "jdbc:odbc:banco.mdb"; 
      String username = "paulo"; 
      String password = "cesar"; 
       
      try { 
         Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
         connection = DriverManager.getConnection(url, username, password); 
      } 
      catch (ClassNotFoundException cnfex) { 
         System.err.println( 
            "Falhou a conexao com o banco"); 
         cnfex.printStackTrace(); 
          System.exit(1); 
      } 
      catch (SQLException sqlex) { 
          System.err.println("Conexao desabilitada");    
          sqlex.printStackTrace(); 
      } 
       
      getTable(); 
      setSize(800,370); 
      setLocation(5,20); 
      show(); 
   } 
    
   private void getTable() 
   { 
      Statement statement; 
      ResultSet resultset; 
       
      try { 
         String query = "Select * from Tab_Pacientes order by nome"; 
         statement = connection.createStatement(); 
         resultset = statement.executeQuery(query); 
         displayResultSet(resultset); 
         statement.close(); 
       } 
      catch ( SQLException sqlex ) { 
         sqlex.printStackTrace(); 
      } 
   } 
    
   private void displayResultSet(ResultSet rs ) 
      throws SQLException 
   { 
      boolean moreRecords = rs.next(); 
       
      if (! moreRecords) { 
         JOptionPane.showMessageDialog(this, "Nao existem registros na tabela!!"); 
         setTitle("Registros Vazios"); 
         return; 
      } 
       
//      setTitle("Listagem Geral dos dados - Tabela Student - bancoDB"); 
       
      Vector columnHeads = new Vector(); 
      Vector rows = new Vector(); 
       
      try { 
         ResultSetMetaData rsmd = rs.getMetaData(); 
         for (int i = 1; i <= rsmd.getColumnCount(); ++i) 
            columnHeads.addElement(rsmd.getColumnName(i)); 
             
         do { 
            rows.addElement(getNextRow(rs, rsmd)); 
         } while (rs.next()); 
          
         table = new JTable(rows, columnHeads); 
         JScrollPane scroller = new JScrollPane(table); 
         getContentPane().add(scroller,BorderLayout.CENTER); 
         validate(); 
      } 
      catch (SQLException sqlex) { 
         sqlex.printStackTrace(); 
      } 
   } 
    
   private Vector getNextRow( ResultSet rs, ResultSetMetaData rsmd)    
        throws SQLException 
   { 
      Vector currentRow = new Vector(); 
      for (int i = 1; i <= rsmd.getColumnCount(); ++i) 
         switch(rsmd.getColumnType(i)) { 
            case Types.VARCHAR: currentRow.addElement(rs.getString(i)); 
              break; 
            case Types.INTEGER:currentRow.addElement(new Long(rs.getLong(i))); 
              break; 
              /*case Types.LONGCHAR:currentRow.addElement(rs.getString(i)); 
                break;*/ 
            default: System.out.println("Tipo dos Dados: " + rsmd.getColumnTypeName(i)); 
       } 
     return currentRow; 
   } 
    
   public void shutDown() 
   { 
      try { 
         connection.close(); 
      } 
      catch (SQLException sqlex) { 
         System.err.println("N� foi poss�el desconectar."); 
         sqlex.printStackTrace(); 
      } 
   } 
    
   public static void montaTable() 
   { 
//      try {   //Faz o aplicativo ter apar�cias. 
//           UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
          //UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); 
//      } catch (Exception exc) { 
//          System.err.println("Error loading L&F: " + exc); 
//      } 
      final TableDisplay app = new TableDisplay(); 
       app.addWindowListener(new WindowAdapter() { 
          public void windowClosing(WindowEvent e) 
          { 
             //app.shutDown(); 
             //System.exit(0); 
          } 
       } 
     ); 
//   } 
    public static void main(String args[]) { 
        montaTable(); 
     }    
}
L

até essa parte legal com vetor eu consegui fazer

O problema é que tenho dois ComboBox na janela e a tabela, de acordo com o que selecionar na tabela vc terá o resultado na mesma janela sem abrir outra…
com array ficou legal funcionou só não sei se dá pra fazer com vetor, e gostaria de saber se dá pra fazer

R

testa ué…

eu acho que deve ter o mesmo efeito

Criado 19 de setembro de 2004
Ultima resposta 21 de set. de 2004
Respostas 6
Participantes 3