Ajouter un JScrollPane a un JPanel

simsima1448 Messages postés 3 Date d'inscription vendredi 25 janvier 2008 Statut Membre Dernière intervention 5 novembre 2008 - 4 nov. 2008 à 13:22
simsima1448 Messages postés 3 Date d'inscription vendredi 25 janvier 2008 Statut Membre Dernière intervention 5 novembre 2008 - 5 nov. 2008 à 12:31
: Salut a tous

Je dois ajouter un JScrollPane a un JPanel qui contient un tableau de JTextField mais je ne sais pas comment ajouter le JScrollPane si vous pouvez m'aider voila le code et Merci d'avance

import java.awt.*;
import java.io.*;
import javax.swing.*; // gerer l'interface graphique
import java.awt.event.*;// gérer les evenements
public class GestionTextField extends JFrame
{ //déclaration des variables
private JFrame frameGestion;
private JPanel panelGestion;
private Container cGestion;
private JButton btGestion;
private JLabel lblGestion;
private JTextField txtGestion []= new JTextField[50];;
private int i=20, j=40, k=0;
private JScrollPane jBar; ///////////////////////////////

/*<*><*><*><*><*><*> - Constructeur - <*><*><*><*><*><*><*/
public GestionTextField()
{ //////////Instancier les variables ////////////
frameGestion = new JFrame("Gestion des TextField");
cGestion = frameGestion.getContentPane();
panelGestion = new JPanel();
btGestion = new JButton("Fermer");
lblGestion=new JLabel("Les TextField :");
jBar = new JScrollPane(); /////////////////////////////////////////////

for(i=0;i<50;i++)
{ txtGestion[i] = new JTextField();
txtGestion[i].addActionListener(ajouterTxt);
}
//////// Configurer les composantes /////////
cGestion.setLayout(null);
frameGestion.setBounds(0, 0, 600, 600);

lblGestion.setBounds(20,20,140,30);
lblGestion.setFont(new Font("Dialog",Font.BOLD,18));
frameGestion.add(lblGestion);

btGestion.setBounds(230,520,100,30);
btGestion.setForeground(Color.black);
frameGestion.add(btGestion);

panelGestion.setLayout(null);
panelGestion.setBounds(0,70,600,400);

jBar = new JScrollPane(panelGestion);/////////////////////////////////

txtGestion[0].setBounds(20,0,500,30);
panelGestion.add(txtGestion[0]);

panelGestion.setPreferredSize(new Dimension(1000,1000));
add(jBar);/////////////////////////////////////////////////////////////

panelGestion.setVisible(true);
frameGestion.getContentPane().add(panelGestion);
frameGestion.setVisible(true);

////La fermeture de la fenêtre
frameGestion.addWindowListener(new WindowListener()
{ public void windowOpened(WindowEvent e) { }
public void windowClosing(WindowEvent e)
{ frameGestion.setVisible(false);
}
public void windowClosed(WindowEvent e) { }
public void windowIconified(WindowEvent e) { }
public void windowDeiconified(WindowEvent e) { }
public void windowActivated(WindowEvent e) { }
public void windowDeactivated(WindowEvent e) { }
});
////ecouteur des évenements de boutons
btGestion.addActionListener(boutonAct); //pour fermer la fenetre
}
/*<*><*><*>L'ecouteur sur le btGestion pr fermer la fenetre<*><*><*>*/
private ActionListener boutonAct = new ActionListener()
{ public void actionPerformed(ActionEvent e)
{ if (e.getSource() == btGestion)
{ frameGestion.setVisible(false);
}
}
};
/*<*><*><* L'ecouteur sur le txtGestion pr ajouter TextField *<*><*>*/
private ActionListener ajouterTxt = new ActionListener()
{ public void actionPerformed(ActionEvent e)
{ if (e.getSource() == txtGestion[k])
{ txtGestion[k+1].setBounds(20,j,500,30);
j=j+40;
txtGestion[k+1].setText(" ");
panelGestion.add(txtGestion[k+1]);
panelGestion.setVisible(true);
k=k+1;
}
}
};
/*<*><*><*><*><*><*La fonction Main *><*><*><*><*><*><*>*/
public static void main(String[] args)
{ GestionTextField essaye = new GestionTextField();
}
}
<!-- / message -->

2 réponses

cs_Chatbour Messages postés 764 Date d'inscription jeudi 27 juillet 2006 Statut Membre Dernière intervention 6 septembre 2010 19
4 nov. 2008 à 20:43
Salut,

un petit exemple pour illustrer ce que tu cherches :

JTextField tab[] = new JTextField[10];
JPanel panel = new JPanel(new GridLayout(10, 1));
for (int i=0 ; i<10 ; i++) {
    tab[i] = new JTextField(20);
    panel.add(tab[i]);
}
JScrollPane scrollPane = new JScrollPane(panel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
this.getContentPane().add(scrollPane);

Cordialement..

_____________________

Vos avis et critiques sur le livre "Objets réactifs en java" de Frédéric Boussinot : contactez moi par MP..
0
simsima1448 Messages postés 3 Date d'inscription vendredi 25 janvier 2008 Statut Membre Dernière intervention 5 novembre 2008
5 nov. 2008 à 12:31
Salut
Merci bcp de m'avoir répondu
j'ai essayé de faire comme l'exemple mais ça ne marche pas je ne sais pas ou se trouve l'erreur dans mon code
0
Rejoignez-nous