2011-12-04 39 views
9

Tôi muốn JList có nhiều kết quả trong một JOptionPane, tuy nhiên, tôi không chắc chắn làm thế nào để thêm vào một chức năng di chuyển nên có quá nhiều kết quả. Làm cách nào để thêm thanh cuộn vào JOptionPane? Bất kỳ sự trợ giúp nào đều sẽ là tuyệt vời.JOptionPane và chức năng cuộn

Cảm ơn.

Trả lời

22

Dưới đây là một ví dụ sử dụng một JTextArea nhúng trong một JScrollPane:

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

hãy thay đổi điều đó thành JList :-) – mKorbel

+0

@AndrewThompson Đồng ý showMessageDialog phù hợp hơn với tình huống này. – GETah

+0

@mKorbel Có, một JList cũng nên làm điều đó. Ví dụ tôi cung cấp ở đây chỉ để được hướng dẫn. – GETah

4

Đặt các đối tượng trong một JList hoặc thành phần khác như vậy, thả nó vào một JScrollPane và đặt JScrollPane vào JOptionPane.

0

Tôi đã có một nhu cầu tương tự, một JOptionPane với một cuộn TextArea rằng bất kỳ lớp nào của tôi trong ứng dụng của tôi có thể ghi vào. Điều này nhằm cung cấp cho người dùng thông tin trạng thái và tiến độ. Cách tiếp cận của tôi là tạo một lớp tĩnh mà tôi đã khởi tạo một lần và bất kỳ lớp nào có thể gọi phương thức cập nhật của nó để ghi vào nó. Dưới đây là mã và một trình điều khiển nhỏ với hy vọng nó sẽ giúp ai đó tiết kiệm thời gian. Điều này có thể được thực hiện không tĩnh, mà chỉ phù hợp với nhu cầu của tôi.

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); 
    } 

}