Skip to content

Commit c76d994

Browse files
committed
Added thread with SwingUtilities for addPanel(), comments and fixes
1 parent a55c1dc commit c76d994

2 files changed

Lines changed: 37 additions & 25 deletions

File tree

src/main/java/com/devparada/frame/InitFrame.form

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@
109109
<AuxValue name="FormSettings_listenerGenerationStyle" type="java.lang.Integer" value="0"/>
110110
<AuxValue name="FormSettings_variablesLocal" type="java.lang.Boolean" value="false"/>
111111
<AuxValue name="FormSettings_variablesModifier" type="java.lang.Integer" value="2"/>
112-
<AuxValue name="designerSize" type="java.awt.Dimension" value="-84,-19,0,5,115,114,0,18,106,97,118,97,46,97,119,116,46,68,105,109,101,110,115,105,111,110,65,-114,-39,-41,-84,95,68,20,2,0,2,73,0,6,104,101,105,103,104,116,73,0,5,119,105,100,116,104,120,112,0,0,1,-99,0,0,2,-9"/>
112+
<AuxValue name="designerSize" type="java.awt.Dimension" value="-84,-19,0,5,115,114,0,18,106,97,118,97,46,97,119,116,46,68,105,109,101,110,115,105,111,110,65,-114,-39,-41,-84,95,68,20,2,0,2,73,0,6,104,101,105,103,104,116,73,0,5,119,105,100,116,104,120,112,0,0,0,-85,0,0,1,-29"/>
113113
</AuxValues>
114114

115115
<Layout class="org.netbeans.modules.form.compat2.layouts.DesignBoxLayout">
@@ -140,19 +140,16 @@
140140
<DimensionLayout dim="0">
141141
<Group type="103" groupAlignment="0" attributes="0">
142142
<Group type="102" attributes="0">
143-
<EmptySpace pref="202" max="32767" attributes="0"/>
143+
<EmptySpace max="32767" attributes="0"/>
144144
<Group type="103" groupAlignment="0" attributes="0">
145-
<Group type="102" attributes="0">
145+
<Group type="102" alignment="1" attributes="0">
146146
<Component id="jLblIntro" min="-2" pref="265" max="-2" attributes="0"/>
147-
<EmptySpace max="32767" attributes="0"/>
147+
<EmptySpace min="-2" pref="53" max="-2" attributes="0"/>
148148
<Component id="jBtnStatus" min="-2" pref="104" max="-2" attributes="0"/>
149149
</Group>
150-
<Group type="102" attributes="0">
151-
<EmptySpace pref="280" max="32767" attributes="0"/>
152-
<Component id="jBtnIntroAdd" min="-2" pref="122" max="-2" attributes="0"/>
153-
</Group>
150+
<Component id="jBtnIntroAdd" alignment="1" min="-2" pref="122" max="-2" attributes="0"/>
154151
</Group>
155-
<EmptySpace min="-2" pref="152" max="-2" attributes="0"/>
152+
<EmptySpace min="129" pref="129" max="-2" attributes="0"/>
156153
</Group>
157154
</Group>
158155
</DimensionLayout>

src/main/java/com/devparada/frame/InitFrame.java

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import javax.swing.JOptionPane;
2929
import javax.swing.JPanel;
3030
import javax.swing.JTextField;
31+
import javax.swing.SwingUtilities;
3132

3233
/**
3334
*
@@ -171,16 +172,14 @@ public void actionPerformed(java.awt.event.ActionEvent evt) {
171172
jPanelIntroLayout.setHorizontalGroup(
172173
jPanelIntroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
173174
.addGroup(jPanelIntroLayout.createSequentialGroup()
174-
.addContainerGap(202, Short.MAX_VALUE)
175+
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
175176
.addGroup(jPanelIntroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
176-
.addGroup(jPanelIntroLayout.createSequentialGroup()
177+
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelIntroLayout.createSequentialGroup()
177178
.addComponent(jLblIntro, javax.swing.GroupLayout.PREFERRED_SIZE, 265, javax.swing.GroupLayout.PREFERRED_SIZE)
178-
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
179+
.addGap(53, 53, 53)
179180
.addComponent(jBtnStatus, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE))
180-
.addGroup(jPanelIntroLayout.createSequentialGroup()
181-
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 280, Short.MAX_VALUE)
182-
.addComponent(jBtnIntroAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE)))
183-
.addGap(152, 152, 152))
181+
.addComponent(jBtnIntroAdd, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE))
182+
.addGap(129, 129, 129))
184183
);
185184
jPanelIntroLayout.setVerticalGroup(
186185
jPanelIntroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
@@ -229,7 +228,7 @@ private void jBtnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST
229228
System.out.println("False");
230229
jDlgAdd.setVisible(false);
231230
database.addRow(serverIp);
232-
addPanel(ipServerArray[0], port, getHostIpDialog());
231+
addPanel(ipServerArray[0], port);
233232
} else {
234233
System.out.println("True");
235234
jDlgAdd.setVisible(true);
@@ -289,11 +288,20 @@ private void start() {
289288
for (Object[] dataServer : dataServers) {
290289
for (Object data : dataServer) {
291290
String[] ipServer = data.toString().split(":");
292-
addPanel(ipServer[0], Integer.parseInt(ipServer[1]), data.toString());
291+
292+
// Used SwingUtilities to prevent concurrence problems
293+
SwingUtilities.invokeLater(() -> {
294+
addPanel(ipServer[0], Integer.parseInt(ipServer[1]));
295+
});
293296
}
294297
}
295298
}
296299

300+
/**
301+
* Check the JTextField the dialog of dialog add server of the database
302+
*
303+
* @return Return true if checks are correct
304+
*/
297305
private boolean checkAddDialog() {
298306
String JTxtText = getHostIpDialog();
299307

@@ -314,15 +322,22 @@ private boolean checkAddDialog() {
314322
return false;
315323
}
316324

317-
private void addPanel(String ipServer, int port, String JTxtText) {
325+
/**
326+
* Add a panel with the server status with a given layout
327+
*
328+
* @param ipServer server ip
329+
* @param port server port
330+
*/
331+
private void addPanel(String ipServer, int port) {
318332
StatusMCServer statusServer = new StatusMCServer(ipServer, port);
333+
String ipServerPort = ipServer + ":" + port;
319334

320335
JPanel jPanelServer = new JPanel();
321336
JLabel jTxtIMG = new JLabel();
322-
JTextField jTxtHostIp = new JTextField(JTxtText);
323-
JTextField jTxtOnline = new JTextField(statusServer.showDataSection(JTxtText, "online"));
324-
JTextField jTxtVersion = new JTextField(statusServer.showDataSection(JTxtText, "version"));
325-
JTextField jTxtPlayers = new JTextField(statusServer.showDataSection(JTxtText, "players"));
337+
JTextField jTxtHostIp = new JTextField(ipServerPort);
338+
JTextField jTxtOnline = new JTextField(statusServer.showDataSection(ipServerPort, "online"));
339+
JTextField jTxtVersion = new JTextField(statusServer.showDataSection(ipServerPort, "version"));
340+
JTextField jTxtPlayers = new JTextField(statusServer.showDataSection(ipServerPort, "players"));
326341
JButton jBtnEdit = new JButton("Edit");
327342
JButton jBtnDelete = new JButton("Delete");
328343

@@ -336,7 +351,7 @@ private void addPanel(String ipServer, int port, String JTxtText) {
336351
jTxtIMG.setPreferredSize(new java.awt.Dimension(64, 64));
337352

338353
ImageServer image = new ImageServer();
339-
BufferedImage imageServer = image.showImage(statusServer.showDataSection(JTxtText, "icon"));
354+
BufferedImage imageServer = image.showImage(statusServer.showDataSection(ipServerPort, "icon"));
340355
if (image.checkImage(imageServer)) {
341356
// Create ImageIcon with base64 Image without "data:image/png;base64"
342357
ImageIcon imageIcon = new ImageIcon(imageServer);
@@ -414,7 +429,7 @@ private void addPanel(String ipServer, int port, String JTxtText) {
414429
jPanelServer.add(jBtnEdit, gridBagConstraintsEdit);
415430

416431
jBtnDelete.addActionListener((java.awt.event.ActionEvent evt) -> {
417-
jBtnDeleteActionPerformed(ipServer + ":" + port);
432+
jBtnDeleteActionPerformed(ipServerPort);
418433
});
419434
GridBagConstraints gridBagConstraintsDelete = new GridBagConstraints();
420435
gridBagConstraintsDelete.gridx = 3;

0 commit comments

Comments
 (0)