简体   繁体   中英

Set a label on a panel after selecting a item in list

Why is the label not displaying?

package javaapplication4;


import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class settingPanel extends javax.swing.JPanel {
    static JPanel con = new JPanel();
    static JFrame frame = new JFrame();
    JLabel jLabel1 = new JLabel();
    public JEditorPane htmlvi = new JEditorPane();

    /** Creates new form settingPanel */
    public settingPanel() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();

        jList1.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4",
                    "Item 5" };

            public int getSize() {
                return strings.length;
            }

            public Object getElementAt(int i) {
                return strings[i];
            }
        });
        jList1.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jList1ValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(jList1);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(
                jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(jPanel1Layout.createParallelGroup(
                javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                jPanel1Layout
                        .createSequentialGroup()
                        .addComponent(jScrollPane1,
                                javax.swing.GroupLayout.PREFERRED_SIZE, 70,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(325, Short.MAX_VALUE)));
        jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(
                javax.swing.GroupLayout.Alignment.LEADING).addComponent(
                jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 272,
                Short.MAX_VALUE));

        jTabbedPane1.addTab("tab1", jPanel1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(
                javax.swing.GroupLayout.Alignment.LEADING).addComponent(
                jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400,
                Short.MAX_VALUE));
        layout.setVerticalGroup(layout.createParallelGroup(
                javax.swing.GroupLayout.Alignment.LEADING).addComponent(
                jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 300,
                Short.MAX_VALUE));
    }// </editor-fold>

    private void addpanel() {
        JPanel pnl = new JPanel();
        pnl.setOpaque(false);
        pnl.setLayout(new GridBagLayout());
        pnl.add(new JButton());
        pnl.setVisible(true);
        jPanel1.setLocation(10, 30);
        jPanel1.add(pnl);
    }

    private void jList1ValueChanged(javax.swing.event.ListSelectionEvent evt) {

        JLabel write = new JLabel();

        // if(evt.getValueIsAdjusting()){
        String read = "<html><h2>" + jList1.getSelectedValue().toString()
                + "<UL>USB";
        addpanel();

        jPanel1.add(write);

    }

    // Variables declaration - do not modify
    private javax.swing.JList jList1;
    public static javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTabbedPane jTabbedPane1;

    // End of variables declaration
    public static void main(String args[]) {

        frame.add(new settingPanel());
        // jPanel2.setLayout(new GridBagLayout());
        // jPanel2.add(new JButton());
        // jPanel2.setVisible(false);
        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(3);
    }
}

You add your JLabel to jPanel1, which uses a GroupLayout to display its components. The GroupLayout needs to know where each component must be placed in its vertical and horizontal group, and it doesn't know where to put your new label, since you didn't tell.

I very much doubt that your end goal is to add a new label to your GUI each time the selection changes. You should probably add a unique label with a default text when the GUI is initialized, and change the text of this label when the selection changes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM