2012-04-21 65 views
3

因此,我的文本比他们占用的某些组件长得多。我已经为大多数问题提出了合理的解决方案,但我不知道如何处理组合框中很长的文本项目。如果用户看不到文字,他不能做出正确的决定 - 特别是如果文字的第一部分匹配。请参阅下面的我的SSCCE如何处理组合框中非常长的文本项目

我已经使用JScrollpane来解决JTextfield中长文本的问题,并且我使用了工具提示来解析JXhyperlink字段中的长文本。另外,我还为组合框提供了一个工具提示,可以在选择后显示一个长条目,但理想情况下,我想在选择前给用户提供该选项。此代码示例使用Netbeans的GUI设计器(非常适合RAD用途),布局是GUI设计器使用的GroupLayout。

package longtextsexample; 

import javax.swing.JFileChooser; 

public class MainFrame extends javax.swing.JFrame { 

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

    /** 
    * This method is called from within the constructor to initialize the form. 
    */ 
    @SuppressWarnings("unchecked") 
    private void initComponents() { 

     jScrollPane1 = new javax.swing.JScrollPane(); 
     jTextField1 = new javax.swing.JTextField(); 
     jComboBox1 = new javax.swing.JComboBox(); 
     jXHyperlink1 = new org.jdesktop.swingx.JXHyperlink(); 
     jButton1 = new javax.swing.JButton(); 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

     jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS); 
     jScrollPane1.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER); 
     jScrollPane1.setViewportView(jTextField1); 

     jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 222222222222222222222222", "Item 333333333333333333333333", "Item 333333333333333333333333444444444444" })); 
     jComboBox1.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       jComboBox1ActionPerformed(evt); 
      } 
     }); 

     jXHyperlink1.setText("Path"); 

     jButton1.setText("Browse"); 
     jButton1.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       jButton1ActionPerformed(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() 
       .addGap(10, 10, 10) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE) 
        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE) 
        .addGroup(layout.createSequentialGroup() 
         .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE) 
         .addGap(6, 6, 6) 
         .addComponent(jXHyperlink1, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)))) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addGap(11, 11, 11) 
       .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addGap(18, 18, 18) 
       .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addGap(18, 18, 18) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
        .addComponent(jButton1) 
        .addGroup(layout.createSequentialGroup() 
         .addGap(4, 4, 4) 
         .addComponent(jXHyperlink1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))) 
     ); 

     pack(); 
    } 

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { 
     JFileChooser jfc = new JFileChooser(); 
     String path; 

     if (jfc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) { 
      path = jfc.getSelectedFile().getAbsolutePath(); 
     } else { 
      path = ""; 
     } 
     jXHyperlink1.setText(path); 
     jXHyperlink1.setToolTipText(path); 
    } 

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) { 
     jComboBox1.setToolTipText(jComboBox1.getSelectedItem().toString()); 
    } 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String args[]) { 
     /* 
     * Set the Nimbus look and feel 
     */ 

     /* 
     * If Nimbus (introduced in Java SE 6) is not available, stay with the 
     * default look and feel. For details see 
     * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */ 
     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(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (InstantiationException ex) { 
      java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (IllegalAccessException ex) { 
      java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
      java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } 


     /* 
     * Create and display the form 
     */ 
     java.awt.EventQueue.invokeLater(new Runnable() { 

      public void run() { 
       new MainFrame().setVisible(true); 
      } 
     }); 
    } 
    // Variables declaration 
    private javax.swing.JButton jButton1; 
    private javax.swing.JComboBox jComboBox1; 
    private javax.swing.JScrollPane jScrollPane1; 
    private javax.swing.JTextField jTextField1; 
    private org.jdesktop.swingx.JXHyperlink jXHyperlink1; 
    // End of variables declaration 
} 

回答

6
How to deal with very long text items in comboboxes 

1)你可以把JTextArea或更好的可能是JTextPane到派生JListJComboBox

无论如何上述观点可能是简单的,但为什么这样做,因为有

2)Combo Box Popup,我会建议设置setMaximumWidth

+0

感谢你的 - 我想tha吨将工作得很好。 – wmdvanzyl 2012-04-21 14:21:14

+0

你是对的,没有更好的周围:-) – mKorbel 2012-04-21 19:56:41

+0

我将标记为答案,只要我得到它的工作,但我upvoted已经。 – wmdvanzyl 2012-04-22 09:22:31