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
|
package pierre;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.util.Vector;
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class TestJTable extends JFrame implements java.awt.event.ActionListener
{
private static final long serialVersionUID = 1L;
int nbCells = 4 ; // nombre de case par ligne et colonne
protected String[][] valeur = new String[nbCells][nbCells];
protected JPanel JFrameContentPane = new JPanel();
protected JPanel panel = null;
protected static DefaultTableModel dtm ;
protected JButton button1, button2, button3 ;
protected JLabel message =null;
private static JTable jt = null;
String [] Filles = {"Jeanne", "Monique", "Hélène", "Justine", "Alexa", "Julie", "Carla", "Roxane"};
String [] Gars = {"Pierre", "Paul", "Jean", "Henri", "Georges", "Alain", "Jacques", "André"};
public TestJTable()
{
super("Test Jtable");
panel = new JPanel();
JFrameContentPane.setLayout(null);
setSize(309, 344);
setContentPane(JFrameContentPane);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new javax.swing.JPanel();
panel.setName("MonPanneau");
panel.setLayout(new java.awt.GridLayout());
panel.setBounds(10, 44, 272, 148);
panel.setBackground(Color.white);
// on définit la grille maintenant
dtm = new DefaultTableModel(nbCells,nbCells);
jt = new JTable(dtm);
panel.add(jt);
jt.setGridColor( Color.black) ;
// maintenant on ajuste la taille des cases au panneau
Dimension dm = panel.getSize();
double h= dm.getHeight()/nbCells ;
int y = (int)Math.round(h);
jt.setRowHeight(y);
JFrameContentPane.add(panel);
button1 = new JButton();
button1.setBounds(215, 230, 70, 25);
button1.setText("Fin");
button2 = new JButton();
button2.setBounds(20, 230, 70, 25);
button2.setText("Filles");
button3 = new JButton();
button3.setBounds(110, 230, 90, 25);
button3.setText("Garçons");
JFrameContentPane.add(button1);
JFrameContentPane.add(button2);
JFrameContentPane.add(button3);
button2.addActionListener(this);
button1.addActionListener(this);
message = new javax.swing.JLabel();
message.setBounds(10, 210, 242, 12);
message.setForeground(java.awt.Color.red);
message.setHorizontalTextPosition(JLabel.CENTER);
message.setFont(new java.awt.Font("Times New Roman", 1, 12));
message.setText("Cliquer sur un des boutons pour colorier.");
JFrameContentPane.add(message);
this.setLocationRelativeTo(null); // centrage au milieu de l'écran
fillCells(); // on remplit les cases
setVisible(true);
}
private void fillCells()
{
int index=0;
for (int i = 0; i< 4; i++)
{
int j=0;
while (j<4)
{
jt.setValueAt(Filles[index], i, j++);
jt.setValueAt(Gars[index++], i, j++);
}
}
}
@Override
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == button1) { System.exit(0);}
if (e.getSource() == button2) { coloringFilles();}
if (e.getSource() == button3) { coloringGars();}
}
private void coloringFilles() {coloringCells(Filles, Color.YELLOW);}
private void coloringGars() {coloringCells(Gars, Color.RED);}
private void coloringCells(String[] tab, Color c)
{
Vector v = new Vector();
for (int i = 0; i<tab.length; i++) v.add(i,tab[i]); // fill the vector
for (int row=0; row<nbCells; row++)
{
for (int col=0;col<nbCells; col++)
{
String wk = (String) jt.getValueAt(row, col);
int indx = v.indexOf(wk);
if( indx==-1) continue;
// IL FAUT COLORIER CETTE CASE, MAIS NOUS NE SAVONS PAS COMMENT FAIRE.
}
}
}
public static void main(String args[])
{
new TestJTable();
}
} |
Partager