(debian,eclipse 3.4) problèmes de base en utilisant swing
bonjour,
une petite question : j'ai pour but de créer une appli qui a le comportement suivant : une classe principale, avec une méthode main, instancie et affiche une JFrame.Cette JFrame possède un menu dont un item instancie et affiche une JDialog, qui contient une JTable.
problème : avec les listings donnés ci-dessous, j'ai les problèmes suivants : la JFrame s'affiche bien, je clique sur l'item de menu, et là il se passe disons 5 secondes avant que la JDialog s'affiche.c'est gênant. autre problème : la JTable ne s'affiche pas, et j'arrive en survolant la JDialog à faire apparaître les titres de colonnes. là aussi c'est gênant.
savez-vous comment je peux arranger les choses?
codes:
classe principale:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| package source_client2;
import javax.swing.SwingUtilities;
public class main_class {
/**
* @param args
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run(){
main_window fenetre = new main_window();
fenetre.setVisible(true);
}
});
}
} |
JFrame:
Code:
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
| package source_client2;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;
/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class main_window extends javax.swing.JFrame {
{
//Set Look & Feel
try {
javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
} catch(Exception e) {
e.printStackTrace();
}
}
private JMenuBar jMenuBar1;
private JMenuItem MI_list_customers;
private JMenu menu_customer;
private JMenu menu_file;
private JMenu jMenu1;
/**
* Auto-generated main method to display this JFrame
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
main_window inst = new main_window();
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}
});
}
public main_window() {
super();
initGUI();
}
private void initGUI() {
try {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
{
jMenuBar1 = new JMenuBar();
setJMenuBar(jMenuBar1);
{
jMenu1 = new JMenu();
jMenuBar1.add(jMenu1);
jMenu1.setText("File");
{
menu_file = new JMenu();
jMenu1.add(menu_file);
menu_file.setText("File");
}
}
{
menu_customer = new JMenu();
jMenuBar1.add(menu_customer);
menu_customer.setText("Customer");
{
MI_list_customers = new JMenuItem();
menu_customer.add(MI_list_customers);
MI_list_customers.setText("List customers");
MI_list_customers.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
MI_list_customersActionPerformed(evt);
}
});
}
}
}
pack();
setSize(400, 300);
} catch (Exception e) {
e.printStackTrace();
}
}
private void MI_list_customersActionPerformed(ActionEvent evt) {
/*SwingUtilities.invokeLater(new Runnable(){
public void run(){
JFrame frame=new JFrame();
dlg_list_customers une_listC=new dlg_list_customers(frame);
une_listC.setSize(300, 200);
une_listC.setVisible(true);
}
});*/
JDialog une_listC=new dlg_list_customers();
une_listC.setVisible(true);
}
} |
JDialog:
Code:
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
| package source_client2;
import java.awt.BorderLayout;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
/**
* This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
* Builder, which is free for non-commercial use. If Jigloo is being used
* commercially (ie, by a corporation, company or business for any purpose
* whatever) then you should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details. Use of Jigloo implies
* acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
* PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
* ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class dlg_list_customers extends javax.swing.JDialog {
private JScrollPane jScrollPane1;
private JTable jTable1;
/**
* Auto-generated main method to display this JDialog
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame();
dlg_list_customers inst = new dlg_list_customers(frame);
inst.setVisible(true);
}
});
}
public dlg_list_customers(JFrame frame) {
super(frame);
initGUI();
// init de la table
}
public dlg_list_customers(){
super();
initGUI();
}
private void initGUI() {
try {
jScrollPane1 = new JScrollPane();
getContentPane().add(jScrollPane1, BorderLayout.NORTH);
jScrollPane1.setPreferredSize(new java.awt.Dimension(390, 223));
// TableModel jTable1Model = new DefaultTableModel();
jTable1 = new JTable(new jTable1Model());
jScrollPane1.setViewportView(jTable1);
jTable1.setPreferredSize(new java.awt.Dimension(382, 164));
setSize(400, 300);
} catch (Exception e) {
e.printStackTrace();
}
}
} |
le modèle de la JTable(classe à part):
Code:
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
| package source_client2;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
import javax.servlet.jsp.tagext.TryCatchFinally;
import javax.swing.table.AbstractTableModel;
public class jTable1Model extends AbstractTableModel {
private String[] columnNames = {"ID","FirstName","LastName","Telephone","Email","City","Country"};
private Object[][] data = null;
public int getColumnCount() {return columnNames.length;}
public int getRowCount() {return data.length;}
public String getColumnName(int col) {return columnNames[col];}
public Object getValueAt(int row, int col) {return data[row][col];}
public Class getColumnClass(int c) {return getValueAt(0,c).getClass();}
public boolean isCellEditable(int row, int col) {return (col < 2);}
public void setValueAt(Object value, int row, int col) {
data[row][col] = value;
// Notification explicite des
fireTableCellUpdated(row,col);
// modifications aux vues
}
public jTable1Model (){
try {
Context ctx=new InitialContext();
Object rem0=ctx.lookup("petstore_ear/CustomerBean/remote");
ejb.customer.CustomerRemote rem =
(ejb.customer.CustomerRemote) PortableRemoteObject.narrow(rem0,
ejb.customer.CustomerRemote.class);
for (source_jpa.customer.Customer client : rem.findCustomers()) {
}
//
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
} |
je précise que j'utilise eclipse et jigloo(plugin GUI).
olivier.