2011-12-04 39 views
9

我想JList在JOptionPane中的很多結果,但是,我不知道如果添加滾動功能應該有太多的結果如何添加。我將如何添加滾動條到JOptionPane?任何幫助都會很棒。JOptionPane和滾動功能

謝謝。

回答

22

下面是使用嵌入在JScrollPane一個JTextArea一個例子:

JTextArea textArea = new JTextArea("Insert your Text here"); 
JScrollPane scrollPane = new JScrollPane(textArea); 
textArea.setLineWrap(true); 
textArea.setWrapStyleWord(true); 
scrollPane.setPreferredSize(new Dimension(500, 500)); 
JOptionPane.showMessageDialog(null, scrollPane, "dialog test with textarea", 
             JOptionPane.YES_NO_OPTION); 
+0

請把它改成JList :-) – mKorbel

+0

@AndrewThompson同意showMessageDialog更適合這種情況。 – GETah

+0

@mKorbel是的,JList也應該這樣做。我在這裏提供的示例僅供參考。 – GETah

4

將對象放入JList或其他此類組件中,將其放入JScrollPane中,並將JScrollPane放入JOptionPane中。

0

我也有類似的需要,具有滾動的JOptionPane TextArea表示我的應用程序中的任何類都可以寫入。這是爲了向用戶提供狀態和進度信息。 我的方法是使這個靜態類,我實例化一次,任何類可以調用其更新方法寫入它。下面是代碼和一個小驅動程序,希望能夠節省時間。這可能不是靜態的,只適合我的需求。

package com.acme.view; 

import java.awt.EventQueue; 
import javax.swing.JFrame; 
import javax.swing.JButton; 
import java.awt.BorderLayout; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 

import com.acme.controller.MyController; 
import com.acme.utils.NonModalMessage; 

public class MyView { 

    private JFrame frame; 
    private int dialogNum = 0; 
    private MyController myCntrlr; 
    /** 
    * Launch the application. 
    */ 
    public static void main(String[] args) { 
     NonModalMessage.createMesgDialog(); 
     NonModalMessage.updateMessage("Acme Anvil Targeting Progress"); 

     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        MyView window = new MyView(); 
        window.frame.setVisible(true); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    /** 
    * Create the application. 
    */ 
    public MyView() { 
     initialize(); 
    } 

    /** 
    * Initialize the contents of the frame. 
    */ 
    private void initialize() { 
     frame = new JFrame(); 
     frame.setBounds(100, 100, 250, 200); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     myCntrlr = new MyController(); 

     JButton btnMybutton = new JButton("myButton"); 

     btnMybutton.addMouseListener(new MouseAdapter() { 
      @Override 
      public void mouseClicked(MouseEvent e) { 
       NonModalMessage.setMessageVisible(); 
       if(dialogNum > 0 && dialogNum % 10 == 0){ 
        NonModalMessage.clearMessage(); 
        NonModalMessage.updateMessage("Acme Anvil Targeting Progress"); 
        myCntrlr.someMethod("Controller reports Roadrunner sighted. Message Number: ", dialogNum); 
       } 
       NonModalMessage.getMessage(); 

       NonModalMessage.updateMessage("Message number: " + Integer.toString(dialogNum)); 
       System.out.println("dialogNum: " + dialogNum); 
       dialogNum++; 
      } 
     }); 

     frame.getContentPane().add(btnMybutton, BorderLayout.NORTH); 
    } 


} 

package com.acme.controller; 
import com.acme.utils.NonModalMessage; 

public class MyController { 

    public MyController(){ 

    } 

    public void someMethod(String mystring, int myInt){ 
     NonModalMessage.updateMessage(mystring + " "+ myInt); 
    } 

} 

package com.acme.utils; 
import javax.swing.JDialog; 
import javax.swing.JOptionPane; 
import javax.swing.JScrollPane; 
import javax.swing.JTextArea; 
import javax.swing.ScrollPaneConstants; 

public class NonModalMessage { 
    private static JTextArea textArea = null; 
    private static JOptionPane oPane = null; 
    private static JDialog dialog  = null; 
    private static JScrollPane myjsPane = null; 
    public NonModalMessage(){} 

    public static void createMesgDialog(){ 

     textArea = new JTextArea(); 
     textArea.setLineWrap(true); 
     textArea.setWrapStyleWord(true); 
     myjsPane = new JScrollPane(textArea); 
     myjsPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); 

     oPane = new JOptionPane(); 
     oPane.add(myjsPane);  

     //final JDialog dialog = pane.createDialog(myPane, "Progress Dialog"); 
     dialog = oPane.createDialog(null, ""); 
     dialog.setTitle("Progress Messages"); 
     dialog.setModal(false); 
     dialog.setSize(400, 250); 
     dialog.setResizable(true); 
     dialog.setAlwaysOnTop(true); 
    } 

    public static void setMessageVisible(){ 
     dialog.setVisible(true); 
    } 

    public static void updateMessage(String newMessage){ 
     String mytext = textArea.getText(); 
     if(mytext.isEmpty()){ 
      textArea.setText(newMessage); 
     } 
     else{ 
      textArea.setText(mytext + "\n" + newMessage); 
     } 

     oPane.setMessage(myjsPane); 
    } 
    public static String getMessage(){ 
     return textArea.getText(); 
    } 

    public static void clearMessage(){ 
     textArea.setText(""); 
     oPane.setMessage(myjsPane); 
    } 

}