动态添加一个或多个 JPanels 到 JScrollPane - (Flow)Layout 问题

Dynamically add one or more JPanels to a JScrollPane - (Flow)Layout problems

我在 JTabbedPane[settingsTabs] 中有一个带有 JScrollPane[smtpPanelList] 的设置 window - 在 1 日标签。但是,我希望能够添加随机 nr。 SMTP 设置到 smtpPanelList。此设置被设计到单独的 JPanel[SmtpSettings].

我尝试了很多不同的解决方案,但都没有奏效。 问题是当我将它们添加到 JPanel[lista] - 使用 FlowLayout - 它位于 smtpPanelList 的内部,它已添加,但它显示为水平对齐,而不是在彼此下方。

查看屏幕截图 video

代码如下:

settingsWindow.java

package code;
import java.awt.FlowLayout;
import java.util.ArrayList;
import java.util.prefs.Preferences;

public class settingsWindow extends javax.swing.JFrame {
public static final String SETTINGS = "DLP.settings";
public static final String SETTINGSEMAILCONTENT = "emailContent";
public ArrayList<SmtpSettings> smtp_list = null;
public int ID = -1;
public SmtpSettings smtp_tmp = null;

private Preferences prefs = null;

public settingsWindow() {
    initComponents();
    prefs = Preferences.userRoot().node(SETTINGS);
    emailContent.setText( prefs.get( SETTINGSEMAILCONTENT, emailContent.getText() ) );

    smtpPanelList.setLayout(null);

    smtp_list = new ArrayList<SmtpSettings>();
    smtpPanelList.setViewportView(lista);
    lista.setLayout(new FlowLayout(FlowLayout.TRAILING));

}

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    settingsTabs = new javax.swing.JTabbedPane();
    Tab1 = new javax.swing.JPanel();
    smtpSettingsHeader = new javax.swing.JPanel();
    jLabel1 = new javax.swing.JLabel();
    addNewSmtpAccount = new javax.swing.JButton();
    smtpPanelList = new javax.swing.JScrollPane();
    lista = new javax.swing.JPanel();
    Tab2 = new javax.swing.JPanel();
    emailContentSettingsPanel = new javax.swing.JLabel();
    jScrollPane1 = new javax.swing.JScrollPane();
    emailContent = new javax.swing.JTextArea();
    saveAndCloseButton = new javax.swing.JButton();
    saveButton = new javax.swing.JButton();
    closeButton = new javax.swing.JButton();

    setTitle("Your personal settings");
    setMaximumSize(new java.awt.Dimension(1900, 1900));
    setMinimumSize(new java.awt.Dimension(555, 500));
    setPreferredSize(new java.awt.Dimension(660, 500));

    jLabel1.setText("Your SMTP Accounts to send an email.");

    addNewSmtpAccount.setText("Add new account");
    addNewSmtpAccount.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            addNewSmtpAccountActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout smtpSettingsHeaderLayout = new javax.swing.GroupLayout(smtpSettingsHeader);
    smtpSettingsHeader.setLayout(smtpSettingsHeaderLayout);
    smtpSettingsHeaderLayout.setHorizontalGroup(
        smtpSettingsHeaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, smtpSettingsHeaderLayout.createSequentialGroup()
            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 466, Short.MAX_VALUE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(addNewSmtpAccount, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap())
    );
    smtpSettingsHeaderLayout.setVerticalGroup(
        smtpSettingsHeaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(smtpSettingsHeaderLayout.createSequentialGroup()
            .addGap(2, 2, 2)
            .addGroup(smtpSettingsHeaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(addNewSmtpAccount)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );

    smtpPanelList.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
    smtpPanelList.setMaximumSize(new java.awt.Dimension(600, 32767));
    smtpPanelList.setMinimumSize(new java.awt.Dimension(600, 100));
    smtpPanelList.setPreferredSize(new java.awt.Dimension(600, 10000));
    smtpPanelList.setSize(new java.awt.Dimension(600, 100));

    lista.setMaximumSize(new java.awt.Dimension(617, 32544));
    lista.setMinimumSize(new java.awt.Dimension(600, 100));
    lista.setSize(new java.awt.Dimension(600, 100));
    smtpPanelList.setViewportView(lista);

    javax.swing.GroupLayout Tab1Layout = new javax.swing.GroupLayout(Tab1);
    Tab1.setLayout(Tab1Layout);
    Tab1Layout.setHorizontalGroup(
        Tab1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(Tab1Layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(smtpSettingsHeader, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addContainerGap())
        .addComponent(smtpPanelList, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );
    Tab1Layout.setVerticalGroup(
        Tab1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(Tab1Layout.createSequentialGroup()
            .addComponent(smtpSettingsHeader, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(smtpPanelList, javax.swing.GroupLayout.DEFAULT_SIZE, 363, Short.MAX_VALUE))
    );

    settingsTabs.addTab("SMTP Accounts", Tab1);

    emailContentSettingsPanel.setText("Text of the email to send.");
    emailContentSettingsPanel.setToolTipText("Text of the email to send.");

    emailContent.setColumns(20);
    emailContent.setRows(5);
    jScrollPane1.setViewportView(emailContent);

    javax.swing.GroupLayout Tab2Layout = new javax.swing.GroupLayout(Tab2);
    Tab2.setLayout(Tab2Layout);
    Tab2Layout.setHorizontalGroup(
        Tab2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, Tab2Layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(Tab2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                .addComponent(jScrollPane1)
                .addComponent(emailContentSettingsPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 615, Short.MAX_VALUE))
            .addContainerGap())
    );
    Tab2Layout.setVerticalGroup(
        Tab2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(Tab2Layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(emailContentSettingsPanel)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 372, Short.MAX_VALUE)
            .addContainerGap())
    );

    settingsTabs.addTab("Email content", Tab2);

    saveAndCloseButton.setText("Save and Close");
    saveAndCloseButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            saveAndCloseButtonActionPerformed(evt);
        }
    });

    saveButton.setText("Save");
    saveButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            saveButtonActionPerformed(evt);
        }
    });

    closeButton.setText("Close");
    closeButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            closeButtonActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                .addComponent(settingsTabs)
                .addGroup(layout.createSequentialGroup()
                    .addGap(0, 0, Short.MAX_VALUE)
                    .addComponent(closeButton)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(saveAndCloseButton)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(saveButton)))
            .addContainerGap())
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(7, 7, 7)
            .addComponent(settingsTabs)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(closeButton)
                .addComponent(saveButton)
                .addComponent(saveAndCloseButton))
            .addContainerGap())
    );

    pack();
}// </editor-fold>                        

private void saveAndCloseButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                   
    saveSettings();
    Popups.alertWindow("Your preferences are saved!");
    this.setVisible(false);
}                                                  

private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
    this.setVisible(false);
}                                           

private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
    saveSettings();
    Popups.alertWindow("Your preferences are saved!");
}                                          
public void deleteSMTP(int ID)
{
    int smtp = findSMTPSettingsIndex(ID);

    if(smtp >= 0)
    {
        smtp_list.remove(smtp);
        lista.remove(smtp);
        lista.validate();
        smtpPanelList.validate();
    }
}
public SmtpSettings findSMTPSettings(int ID)
{
    for(int i=0;i<smtp_list.size();i++)
    {
        if(smtp_list.get(i).ID == ID)
            return smtp_list.get(i);
    }

    return null;
}
public int findSMTPSettingsIndex(int ID)
{
    for(int i=0;i<smtp_list.size();i++)
    {
        if(smtp_list.get(i).ID == ID)
            return i;
    }

    return -1;
}
private void addNewSmtpAccountActionPerformed(java.awt.event.ActionEvent evt) {                                                  

    ID++;        
    smtp_tmp = new SmtpSettings(this, ID);
    //smtp_tmp.setBounds(0, (ID * smtp_tmp.getSize().height)+5, 600, 100);
    lista.add(smtp_tmp);
    lista.validate();
    smtpPanelList.validate();

    smtp_list.add(smtp_tmp);


}                                                 
private void saveSettings(){
    prefs.put(SETTINGSEMAILCONTENT, emailContent.getText());
}
/**
 * @param args the command line arguments
 */
public static void main(String args[]) {

    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">

    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(settingsWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(settingsWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(settingsWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(settingsWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new settingsWindow().setVisible(true);
        }
    });
}


private javax.swing.JPanel Tab1;
private javax.swing.JPanel Tab2;
private javax.swing.JButton addNewSmtpAccount;
private javax.swing.JButton closeButton;
private javax.swing.JTextArea emailContent;
private javax.swing.JLabel emailContentSettingsPanel;
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JPanel lista;
private javax.swing.JButton saveAndCloseButton;
private javax.swing.JButton saveButton;
private javax.swing.JTabbedPane settingsTabs;
private javax.swing.JScrollPane smtpPanelList;
private javax.swing.JPanel smtpSettingsHeader;
}

和SmtpSettings.java

package code;

public class SmtpSettings extends javax.swing.JPanel {


    public int ID = -1;
    private settingsWindow stw = null;

    public SmtpSettings(settingsWindow stw, int id) {
        initComponents();
        this.ID = id;
        this.stw = stw;
    }
    public void setSMTPServer(String srv){
        this.smtpServer.setText(srv);
    }
    public String getSMTPServer()
    {
        return this.smtpServer.getText();
    }


    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        smtpServer = new javax.swing.JTextField();
        smtpPort = new javax.swing.JTextField();
        smtpSsl = new javax.swing.JCheckBox();
        smtpUsername = new javax.swing.JTextField();
        smtpPassword = new javax.swing.JPasswordField();
        deleteBtn = new javax.swing.JButton();

        setBorder(new javax.swing.border.LineBorder(new java.awt.Color(200, 200, 200), 1, true));
        setBounds(new java.awt.Rectangle(0, 0, 600, 100));
        setMaximumSize(new java.awt.Dimension(600, 100));
        setPreferredSize(new java.awt.Dimension(600, 100));

        smtpServer.setText("SMTP Server URL");
        smtpServer.setToolTipText("SMTP Server URL");

        smtpPort.setText("SMTP Port");
        smtpPort.setToolTipText("SMTP Port");
        smtpPort.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                smtpPortActionPerformed(evt);
            }
        });

        smtpSsl.setText("SSL");

        smtpUsername.setText("SMTP Username");
        smtpUsername.setToolTipText("SMTP Username");

        smtpPassword.setText("SMTP Password");
        smtpPassword.setToolTipText("SMTP Password");

        deleteBtn.setText("Delete SMTP Account");
        deleteBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteBtnActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(smtpServer, javax.swing.GroupLayout.PREFERRED_SIZE, 227, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(smtpPort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(smtpSsl))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(smtpUsername, javax.swing.GroupLayout.PREFERRED_SIZE, 176, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(smtpPassword, javax.swing.GroupLayout.PREFERRED_SIZE, 186, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 37, Short.MAX_VALUE)
                .addComponent(deleteBtn))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(smtpServer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(smtpPort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(smtpSsl))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(smtpUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(smtpPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(deleteBtn))
                .addContainerGap(17, Short.MAX_VALUE))
        );
    }// </editor-fold>                        

    private void deleteBtnActionPerformed(java.awt.event.ActionEvent evt) {                                          

        if(Popups.confirmWindow("Do you really want to delete?") == 0)
        {
            stw.deleteSMTP(ID);
        }
    }                                         

    private void smtpPortActionPerformed(java.awt.event.ActionEvent evt) {                                         

    }                                        


    private javax.swing.JButton deleteBtn;
    private javax.swing.JPasswordField smtpPassword;
    private javax.swing.JTextField smtpPort;
    private javax.swing.JTextField smtpServer;
    private javax.swing.JCheckBox smtpSsl;
    private javax.swing.JTextField smtpUsername;
}

有人知道吗? 另外,我非常感谢任何 Java 大师提供的任何解决方案或建议,因为我是 Java 的新手。 如果您需要我的代码中的更多内容,例如 Popups class 声明或其他任何内容,请告诉我。

使用类似 GridBagLayout

的东西

private void addNewSmtpAccountActionPerformed(java.awt.event.ActionEvent evt) {

    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridwidth = gbc.REMAINDER;
    gbc.fill = gbc.HORIZONTAL;
    gbc.weightx = 1;

    ID++;
    smtp_tmp = new SmtpSettings(this, ID);
    //smtp_tmp.setBounds(0, (ID * smtp_tmp.getSize().height)+5, 600, 100);
    lista.add(smtp_tmp, gbc);
    lista.revalidate();

    smtp_list.add(smtp_tmp);

}

此外,避免使用 setPreferred/Minimum/MaximumSize

查看 Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? and How to Use GridBagLayout 了解更多详情

哦,smtpPanelList.setLayout(null); 会搞砸到没完没了

import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.ArrayList;
import java.util.prefs.Preferences;

public class settingsWindow extends javax.swing.JFrame {

    public static final String SETTINGS = "DLP.settings";
    public static final String SETTINGSEMAILCONTENT = "emailContent";
    public ArrayList<SmtpSettings> smtp_list = null;
    public int ID = -1;
    public SmtpSettings smtp_tmp = null;

    private Preferences prefs = null;

    public settingsWindow() {
        initComponents();
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        prefs = Preferences.userRoot().node(SETTINGS);
        emailContent.setText(prefs.get(SETTINGSEMAILCONTENT, emailContent.getText()));

//      smtpPanelList.setLayout(null);
        smtp_list = new ArrayList<SmtpSettings>();
//      smtpPanelList.setViewportView(lista);
        lista.setLayout(new GridBagLayout());

    }

    @SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        settingsTabs = new javax.swing.JTabbedPane();
        Tab1 = new javax.swing.JPanel();
        smtpSettingsHeader = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        addNewSmtpAccount = new javax.swing.JButton();
        smtpPanelList = new javax.swing.JScrollPane();
        lista = new javax.swing.JPanel();
        Tab2 = new javax.swing.JPanel();
        emailContentSettingsPanel = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        emailContent = new javax.swing.JTextArea();
        saveAndCloseButton = new javax.swing.JButton();
        saveButton = new javax.swing.JButton();
        closeButton = new javax.swing.JButton();

        setTitle("Your personal settings");
//      setMaximumSize(new java.awt.Dimension(1900, 1900));
//      setMinimumSize(new java.awt.Dimension(555, 500));
//      setPreferredSize(new java.awt.Dimension(660, 500));

        jLabel1.setText("Your SMTP Accounts to send an email.");

        addNewSmtpAccount.setText("Add new account");
        addNewSmtpAccount.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addNewSmtpAccountActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout smtpSettingsHeaderLayout = new javax.swing.GroupLayout(smtpSettingsHeader);
        smtpSettingsHeader.setLayout(smtpSettingsHeaderLayout);
        smtpSettingsHeaderLayout.setHorizontalGroup(
                        smtpSettingsHeaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, smtpSettingsHeaderLayout.createSequentialGroup()
                                        .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 466, Short.MAX_VALUE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(addNewSmtpAccount, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addContainerGap())
        );
        smtpSettingsHeaderLayout.setVerticalGroup(
                        smtpSettingsHeaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(smtpSettingsHeaderLayout.createSequentialGroup()
                                        .addGap(2, 2, 2)
                                        .addGroup(smtpSettingsHeaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                        .addComponent(addNewSmtpAccount)
                                                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        smtpPanelList.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
//      smtpPanelList.setMaximumSize(new java.awt.Dimension(600, 32767));
//      smtpPanelList.setMinimumSize(new java.awt.Dimension(600, 100));
//      smtpPanelList.setPreferredSize(new java.awt.Dimension(600, 10000));
//      smtpPanelList.setSize(new java.awt.Dimension(600, 100));

//      lista.setMaximumSize(new java.awt.Dimension(617, 32544));
//      lista.setMinimumSize(new java.awt.Dimension(600, 100));
//      lista.setSize(new java.awt.Dimension(600, 100));
        smtpPanelList.setViewportView(lista);

        javax.swing.GroupLayout Tab1Layout = new javax.swing.GroupLayout(Tab1);
        Tab1.setLayout(Tab1Layout);
        Tab1Layout.setHorizontalGroup(
                        Tab1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(Tab1Layout.createSequentialGroup()
                                        .addContainerGap()
                                        .addComponent(smtpSettingsHeader, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addContainerGap())
                        .addComponent(smtpPanelList, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        Tab1Layout.setVerticalGroup(
                        Tab1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(Tab1Layout.createSequentialGroup()
                                        .addComponent(smtpSettingsHeader, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(smtpPanelList, javax.swing.GroupLayout.DEFAULT_SIZE, 363, Short.MAX_VALUE))
        );

        settingsTabs.addTab("SMTP Accounts", Tab1);

        emailContentSettingsPanel.setText("Text of the email to send.");
        emailContentSettingsPanel.setToolTipText("Text of the email to send.");

        emailContent.setColumns(20);
        emailContent.setRows(5);
        jScrollPane1.setViewportView(emailContent);

        javax.swing.GroupLayout Tab2Layout = new javax.swing.GroupLayout(Tab2);
        Tab2.setLayout(Tab2Layout);
        Tab2Layout.setHorizontalGroup(
                        Tab2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, Tab2Layout.createSequentialGroup()
                                        .addContainerGap()
                                        .addGroup(Tab2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                                        .addComponent(jScrollPane1)
                                                        .addComponent(emailContentSettingsPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 615, Short.MAX_VALUE))
                                        .addContainerGap())
        );
        Tab2Layout.setVerticalGroup(
                        Tab2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(Tab2Layout.createSequentialGroup()
                                        .addContainerGap()
                                        .addComponent(emailContentSettingsPanel)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 372, Short.MAX_VALUE)
                                        .addContainerGap())
        );

        settingsTabs.addTab("Email content", Tab2);

        saveAndCloseButton.setText("Save and Close");
        saveAndCloseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveAndCloseButtonActionPerformed(evt);
            }
        });

        saveButton.setText("Save");
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });

        closeButton.setText("Close");
        closeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                        .addContainerGap()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                                        .addComponent(settingsTabs)
                                                        .addGroup(layout.createSequentialGroup()
                                                                        .addGap(0, 0, Short.MAX_VALUE)
                                                                        .addComponent(closeButton)
                                                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                        .addComponent(saveAndCloseButton)
                                                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                        .addComponent(saveButton)))
                                        .addContainerGap())
        );
        layout.setVerticalGroup(
                        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                        .addGap(7, 7, 7)
                                        .addComponent(settingsTabs)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                        .addComponent(closeButton)
                                                        .addComponent(saveButton)
                                                        .addComponent(saveAndCloseButton))
                                        .addContainerGap())
        );

        pack();
    }// </editor-fold>                        

    private void saveAndCloseButtonActionPerformed(java.awt.event.ActionEvent evt) {
        saveSettings();
//      Popups.alertWindow("Your preferences are saved!");
        this.setVisible(false);
    }

    private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) {
        this.setVisible(false);
    }

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {
        saveSettings();
//      Popups.alertWindow("Your preferences are saved!");
    }

    public void deleteSMTP(int ID) {
        int smtp = findSMTPSettingsIndex(ID);

        if (smtp >= 0) {
            smtp_list.remove(smtp);
            lista.remove(smtp);
            lista.revalidate();
            lista.repaint();
        }
    }

    public SmtpSettings findSMTPSettings(int ID) {
        for (int i = 0; i < smtp_list.size(); i++) {
            if (smtp_list.get(i).ID == ID) {
                return smtp_list.get(i);
            }
        }

        return null;
    }

    public int findSMTPSettingsIndex(int ID) {
        for (int i = 0; i < smtp_list.size(); i++) {
            if (smtp_list.get(i).ID == ID) {
                return i;
            }
        }

        return -1;
    }

    private void addNewSmtpAccountActionPerformed(java.awt.event.ActionEvent evt) {

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridwidth = gbc.REMAINDER;
        gbc.fill = gbc.HORIZONTAL;
        gbc.weightx = 1;

        ID++;
        smtp_tmp = new SmtpSettings(this, ID);
        //smtp_tmp.setBounds(0, (ID * smtp_tmp.getSize().height)+5, 600, 100);
        lista.add(smtp_tmp, gbc);
        lista.revalidate();

        smtp_list.add(smtp_tmp);

    }

    private void saveSettings() {
        prefs.put(SETTINGSEMAILCONTENT, emailContent.getText());
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {

        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(settingsWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(settingsWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(settingsWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(settingsWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new settingsWindow().setVisible(true);
            }
        });
    }

    private javax.swing.JPanel Tab1;
    private javax.swing.JPanel Tab2;
    private javax.swing.JButton addNewSmtpAccount;
    private javax.swing.JButton closeButton;
    private javax.swing.JTextArea emailContent;
    private javax.swing.JLabel emailContentSettingsPanel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel lista;
    private javax.swing.JButton saveAndCloseButton;
    private javax.swing.JButton saveButton;
    private javax.swing.JTabbedPane settingsTabs;
    private javax.swing.JScrollPane smtpPanelList;
    private javax.swing.JPanel smtpSettingsHeader;

    public class SmtpSettings extends javax.swing.JPanel {

        public int ID = -1;
        private settingsWindow stw = null;

        public SmtpSettings(settingsWindow stw, int id) {
            initComponents();
            this.ID = id;
            this.stw = stw;
        }

        public void setSMTPServer(String srv) {
            this.smtpServer.setText(srv);
        }

        public String getSMTPServer() {
            return this.smtpServer.getText();
        }

        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {

            smtpServer = new javax.swing.JTextField();
            smtpPort = new javax.swing.JTextField();
            smtpSsl = new javax.swing.JCheckBox();
            smtpUsername = new javax.swing.JTextField();
            smtpPassword = new javax.swing.JPasswordField();
            deleteBtn = new javax.swing.JButton();

            setBorder(new javax.swing.border.LineBorder(new java.awt.Color(200, 200, 200), 1, true));
            setBounds(new java.awt.Rectangle(0, 0, 600, 100));
            setMaximumSize(new java.awt.Dimension(600, 100));
            setPreferredSize(new java.awt.Dimension(600, 100));

            smtpServer.setText("SMTP Server URL");
            smtpServer.setToolTipText("SMTP Server URL");

            smtpPort.setText("SMTP Port");
            smtpPort.setToolTipText("SMTP Port");
            smtpPort.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    smtpPortActionPerformed(evt);
                }
            });

            smtpSsl.setText("SSL");

            smtpUsername.setText("SMTP Username");
            smtpUsername.setToolTipText("SMTP Username");

            smtpPassword.setText("SMTP Password");
            smtpPassword.setToolTipText("SMTP Password");

            deleteBtn.setText("Delete SMTP Account");
            deleteBtn.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    deleteBtnActionPerformed(evt);
                }
            });

            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
            this.setLayout(layout);
            layout.setHorizontalGroup(
                            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                            .addContainerGap()
                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                                            .addGroup(layout.createSequentialGroup()
                                                                            .addComponent(smtpServer, javax.swing.GroupLayout.PREFERRED_SIZE, 227, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                            .addComponent(smtpPort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                            .addComponent(smtpSsl))
                                                            .addGroup(layout.createSequentialGroup()
                                                                            .addComponent(smtpUsername, javax.swing.GroupLayout.PREFERRED_SIZE, 176, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                            .addComponent(smtpPassword, javax.swing.GroupLayout.PREFERRED_SIZE, 186, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 37, Short.MAX_VALUE)
                                            .addComponent(deleteBtn))
            );
            layout.setVerticalGroup(
                            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                            .addGap(11, 11, 11)
                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                            .addComponent(smtpServer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                            .addComponent(smtpPort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                            .addComponent(smtpSsl))
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                            .addComponent(smtpUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                            .addComponent(smtpPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                            .addComponent(deleteBtn))
                                            .addContainerGap(17, Short.MAX_VALUE))
            );
        }// </editor-fold>                        

        private void deleteBtnActionPerformed(java.awt.event.ActionEvent evt) {

//          if (Popups.confirmWindow("Do you really want to delete?") == 0) {
//              stw.deleteSMTP(ID);
//          }
        }

        private void smtpPortActionPerformed(java.awt.event.ActionEvent evt) {

        }

        private javax.swing.JButton deleteBtn;
        private javax.swing.JPasswordField smtpPassword;
        private javax.swing.JTextField smtpPort;
        private javax.swing.JTextField smtpServer;
        private javax.swing.JCheckBox smtpSsl;
        private javax.swing.JTextField smtpUsername;
    }
}