Ajouter un commentaire

Niroken

Hello,

Voici un exemple simple qui te permet d afficher un JTable et d afficher son contenu quand tu cliques sur un boutton, de plus il te permet d afficher également le contenu de la case sélectionnée s il y en a une.

package graphic;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTable;

public class JTableViewer extends JFrame {

	private static final long serialVersionUID = 1L;
	
	private JTable mTable;
	private JButton mButton;
	
	public JTableViewer() {
		setTitle("TankGame");
        setLayout(new FlowLayout());
        setSize(new Dimension(500, 500));
        
        String[] vColumnsNames = {"Example 1", "Example 2"};
                
        Object[][] vDatas = {
        		{"Pouet 1", "Pouet 2"},
        		{"Pouet 3", "Pouet 4"}
        };
        
        mTable = new JTable(vDatas, vColumnsNames);
                
        mButton = new JButton("Voir Contenu mTable");
        mButton.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				displayTableContent();
				displaySelectedDataInJTable();
			}
        	
        });
        
        add(mTable);
        add(mButton);
                               
        setVisible(true);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	}
	
	private void displayTableContent() {
		for (int i = 0; i < mTable.getRowCount(); i++) {
			for (int j = 0; j < mTable.getColumnCount(); j++) {
				System.out.println(mTable.getValueAt(i, j).toString());
			}
		}				
	}
	
	private void displaySelectedDataInJTable() {
		if (mTable.getSelectedColumn() != -1) {
			System.out.println(mTable.getValueAt(mTable.getSelectedRow(), mTable.getSelectedColumn()));
		}
	}

}

Le lanceur :

import graphic.JTableViewer;


public class Program {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new JTableViewer();
	}

}

Bonne chance,
Niroken

Filtered HTML

Plain text

CAPTCHA
Cette question permet de vérifier que vous n'êtes pas un robot spammeur :-)
 BBBB   V     V  Y   Y  U   U   SSS  
B B V V Y Y U U S
BBBB V V Y U U SSS
B B V V Y U U S
BBBB V Y UUU SSSS