1 pièce(s) jointe(s)
Afficher ma classe JTable dans un JDesktopPane
Bonjour,
Alors voilà, j'ai 3 classe qui me permettent d'afficher un tableau de données d'un table de ma DB.
Voici ce que j'obtiens :
Pièce jointe 446856
Mon code pour ceci :
ma classe Main
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| public class Main {
public static void main(String[] args)
{
Connection connection = getConnection();
try
{
Statement st = connection.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY );
ResultSet rs = st.executeQuery( "SELECT avarie.id_avarie, equipement.des_eqpmt, avarie.des_avarie, avarie.prio_dir, avarie.prio_resp, avarie.prio_agent, avarie.prio_man,avarie.prio_emis,avarie.date_creation FROM `avarie` INNER JOIN `equipement` ON avarie.id_eqpmt = equipement.id_eqpmt" );
ResultSetTableModel rtm = new ResultSetTableModel( rs );
TablePanel tablePanel = new TablePanel( rtm );
JFrame mainFrame = new JFrame( "Affiche table " );
mainFrame.add( tablePanel, BorderLayout.CENTER );
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
mainFrame.setSize( 640, 480 );
mainFrame.setVisible( true );
}
catch ( SQLException e )
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Connection getConnection()
{
Connection connection = null;
boolean ok = false;
//--- chargement en mémoire du pilote
try
{
Class.forName( PILOTE );
ok = true;
}
catch ( ClassNotFoundException e )
{
System.out.println( "ERREUR chargement du pilote: pilote non trouvé" );
e.printStackTrace();
}
//--- connexion à la base de données
if ( ok )
{
try
{
connection = DriverManager.getConnection( URL_DATABASE,"root","" );
}
catch ( SQLException e )
{
System.out.println( "ERREUR de connexion à la base de données: " + URL_DATABASE );
e.printStackTrace();
}
}
return connection;
}
//--- attributs
private static final String PILOTE = "com.mysql.jdbc.Driver";
private static final String URL_DATABASE = "jdbc:mysql://localhost:3306/projetmcapp";
} |
ma classe ResultSetTableModel :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
| public class ResultSetTableModel extends AbstractTableModel {
public ResultSetTableModel( ResultSet resultSet )
{
this.resultSet = resultSet;
try
{
this.resultSetMetaData = resultSet.getMetaData();
}
catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public int getColumnCount()
{
try
{
return resultSetMetaData.getColumnCount();
}
catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
return 0;
}
}
@Override
public int getRowCount()
{
try
{
resultSet.last();
return resultSet.getRow();
}
catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
return 0;
}
}
@Override
public Object getValueAt(int rowIndex, int columnIndex)
{
try
{
resultSet.absolute( rowIndex + 1 );
return resultSet.getObject(columnIndex + 1 );
}
catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
@Override
public String getColumnName( int column )
{
try
{
return resultSetMetaData.getColumnName( column + 1 );
}
catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
return "";
}
}
private ResultSet resultSet;
private ResultSetMetaData resultSetMetaData;
} |
et ma classe TablePanel :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13
| public class TablePanel extends JPanel {
public TablePanel( TableModel model )
{
table = new JTable( model );
//les parametres de la table
setLayout( new BorderLayout() );
// Ajout de la table dans un JScrollPane, dans le JPanel + border Layout center
add( new JScrollPane( table ), BorderLayout.CENTER );
}
private JTable table;
} |
Je n'arrive pas à faire en sorte d'ajouter mon tableau dans un JDesktopPane présent dans un "JFrame Form" dans lequel je rajoute le code :
Code:
1 2 3 4 5
|
Main m = new Main();
//DeskList c'est le nom de mon JDesktopPane
DeskList.add(m);
m.setVisible(true); |
et en rajoutant un "extends JInternalFrame" pour ma class Main.
Je ne sais plus quoi faire :calim2:
Merci pour ceux qui prendrons le temps de m'aider. :help: