BorderLayout隐藏了JPanel

时间:2012-07-26 04:05:58

标签: java swing layout netbeans border-layout

我正在使用边框布局,它似乎隐藏了一个持有JTextPane的JScrollPane,它位于Center中。如何强制中心JScrollPane显示?

package net.bounceme.dur.usenet.swing;

import java.util.Observable;
import java.util.Observer;
import java.util.logging.Logger;
import javax.mail.Folder;
import javax.swing.ListModel;
import net.bounceme.dur.usenet.controller.Controller;
import net.bounceme.dur.usenet.controller.MessageBean;
import net.bounceme.dur.usenet.controller.MessagesDefaultListModel;

public class MessageSelect extends javax.swing.JPanel implements Observer {

    private static final Logger LOG = Logger.getLogger(MessageSelect.class.getName());
    private Controller controller = Controller.getInstance();
    private ListModel messages = new MessagesDefaultListModel();
    private MessageBean messageBean = new MessageBean();

    public MessageSelect() {
        controller.addObserver(this);
        initComponents();
        messageContent.setText("hello world");
    }

    /**
     * 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() {

        center = new javax.swing.JScrollPane();
        messageContent = new javax.swing.JTextPane();
        south = new javax.swing.JPanel();
        commentJButton = new javax.swing.JButton();
        west = new javax.swing.JScrollPane();
        messagesJList = new javax.swing.JList();

        setLayout(new java.awt.BorderLayout());

        messageContent.setContentType("text/html"); // NOI18N
        center.setViewportView(messageContent);

        add(center, java.awt.BorderLayout.CENTER);

        south.setLayout(new java.awt.BorderLayout());

        commentJButton.setText("comment");
        commentJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                commentJButtonActionPerformed(evt);
            }
        });
        south.add(commentJButton, java.awt.BorderLayout.CENTER);

        add(south, java.awt.BorderLayout.SOUTH);

        messagesJList.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]; }
        });
        messagesJList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                messagesJListMouseReleased(evt);
            }
        });
        messagesJList.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                messagesJListKeyReleased(evt);
            }
        });
        west.setViewportView(messagesJList);

        add(west, java.awt.BorderLayout.WEST);
    }// </editor-fold>

    private void commentJButtonActionPerformed(java.awt.event.ActionEvent evt) {                                               
        LOG.fine("button clicked" + messageBean);
    }                                              

    private void messagesJListKeyReleased(java.awt.event.KeyEvent evt) {                                          
        userSelectedRow();
    }                                         

    private void messagesJListMouseReleased(java.awt.event.MouseEvent evt) {                                            
        userSelectedRow();
    }                                           
    // Variables declaration - do not modify
    private javax.swing.JScrollPane center;
    private javax.swing.JButton commentJButton;
    private javax.swing.JTextPane messageContent;
    private javax.swing.JList messagesJList;
    private javax.swing.JPanel south;
    private javax.swing.JScrollPane west;
    // End of variables declaration

    @Override
    @SuppressWarnings("unchecked")
    public void update(Observable o, Object arg) {
        LOG.fine("trying folder:  " + arg);
        //Folder folder = null;
        try {
            Folder folder = (Folder) arg;
            messages = new MessagesDefaultListModel(folder);
            LOG.fine("how many messages? " + messages.getSize());
            messagesJList.setModel(messages);
            LOG.fine("loaded messages..");
        } catch (Exception e) {  //err, class cast exception?
            LOG.fine("not a valid folder " + arg);
        }
    }

    private void userSelectedRow() {
        messageBean = (MessageBean) messagesJList.getSelectedValue();
        LOG.fine(messageBean.toString());
        messageContent.setText(messageBean.getContent());
        LOG.fine("..gotMessageBean: " + messageBean);
    }
}

下面的第一个标签允许选择USENET组,某些组的行为比其他组更好。 android组的独特之处在于常规大小的窗口不显示CENTER JScrollPane:

enter image description here

但是最大化窗口允许CENTER JSCrollPane正确显示:

enter image description here

1 个答案:

答案 0 :(得分:2)

我认为JList正在调整最长行。试试

messageList.setPrototypeCellValue("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");

确定宽度。您还可以尝试为保留JScrollPane的{​​{1}}设置首选宽度并启用水平滚动。