Affichage composants

Signaler
Messages postés
48
Date d'inscription
mercredi 24 mars 2004
Statut
Membre
Dernière intervention
24 décembre 2009
-
tomski
Messages postés
48
Date d'inscription
mercredi 24 mars 2004
Statut
Membre
Dernière intervention
24 décembre 2009
-
salut a tous,


voila jai un souci lors de laffichage de mes composants: comme jai un gridlayout, la jtable a la meme taille que tous les autres composants.

Est ce que je peux faire en sorte qu'elle apparaisse plus grande(sinon n voit pas les noms des colonnes)?


voila le code


import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.Graphics.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.Properties;import java.util.*;
import java.io.CharArrayWriter;
import java.io.IOException;
import java.io.Writer;
import java.math.*;
import javax.imageio.*;
import javax.imageio.stream.FileImageOutputStream;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.*;
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
import java.awt.image.DataBufferByte;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.Graphics.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.Properties;
import java.util.*;
import java.io.CharArrayWriter;
import java.io.IOException;
import java.io.Writer;
import java.math.*;
import java.awt.Point;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
import java.awt.image.DataBufferByte;
import javax.imageio.ImageIO;
import java.security.spec.AlgorithmParameterSpec;
import java.security.spec.KeySpec;
import java.security.*;
import java.security.spec.*;
import java.math.*;
import javax.imageio.*;
import javax.imageio.stream.FileImageOutputStream;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.*;
import java.util.regex.Pattern;
import java.net.URL.*;
import javax.swing.*;
import javax.swing.table.*;


/*Comparer 2 fichiers Excel*/
/*
*Author= Julie Brunias All Rights Reserved
*
*
*
*/

public class C extends JFrame{


JPanel p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11;
JList l1, l2, l3, l4, l5, l6;
JLabel lb_1, lb_2, lb_c1, lb_c2, lb_c11, lb_c22, lb_c222, lb_c2222, lb_c22222;
static String[] array_1;
static String[] array_2;
JTable tb1, tb2;
JButton choice1BTN, choice2BTN;
JDialog d_choosefile;
String[] str = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" };
JTextField txt_choice1, txt_choice2;
String aLine;
Vector columnNames = new Vector();
Vector data = new Vector();
JTable table;
DefaultTableModel model;
JScrollPane sc1, sc2, sc3, sc4, sc5, sc6, sc7, sc8;
int cols1, cols2;


public C(){

JPanel lstPanel = new JPanel();
p1 = new JPanel();
p2 = new JPanel();
p3 = new JPanel();
p4 = new JPanel();
p5 = new JPanel();
p6 = new JPanel();
p7 = new JPanel();
p8 = new JPanel();
p9 = new JPanel();//p inutile
p10 = new JPanel();//p inutile
p11 = new JPanel();//p inutile
GridLayout grid1 = new GridLayout(8,1,00,00);
GridLayout grid2 = new GridLayout(1,2,00,00);
GridLayout grid3 = new GridLayout(1,2,00,00);
GridLayout grid4 = new GridLayout(1,1,00,00);
GridLayout grid5 = new GridLayout(2,4,00,00);
GridLayout grid6 = new GridLayout(1,1,00,00);
GridLayout grid7 = new GridLayout(1,4,00,00);
GridLayout grid8 = new GridLayout(1,3,00,00);
lstPanel.setLayout(grid1);
p1.setLayout(grid4);
p4.setLayout(grid6);
//p2.setLayout(grid2);
p2.setLayout(grid8);
p5.setLayout(grid2);
p7.setLayout(grid3);
p8.setLayout(grid7);

lb_1= new JLabel("Choix du fichier 1");
lb_2= new JLabel("Choix du fichier 2");
lb_c1= new JLabel("Champs ND du fichier 1");
lb_c11= new JLabel("Champs Date du fichier 1");
lb_c2= new JLabel("Champs ND du fichier 2");
lb_c22= new JLabel("Choix Date du fichier 2");
lb_c222= new JLabel("Champs Equipe du fichier 2");
lb_c2222= new JLabel("Champs UP du fichier 2");
lb_c22222= new JLabel("Champs Commentaire du fichier 2");

txt_choice1= new JTextField(30);
txt_choice2= new JTextField(30);

choice1BTN = new JButton("Ajouter");
choice2BTN = new JButton("Ajouter");

if (array_1 != null){
l1 = new JList(array_1);
l2 = new JList(array_1);
}else{
l1 = new JList();
l2 = new JList();
}
if (array_2 != null){
l3 = new JList(array_2);
l4 = new JList(array_2);
l5 = new JList(array_2);
l6 = new JList(array_2);
}else{
l3 = new JList();
l4 = new JList();
l5 = new JList();
l6 = new JList();
}

l1.setVisibleRowCount(1);
l2.setVisibleRowCount(1);
l3.setVisibleRowCount(1);
l4.setVisibleRowCount(1);
l5.setVisibleRowCount(1);
l6.setVisibleRowCount(1);

sc3= new JScrollPane(l3);
sc4= new JScrollPane(l4);
sc5= new JScrollPane(l5);
sc6= new JScrollPane(l6);
sc7= new JScrollPane(l1);
sc8= new JScrollPane(l2);



choice1BTN.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("in Choice1");
JFileChooser chooser = new JFileChooser("./");
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
int returnVal = chooser.showOpenDialog(getParent());
if (returnVal == JFileChooser.APPROVE_OPTION){
File directory = chooser.getSelectedFile();
try{
String pathname = directory.getPath();
txt_choice1.setText(pathname);
tb1 = createTable(txt_choice1.getText());
sc1 = new JScrollPane(tb1);
cols1 = tb1.getColumnCount();
array_1 = new String[cols1];
for (int i = 0; i< array_1.length; i++){
array_1[i] = new String(""+i);
}
//p3.add(sc1);
}catch(Exception except) {
System.out.println("Problem HERE!");
}//end catch
}else{
System.out.println("Problem in IF!in CHOICE1");
}//end if
}});
choice2BTN.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("in Choice2");
JFileChooser chooser = new JFileChooser("./");
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
int returnVal = chooser.showOpenDialog(getParent());
if (returnVal == JFileChooser.APPROVE_OPTION){
File directory = chooser.getSelectedFile();
try{
String pathname = directory.getPath();
txt_choice2.setText(pathname);
tb2 = createTable(txt_choice2.getText());
sc2 = new JScrollPane(tb2);
cols2 = tb2.getColumnCount();
array_2 = new String[cols2];
for (int i = 0; i< array_2.length; i++){
array_2[i] = new String(""+i);
}
//p6.add(sc2);
}catch(Exception except) {
System.out.println("Problem HERE!");
}//end catch
}else{
System.out.println("Problem in IF! in CHOICE2");
}//end if
}
});

tb1= new JTable();
tb2= new JTable();
p3.add(new JScrollPane(tb1));
p6.add(new JScrollPane(tb2));
p1.add(lb_1);
p2.add(txt_choice1);
p2.add(choice1BTN);
p7.add(lb_c1);
p7.add(sc7);
p7.add(lb_c11);
p7.add(sc8);
p4.add(lb_2);
p5.add(txt_choice2);
p5.add(choice2BTN);
p8.add(lb_c2);
p8.add(sc3);
p8.add(lb_c22);
p8.add(sc4);
p8.add(lb_c222);
p8.add(sc5);
p8.add(lb_c2222);
p8.add(sc6);

lstPanel.add(p1);
lstPanel.add(p2);
lstPanel.add(p3);
lstPanel.add(p7);
lstPanel.add(p4);
lstPanel.add(p5);
lstPanel.add(p6);
lstPanel.add(p8);

JPanel pstr= new JPanel();
String myString = new String();
myString= str[12].toUpperCase()+str[0]+str[3]+str[4]+ " " +str[1]+str[24]+ " "+str[9].toUpperCase()+str[20]+str[11]+
str[8]+str[4]+ " "+str[1].toUpperCase()+str[17]+str[20]+str[13]+str[8]+str[0]+str[18]+ " "+
str[0].toUpperCase()+str[11]+str[11]+ " "+str[17].toUpperCase()+str[8]+str[6]+str[7]+str[19]+str[18]+ " "+
str[17].toUpperCase()+str[4]+str[18]+str[4]+str[17]+str[21]+str[4]+str[3];
JLabel lb = new JLabel(myString);
pstr.add(lb);

add(pstr, BorderLayout.PAGE_END);
add(lstPanel, BorderLayout.CENTER);

setSize(500,500);
setVisible(true);


}//end C()



public static void main(String[] args) {
System.out.println("in MAIN");
C c = new C();
c.getContentPane().add(new MyComponent());
c.setDefaultCloseOperation(EXIT_ON_CLOSE);
c.setVisible(true);
}//end main()


static class MyComponent extends JComponent {
MyComponent() {
System.out.println("in MYCOMPONENT()");
}//end MyComponent
}//end class MyComponent


public JTable createTable(String csv){

try{
FileInputStream fin = new FileInputStream(csv);
BufferedReader br = new BufferedReader(new InputStreamReader(fin));

// extract column names

StringTokenizer st1 = new StringTokenizer(br.readLine(), ";");

while(st1.hasMoreTokens()){
columnNames.addElement(st1.nextToken());
}

// extract data

while ((aLine = br.readLine()) != null){
StringTokenizer st2 = new StringTokenizer(aLine, ";");
Vector row = new Vector();

while(st2.hasMoreTokens()){
row.addElement(st2.nextToken());
}
data.addElement(row);
}
br.close();
}
catch (Exception e){
e.printStackTrace();
}

// Create table using the DefaultTableModel

model = new DefaultTableModel(data, columnNames);
table = new JTable(model);

return table;

}

public int getColumnCount() {
return columnNames.size();
}

}//end public class


(_.:~:._)

2 réponses

Messages postés
15815
Date d'inscription
jeudi 8 août 2002
Statut
Modérateur
Dernière intervention
4 mars 2013
94
Salut,

C'est le but d'un GridLayout que tout les composants aient la même taille.
Combine plutôt un GridBagLayout ou un BorderLayout par exemple pour avoir des tailles différentes.
______________________________________
DarK Sidious
Messages postés
48
Date d'inscription
mercredi 24 mars 2004
Statut
Membre
Dernière intervention
24 décembre 2009

ok darksidiaous, jvais essayer.
sinon ca naffiche pas ma jtable pourtant je me suis inspiree du code de MikeToth, qui lui fonctionne (il affiche une jtable direcetement dans la fenetre)!
Aurais tu une idee?

public JTable makeTB(String csv){

try{
FileInputStream fin = new FileInputStream(csv);
BufferedReader br = new BufferedReader(new InputStreamReader(fin));
// extract column names
StringTokenizer st1 = new StringTokenizer(br.readLine(), ";");
while( st1.hasMoreTokens()){
columnNames.addElement(st1.nextToken());
}
// extract data
while ((aLine = br.readLine()) != null){
StringTokenizer st2 = new StringTokenizer(aLine, ";");
Vector row = new Vector();
while(st2.hasMoreTokens()){
row.addElement(st2.nextToken());
}
data.addElement( row );
}
br.close();
}
catch (Exception e){
e.printStackTrace();
}
// Create table using the DefaultTableModel
mod = new DefaultTableModel(data, cN);
tb = new JTable(mod);

return tb;

}

(_.:~:._)