IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Documents Java Discussion :

JTabbedPane JTable JPanel Excel (xls, xlsx) Apache POI


Sujet :

Documents Java

  1. #21
    Modérateur
    Avatar de joel.drigo
    Homme Profil pro
    Ingénieur R&D - Développeur Java
    Inscrit en
    Septembre 2009
    Messages
    12 430
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 54
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Ingénieur R&D - Développeur Java
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2009
    Messages : 12 430
    Points : 29 131
    Points
    29 131
    Billets dans le blog
    2
    Par défaut
    Citation Envoyé par nadong94 Voir le message
    Et pour finir le message d'erreur :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at EVENT.UtilityMethods.createJTabbed(UtilityMethods.java:62)
    Il y a quoi à la ligne 62 dans ton code ? (Parce que dans la version que tu postes, c'est return tabbedPane;, donc impossible d'avoir une NPE là). Ensuite une NPE, c'est relativement facile à analyser : il faut regarder la ligne concernée, et regarder ce qui est null dedans, et ensuite chercher pourquoi ce n'est pas initialisé avant...
    L'expression "ça marche pas" ne veut rien dire. Indiquez l'erreur, et/ou les comportements attendus et obtenus, et donnez un Exemple Complet Minimal qui permet de reproduire le problème.
    La plupart des réponses à vos questions sont déjà dans les FAQs ou les Tutoriels, ou peut-être dans une autre discussion : utilisez la recherche interne.
    Des questions sur Java : consultez le Forum Java. Des questions sur l'EDI Eclipse ou la plateforme Eclipse RCP : consultez le Forum Eclipse.
    Une question correctement posée et rédigée et vous aurez plus de chances de réponses adaptées et rapides.
    N'oubliez pas de mettre vos extraits de code entre balises CODE (Voir Mode d'emploi de l'éditeur de messages).
    Nouveau sur le forum ? Consultez Les Règles du Club.

  2. #22
    Membre régulier
    Homme Profil pro
    Étudiant
    Inscrit en
    Janvier 2016
    Messages
    213
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Janvier 2016
    Messages : 213
    Points : 96
    Points
    96
    Par défaut
    Salut,
    Pour les variables elles doivent êtres static a cause du fait que les méthodes soit elle même static et je les utilises plusieurs fois donc je ne peux pas les initialiser directement.
    Ma question est surtout pourquoi quand je fais :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
     
    List<JPanel> lstPanel = createTabPanel(file);
    for (JPanel panel : lstPanel) {
    	tabbedPane.addTab(panel.getName(), // tab name
    			null, // tab icon
    			panel, // tabs panel for the JTable
    			panel.getToolTipText() // tab tool tip text
    					);
    	IhmConverter.getHDMT().add(tabbedPane);
    }
    Tous mes panels sensés être créé sont nulles ?

    Normalement :
    1→ Créer les JTables par contenu de chaque feuille Excel.
    2→ Créer un JPanel par JTable
    3→ Créer un JTabbedPane par JPanel (pour ainsi dire par feuille Excel, afin de récupérer les JTables et leurs contenu)
    4→ Ajouter les JTabbedPane a IhmConverter

  3. #23
    Membre régulier
    Homme Profil pro
    Étudiant
    Inscrit en
    Janvier 2016
    Messages
    213
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Janvier 2016
    Messages : 213
    Points : 96
    Points
    96
    Par défaut
    Salut, j'ai enlever tout les statics pour être tranquille mais le résultat et le même :
    UtilityMethods :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
     
    package EVENT;
     
    import java.awt.Color;
    import java.awt.Font;
    import java.io.File;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
     
    import javax.swing.BorderFactory;
    import javax.swing.JButton;
    import javax.swing.JDialog;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTabbedPane;
    import javax.swing.JTable;
    import javax.swing.table.DefaultTableModel;
     
    import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
    import org.apache.poi.xssf.usermodel.XSSFCell;
    import org.apache.poi.xssf.usermodel.XSSFRow;
    import org.apache.poi.xssf.usermodel.XSSFSheet;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
     
    import IHM.IhmConverter;
     
    /**
     * Some utility methods for the application
     * 
     * @author t0163126
     */
    public class UtilityMethods 
    {
    	private XSSFWorkbook wb;
    	private XSSFSheet sheet;
    	private XSSFCell cell;
    	private XSSFRow row;
    	private JButton btnYes;
    	private JButton btnNo;
     
    	// Constructor
    	public UtilityMethods() {
    	}
     
    	//Getter for the yes and no button
    	public JButton getBtnYes() {
    		return this.btnYes;
    	}
    	public JButton getBtnNo() {
    		return this.btnNo;
    	}
     
    	/**
             * Create tabs based on file and panel
             * 
             * @param tabPanel
             * @param file
             * @return
             */
    	public JTabbedPane createJTabbed(File file) {
    		JTabbedPane tabbedPane = new JTabbedPane();
     
    		List<JPanel> lstPanel = createTabPanel(file);
    		for (JPanel panel : lstPanel) {
    			tabbedPane.addTab(panel.getName(), // tab name
    					null, // tab icon
    					panel, // tabs panel for the JTable
    					panel.getToolTipText() // tab tool tip text
    					);
    			IhmConverter.getHDMT().add(tabbedPane);
    		}
    		return tabbedPane;
    	}
     
    	/**
             * Create a tab panel by excel feel
             * 
             * @param fileName
             * @param excelFile
             * @return
             */
    	public List<JPanel> createTabPanel(File file) {
    		JTable spreadsheet = new JTable();
    		JPanel panel;
    		List<JPanel> lstPanel = new ArrayList<JPanel>();
     
    		try {
    			wb = new XSSFWorkbook(file);
    			// Get the sheet number
    			int nbFeuilles = wb.getNumberOfSheets();
    			for (int i = 0; i < nbFeuilles; i++) {
    				sheet = wb.getSheetAt(i);
    				// Create one JTable by excel feel
    				spreadsheet = createJTableWithExcel(sheet);
     
    				spreadsheet.setFont(new Font(Font.SERIF, Font.PLAIN, 10));
    				JScrollPane elevator = new JScrollPane();
    				elevator.setViewportView(spreadsheet);
    				// Create a panel by excel feel number
    				panel = new JPanel();
    				panel.setBorder(BorderFactory.createTitledBorder(sheet
    						.getSheetName()));
    				panel.setBackground(Color.LIGHT_GRAY);
    				panel.add(elevator);
    				lstPanel.add(panel);
    			}
    			wb.close();
    		} catch (InvalidFormatException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    		return lstPanel;
    	}
     
    	/**
             * Create a JTable depending on the file and the entry sheet number
             * 
             * @param file
             * @param numSheet
             * @return
             */
    	public JTable createJTableWithExcel(XSSFSheet sheet) {
    		// Create new table
    		JTable table = new JTable();
    		// Reading the excel file
    		// Count the line number
    		int nbRows = sheet.getLastRowNum();
    		// Count the column number
    		int nbColumns = numberMaxColumn(sheet);
     
    		System.out.println("In the sheet " + sheet.getSheetName()
    				+ " there are " + nbRows + " lines and " + nbColumns
    				+ " columns");
     
    		// Create new object array
    		Object[][] obj = new Object[nbRows][nbColumns];
     
    		// Array header
    		String[] title = new String[nbColumns];
    		for (int i = 0; i < nbColumns; i++) {
    			title[i] = "#" + i;
    		}
     
    		// Browsing the sheet and retrieves the lines one by one
    		for (int rowContent = 0; rowContent < nbRows; rowContent++) {
    			row = sheet.getRow(rowContent);
     
    			// Browsing the line and retrieves the columns
    			if (row != null) {
    				for (int columnContent = 0; columnContent < nbColumns; columnContent++) {
    					// Retrieves the cell and this value
    					cell = row.getCell((short) columnContent);
    					Object value = contentCell(cell);
     
    					obj[rowContent][columnContent] = value;
    				}
    			}
    		}
    		table.setModel(new DefaultTableModel(obj, title));
     
    		return table;
    	}
     
    	/**
             * The cell can contain different type of value that should be specifically
             * recovered
             * 
             * @param cell
             * @return
             */
    	private Object contentCell(XSSFCell c) {
    		Object value = null;
     
    		if (c == null) {
    			value = "";
    		} else if (c.getCellType() == XSSFCell.CELL_TYPE_BOOLEAN) {
    			value = c.getBooleanCellValue();
    		} else if (c.getCellType() == XSSFCell.CELL_TYPE_ERROR) {
    			value = c.getErrorCellValue();
    		} else if (c.getCellType() == XSSFCell.CELL_TYPE_FORMULA) {
    			value = c.getCellFormula();
    		} else if (c.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) {
    			value = c.getNumericCellValue();
    		} else if (c.getCellType() == XSSFCell.CELL_TYPE_STRING) {
    			value = c.getStringCellValue();
    		}
    		return value;
    	}
     
    	/**
             * Retrieves the maximum number of column. If a line has 2 empty boxes at
             * the end , they are not taken into account. So to retrieve the number of
             * columns to initialize our table we must browse all lines
             * 
             * @param sheet
             * @return
             */
    	private int numberMaxColumn(XSSFSheet st) {
    		int r = st.getLastRowNum();
    		int max = 0;
    		int s = 0;
    		while (s < r) {
    			if (st.getRow(s) != null) {
    				int c = st.getRow(s).getLastCellNum();
    				if (c > max) {
    					max = c;
    				}
    			}
    			s++;
    		}
    		return max + 1;
    	}
     
    	/**
             * Closing the application
             * 
             * @param ihm
             */
    	public void closeApplication(final IhmConverter ihm) {
    		btnYes = new JButton();
    		btnYes.addActionListener(new ActionEvents(this));
    		btnYes.setText("Yes, please");
    		btnYes.setToolTipText("Click Yes to quit the application");
    		btnYes.setMnemonic('Y');
     
    		btnNo = new JButton();
    		btnNo.addActionListener(new ActionEvents(this));
    		btnNo.setText("Oops no!");
    		btnNo.setToolTipText("Click No to return in the application");
    		btnNo.setMnemonic('N');
     
    		final Object[] options = { btnYes, btnNo };
     
    		JOptionPane optionPane = new JOptionPane(Final.getMsgExit(), // the message
    				JOptionPane.QUESTION_MESSAGE, // the type of message
    				JOptionPane.YES_NO_OPTION, // the type of dialog box
    				null, // A icon
    				options, // Array String for buttons text
    				options[0] // Where 0 is the index of preset button - one that
    							// will be chosen if we press Enter
    		);
     
    		JDialog dialog = optionPane.createDialog(Final.getMsgExitTitle());
    		dialog.setAlwaysOnTop(true);
    		dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
    		dialog.toFront();
    		dialog.setFocusable(true);
    		dialog.setVisible(true);
     
    		dialog.addKeyListener(new ActionEvents(ihm));
    	}
    }
    Merci bien

  4. #24
    Membre régulier
    Homme Profil pro
    Étudiant
    Inscrit en
    Janvier 2016
    Messages
    213
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Janvier 2016
    Messages : 213
    Points : 96
    Points
    96
    Par défaut
    Le message d'erreur est le suivant :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at EVENT.UtilityMethods.createJTabbed(UtilityMethods.java:71)
    	Code -> IhmConverter.getHDMT().add(tabbedPane);
    	at EVENT.ActionEvents.btnLoad_click(ActionEvents.java:74)
    	Code -> utiMethods.createJTabbed(fc.getSelectedFile());
    	at EVENT.ActionEvents.actionPerformed(ActionEvents.java:44)
    	Code -> if (e.getSource() == toolBar.getBtnLoad()) btnLoad_click();
    	at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    	at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    	at javax.swing.plaf.basic.BasicButtonListener$Actions.actionPerformed(Unknown Source)
    	at javax.swing.SwingUtilities.notifyAction(Unknown Source)
    	at javax.swing.JComponent.processKeyBinding(Unknown Source)
    	at javax.swing.KeyboardManager.fireBinding(Unknown Source)
    	at javax.swing.KeyboardManager.fireKeyboardAction(Unknown Source)
    	at javax.swing.JComponent.processKeyBindingsForAllComponents(Unknown Source)
    	at javax.swing.JComponent.processKeyBindings(Unknown Source)
    	at javax.swing.JComponent.processKeyEvent(Unknown Source)
    	at java.awt.Component.processEvent(Unknown Source)
    	at java.awt.Container.processEvent(Unknown Source)
    	at java.awt.Component.dispatchEventImpl(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source)
    	at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
    	at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
    	at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
    	at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
    	at java.awt.Component.dispatchEventImpl(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Window.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    	at java.awt.EventQueue.access$000(Unknown Source)
    	at java.awt.EventQueue$1.run(Unknown Source)
    	at java.awt.EventQueue$1.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue$2.run(Unknown Source)
    	at java.awt.EventQueue$2.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue.dispatchEvent(Unknown Source)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.run(Unknown Source)

  5. #25
    Membre régulier
    Homme Profil pro
    Étudiant
    Inscrit en
    Janvier 2016
    Messages
    213
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Janvier 2016
    Messages : 213
    Points : 96
    Points
    96
    Par défaut
    C'est pas trop tôt lol, j'ai enfin corrigé tout ça même si il y a des améliorations à faire pour l'affichage, il m'affiche toute mes donnée en fonction des 4 étapes dites précédemment
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
     
    package EVENT;
     
    import java.awt.Color;
    import java.awt.Font;
    import java.io.File;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
     
    import javax.swing.BorderFactory;
    import javax.swing.JButton;
    import javax.swing.JDialog;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTabbedPane;
    import javax.swing.JTable;
    import javax.swing.table.DefaultTableModel;
     
    import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
    import org.apache.poi.xssf.usermodel.XSSFCell;
    import org.apache.poi.xssf.usermodel.XSSFRow;
    import org.apache.poi.xssf.usermodel.XSSFSheet;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
     
    import IHM.IhmConverter;
     
    /**
     * Some utility methods for the application
     * 
     * @author t0163126
     */
    public class UtilityMethods 
    {
    	private XSSFWorkbook wb;
    	private XSSFSheet sheet;
    	private XSSFCell cell;
    	private XSSFRow row;
    	private JButton btnYes;
    	private JButton btnNo;
    	private JOptionPane optionPane;
    	private JDialog dialog;
     
    	// Constructor
    	public UtilityMethods() {
    	}
     
    	//Getter
    	public JButton getBtnYes() {
    		return this.btnYes;
    	}
    	public JButton getBtnNo() {
    		return this.btnNo;
    	}
    	public JDialog getDialog() {
    		return this.dialog;
    	}
     
    	/**
             * Create tabs based on file and panel
             * 
             * @param tabPanel
             * @param file
             * @return
             */
    	public JTabbedPane createJTabbed(File file) {
    		JTabbedPane tabbedPane = new JTabbedPane();
    		try {
    			List<JPanel> lstPanel = createTabPanel(file);
    			for (JPanel panel : lstPanel) {
    				tabbedPane.addTab(panel.getName(), // tab name
    						null, // tab icon
    						panel, // tabs panel for the JTable
    						panel.getToolTipText() // tab tool tip text
    						);
    				IhmConverter.getHDMT().add(tabbedPane);
    			}
    			wb.close();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    		return tabbedPane;
    	}
     
    	/**
             * Create a tab panel by excel feel
             * 
             * @param fileName
             * @param excelFile
             * @return
             */
    	public List<JPanel> createTabPanel(File file) {
    		JTable spreadsheet = new JTable();
    		JPanel panel;
    		List<JPanel> lstPanel = new ArrayList<JPanel>();
     
    		try {
    			wb = new XSSFWorkbook(file);
    			// Get the sheet number
    			int nbFeuilles = wb.getNumberOfSheets();
    			for (int i = 0; i < nbFeuilles; i++) {
    				sheet = wb.getSheetAt(i);
    				// Create one JTable by excel feel
    				spreadsheet = createJTableWithExcel(sheet);
     
    				spreadsheet.setFont(new Font(Font.SERIF, Font.PLAIN, 10));
    				JScrollPane elevator = new JScrollPane();
    				elevator.setViewportView(spreadsheet);
    				// Create a panel by excel feel number
    				panel = new JPanel();
    				panel.setBorder(BorderFactory.createTitledBorder(sheet
    						.getSheetName()));
    				panel.setBackground(Color.LIGHT_GRAY);
    				panel.add(elevator);
    				lstPanel.add(panel);
    			}
    			wb.close();
    		} catch (InvalidFormatException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    		return lstPanel;
    	}
     
    	/**
             * Create a JTable depending on the file and the entry sheet number
             * 
             * @param file
             * @param numSheet
             * @return
             */
    	public JTable createJTableWithExcel(XSSFSheet sheet) {
    		// Create new table
    		JTable table = new JTable();
    		// Reading the excel file
    		// Count the line number
    		int nbRows = sheet.getLastRowNum();
    		// Count the column number
    		int nbColumns = numberMaxColumn(sheet);
     
    		System.out.println("In the sheet " + sheet.getSheetName()
    				+ " there are " + nbRows + " lines and " + nbColumns
    				+ " columns");
     
    		// Create new object array
    		Object[][] obj = new Object[nbRows][nbColumns];
     
    		// Array header
    		String[] title = new String[nbColumns];
    		for (int i = 0; i < nbColumns; i++) {
    			title[i] = "#" + i;
    		}
     
    		// Browsing the sheet and retrieves the lines one by one
    		for (int rowContent = 0; rowContent < nbRows; rowContent++) {
    			row = sheet.getRow(rowContent);
     
    			// Browsing the line and retrieves the columns
    			if (row != null) {
    				for (int columnContent = 0; columnContent < nbColumns; columnContent++) {
    					// Retrieves the cell and this value
    					cell = row.getCell((short) columnContent);
    					Object value = contentCell(cell);
     
    					obj[rowContent][columnContent] = value;
    				}
    			}
    		}
    		table.setModel(new DefaultTableModel(obj, title));
     
    		return table;
    	}
     
    	/**
             * The cell can contain different type of value that should be specifically
             * recovered
             * 
             * @param cell
             * @return
             */
    	private Object contentCell(XSSFCell c) {
    		Object value = null;
     
    		if (c == null) {
    			value = "";
    		} else if (c.getCellType() == XSSFCell.CELL_TYPE_BOOLEAN) {
    			value = c.getBooleanCellValue();
    		} else if (c.getCellType() == XSSFCell.CELL_TYPE_ERROR) {
    			value = c.getErrorCellValue();
    		} else if (c.getCellType() == XSSFCell.CELL_TYPE_FORMULA) {
    			value = c.getCellFormula();
    		} else if (c.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) {
    			value = c.getNumericCellValue();
    		} else if (c.getCellType() == XSSFCell.CELL_TYPE_STRING) {
    			value = c.getStringCellValue();
    		}
    		return value;
    	}
     
    	/**
             * Retrieves the maximum number of column. If a line has 2 empty boxes at
             * the end , they are not taken into account. So to retrieve the number of
             * columns to initialize our table we must browse all lines
             * 
             * @param sheet
             * @return
             */
    	private int numberMaxColumn(XSSFSheet st) {
    		int r = st.getLastRowNum();
    		int max = 0;
    		int s = 0;
    		while (s < r) {
    			if (st.getRow(s) != null) {
    				int c = st.getRow(s).getLastCellNum();
    				if (c > max) {
    					max = c;
    				}
    			}
    			s++;
    		}
    		return max + 1;
    	}
     
    	/**
             * Closing the application
             * 
             * @param ihm
             */
    	public void closeApplication() {
    		btnYes = new JButton();
    		btnYes.addActionListener(new ActionEvents(this));
    		btnYes.setText("Yes, please");
    		btnYes.setToolTipText("Click Yes to quit the application");
    		btnYes.setMnemonic('Y');
     
    		btnNo = new JButton();
    		btnNo.addActionListener(new ActionEvents(this));
    		btnNo.setText("Oops no!");
    		btnNo.setToolTipText("Click No to return in the application");
    		btnNo.setMnemonic('N');
     
    		final Object[] options = { btnYes, btnNo };
     
    		optionPane = new JOptionPane(Final.getMsgExit(), // the message
    				JOptionPane.QUESTION_MESSAGE, // the type of message
    				JOptionPane.YES_NO_OPTION, // the type of dialog box
    				null, // A icon
    				options, // Array String for buttons text
    				options[0] // Where 0 is the index of preset button - one that
    							// will be chosen if we press Enter
    		);
     
    		dialog = optionPane.createDialog(Final.getMsgExitTitle());
    		dialog.setAlwaysOnTop(true);
    		dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
    		dialog.toFront();
    		dialog.setFocusable(true);
    		dialog.setVisible(true);
    	}
    }
    MERCI

  6. #26
    Modérateur
    Avatar de joel.drigo
    Homme Profil pro
    Ingénieur R&D - Développeur Java
    Inscrit en
    Septembre 2009
    Messages
    12 430
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 54
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Ingénieur R&D - Développeur Java
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2009
    Messages : 12 430
    Points : 29 131
    Points
    29 131
    Billets dans le blog
    2
    Par défaut
    Citation Envoyé par nadong94 Voir le message
    Salut, j'ai enlever tout les statics pour être tranquille
    Oui, enfin, quand je disais d'enlever les "statics", je ne parlais pas d'enlever le mot clef, je parlais d'enlever les variables statiques !!! Il est inutile d'avoir des variables de classes (cf mon exemple !!!). M'enfin c'est un moindre mal.

    Citation Envoyé par nadong94 Voir le message
    mais le résultat et le même :
    Normal, tu n'utilises qu'une seule instance de UtilityMethods à la fois. Le jour où tu auras pris l'habitude de faire tout avec des variables de classe statiques ce qui peut être fait avec des variables locales, ou des paramètres de méthode (ou valeur de retour), et que tu passeras en multi-instances, voire en multi-processes, tu vas t'amuser...

    Mais l'important est que ton programme fonctionne déjà...
    L'expression "ça marche pas" ne veut rien dire. Indiquez l'erreur, et/ou les comportements attendus et obtenus, et donnez un Exemple Complet Minimal qui permet de reproduire le problème.
    La plupart des réponses à vos questions sont déjà dans les FAQs ou les Tutoriels, ou peut-être dans une autre discussion : utilisez la recherche interne.
    Des questions sur Java : consultez le Forum Java. Des questions sur l'EDI Eclipse ou la plateforme Eclipse RCP : consultez le Forum Eclipse.
    Une question correctement posée et rédigée et vous aurez plus de chances de réponses adaptées et rapides.
    N'oubliez pas de mettre vos extraits de code entre balises CODE (Voir Mode d'emploi de l'éditeur de messages).
    Nouveau sur le forum ? Consultez Les Règles du Club.

+ Répondre à la discussion
Cette discussion est résolue.
Page 2 sur 2 PremièrePremière 12

Discussions similaires

  1. [Apache POI] Créer group dans excel
    Par Mattorre dans le forum Documents
    Réponses: 1
    Dernier message: 11/05/2011, 21h41
  2. problème création rapport excel avec apache poi
    Par jakcam dans le forum API standards et tierces
    Réponses: 7
    Dernier message: 07/10/2009, 14h07
  3. Réponses: 1
    Dernier message: 22/06/2009, 14h26
  4. [XL-2007] Type de fichier pour Excel 2007: xls, xlsx, xlsm ou xlsb ?
    Par APE3392 dans le forum Excel
    Réponses: 2
    Dernier message: 18/06/2009, 18h02
  5. Réponses: 0
    Dernier message: 27/10/2008, 14h58

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo