Précédent   Forum du club des développeurs et IT Pro > Java > Interfaces Graphiques en Java > SWT/JFace
SWT/JFace Forum d'entraide pour les API SWT/JFace. Avant de poster -> FAQ SWT/JFace
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse
 
Outils de la discussion
Publicité
'
Vieux 03/01/2013, 15h09   #1
Péchereau
Futur Membre du Club
 
Inscription : mai 2003
Messages : 67
Détails du profil
Informations forums :
Inscription : mai 2003
Messages : 67
Points : 19
Points : 19
Par défaut Paramétrer la taille d'une fenêtre Popup ou d'un tableau

Bonjour à tous,
dans une application en SWT/JFace avec une fenêtre qui ouvre une autre fenêtre de type "popup".
Dans cette "fenêtre popup", il y a un tableau avec un bouton (bas de la fenêtre). Généralement, le tableau possède quelques lignes et la fenêtre popup s'ouvre correctement (fichier "popup-sans-pb"). Mais quand le tableau possède beaucoup de lignes, celui-ci prend toute la place à l'écran (au moins en hauteur) et même plus, et du coup, je n'ai plus accès au bouton (PJ "popup-avec-pb1").
Du coup, j'essaye de modifier la taille de la fenêtre (dans le code, je force avec "shell.setSize(...)", mais cette fois-ci c'est le tableau qui prend toute la place (pas de scrollPane vertical), et je ne peux pas voir tout le tableau, encore moins le bouton.

Cela peut paraître bête, mais comment faire pour régler correctement la taille pour que, si nécessaire, le tableau affiche un scrollPane vertical pour que l'on puisse voir toutes les lignes du tableau, et qu'il y ait assez de place pour le bouton en bas de la fenêtre popup ?

J'ai essayé de modifier la taille de la fenêtre via la méthode "setSize()" de ma classe "TestExportPopup".
La méthode qui crée le tableau dans la fenêtre (méthode "createTable()", appelée dans "createContent()" de la classe "TestExportPopup") précise bien que l'on veut un scroll vertical - SWT.V_CHECK dans le style du TableViewer-.

Bref, où est le problème ?


Voici le code :
Tout d'abord, une classe mère pour toutes les fenêtres de type "popup" dans notre application :
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
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Shell;
 
public abstract class AbstractPopup {
	protected Shell parentShell;
	protected Shell shell;
 
	public AbstractPopup(Shell aParent, boolean isModal) {
		parentShell = aParent;
		if (isModal) {
			shell = new Shell(aParent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		} else {
			shell = new Shell(aParent, SWT.DIALOG_TRIM);
		}
	}
 
	public AbstractPopup(Shell aParent) {
		parentShell = aParent;
		shell = new Shell(aParent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
	}
 
	public Object show() {
		shell.setLayout(new GridLayout());
 
		shell.setText(getTitle());
		createContent();
		setSize();
 
		centerPopup();
		shell.open();
 
		return null;
	}
 
	protected abstract void createContent();
 
	protected abstract String getTitle();
 
	protected abstract void setSize();
 
	private void centerPopup() {
		// Move the dialog to the center of the top level shell.
		Rectangle shellBounds = parentShell.getBounds();
		Point dialogSize = shell.getSize();
		shell.setLocation(shellBounds.x + (shellBounds.width - dialogSize.x) / 2, shellBounds.y
				+ (shellBounds.height - dialogSize.y) / 2);
	}
 
}
Ma classe en question :
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
public class TestExportPopup extends AbstractPopup {
 
	private static Logger LOGGER = Logger.getLogger(TestExportPopup.class);
 
	private Table tradeTable;
	private TableViewer tradeTableViewer;
	private Button sendButton;
 
	public TestExportPopup(Shell aParent, // autre parametres 
) {
		super(aParent);
                // autre parametres settés
	}
 
	@Override
	protected String getTitle() {
		return "Test Export";
	}
 
	@Override
	protected void setSize() {
		Rectangle bounds = Display.getCurrent().getBounds();
		int height = bounds.height;
		int width = bounds.width;
		System.err.println("Bounds: x=" + bounds.x + "+, y=" + bounds.y + ", height=" + height + ", width=" + width);
 
		Point size = shell.getSize();
		int x = size.x;
		int y = size.y;
		if (x > width) {
			x = width;
		}
		if (y > (height - 50)) {
			y = height - 50;
		}
 
		System.err.println("Size (before setSize()=" + shell.getSize() + ")");
		shell.setSize(x, y);
		System.err.println("Size (after setSize()=" + shell.getSize() + ")");
	}
 
	@Override
	protected void createContent() {
		GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
		Composite content = new Composite(shell, SWT.NONE);
		content.setLayout(new GridLayout());
		content.setLayoutData(gridData);
 
		tradeTable = createTable(content);
 
		sendButton = new Button(content, SWT.CENTER);
		sendButton.setText("Send");
		sendButton.addSelectionListener(new SelectionListener() {
			@Override
			public void widgetSelected(SelectionEvent e) {
                                // on recupere les lignes du tableau et on les envoie a un serveur pour traitement
				List<MonObjetVO> trades = new ArrayList<MonObjetVO>();
				for (TableItem item : tradeTable.getItems()) {
					if (item.getChecked()) {
						trades.add((MonObjetVO) item.getData());
					}
				}
                                // etc
			}
		});
 
		try {
			List<MonObjetVO> trades = getTestTrades();
                        // tri des objets par id avec le comparator adequat
 
			for (MonObjetVO trade : trades) {
				TableItem item = new TableItem(tradeTable, SWT.NONE);
 
				String[] line = new String[8];
 
				line[0] = "";
                                // line[1], line[2] ... line[7] avec les informations correspondantes 
 
				item.setText(line);
				item.setData(trade);
				item.setChecked(true);
			}
		} catch (NxServiceException e) {
			LOGGER.error(e.getMessage(), e);
			MessageDialog.openError(shell, "Error", "Error while loading the trades");
		}
 
		Point size = shell.getSize();
		System.err.println("Size (before pack()=" + size + ")");
		shell.pack();
		size = shell.getSize();
		System.err.println("Size (after pack()=" + size + ")");
	}
 
	protected Table createTable(Composite content) {
		tradeTableViewer = new TableViewer(content, SWT.CHECK | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL
				| SWT.FULL_SELECTION);
		// Table table = new Table(content, SWT.CHECK | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL | SWT.FULL_SELECTION);
		Table table = tradeTableViewer.getTable();
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
 
		TableColumn tc = new TableColumn(table, SWT.LEFT);
		tc.setText("");
		tc.setWidth(30);
 
		tc = new TableColumn(table, SWT.LEFT);
		tc.setText("Order ID");
		tc.setWidth(80);
 
		tc = new TableColumn(table, SWT.LEFT);
		tc.setText("Date");
		tc.setWidth(130);
 
		tc = new TableColumn(table, SWT.LEFT);
		tc.setText("Direction");
		tc.pack();
 
		tc = new TableColumn(table, SWT.LEFT);
		tc.setText("Quantity");
		tc.setWidth(90);
 
		tc = new TableColumn(table, SWT.LEFT);
		//tc.setText("Instrument");
		tc.setText("Kesako ?");
		tc.setWidth(270);
 
		tc = new TableColumn(table, SWT.LEFT);
		tc.setText("User");
		tc.setWidth(130);
 
		tc = new TableColumn(table, SWT.LEFT);
		tc.setText("Status");
		tc.setWidth(60);
 
		return table;
	}
 
	private List<MonObjetVO> getTestTrades() {
		List<MonObjetVO> trades = new ArrayList<MonObjetVO>();
 
                // renvoie une liste de "MonObjetVO"
 
		return trades;
	}
}
L'appel de cette fenêtre se fait simplement :
Code :
1
2
3
new TestExportPopup(XXX.getShell(), 
// autres parametres
).show();
Très cordialement,
Images attachées
Type de fichier : jpg popup-sans-pb.jpg (100,4 Ko, 2 affichages)
Type de fichier : jpg popup-avec-pb-1.jpg (207,4 Ko, 3 affichages)
Type de fichier : jpg popup-avec-pb-2.jpg (202,0 Ko, 2 affichages)
Péchereau est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 03/01/2013, 17h28   #2
Péchereau
Futur Membre du Club
 
Inscription : mai 2003
Messages : 67
Détails du profil
Informations forums :
Inscription : mai 2003
Messages : 67
Points : 19
Points : 19
argh la solution était pourtant évidente et je ne l'ai pas vu de suite !

1) mettre un GridData sur la Table, aligné en haut
2) déplacer mettre shell.pack() au début de setSize() (sur ce point, je n'ai pas vérifié, mais ça doit être optionnel);

Code :
1
2
3
4
5
6
7
	protected Table createTable(Composite content) {
		tradeTableViewer = new TableViewer(content, SWT.CHECK | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL
				| SWT.FULL_SELECTION);
		Table table = tradeTableViewer.getTable();
		table.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, true));
                // etc
}
et
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
	@Override
	protected void setSize() {
		shell.pack();
 
		Rectangle bounds = Display.getCurrent().getBounds();
		int height = bounds.height;
		int width = bounds.width;
 
		Point size = shell.getSize();
		int x = size.x;
		int y = size.y;
		if (x > (width - 50)) {
			x = width - 50;
		}
		if (y > (height - 50)) {
			y = height - 50;
		}
 
		shell.setSize(x, y);
	}
Péchereau est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Cette discussion est résolue.
Outils de la discussion

Navigation rapide


Fuseau horaire GMT +2. Il est actuellement 23h34.


 
 
 
 
Partenaires

Hébergement Web