2011-11-08 14 views
1

ツールテイクを作成していますが、テキストファイルにタスクを実行しています。タスクの実行には時間がかかりますので、ファイル名と進行状況をパーセンテージで表示するパネルを作成しました。ユーザーは1つまたは複数のファイルでタスクを実行できるため、各ファイルのパネルを表示する必要があります。
私はここにいるので、テキスト領域を表示してパネルを追加するこのコードを持っています。問題は、新しい項目が追加されたときに、テキストエリアとパネルリストの両方が互いに犠牲を払って成長することです。行を追加したり、新しいボタンをクリックし、パネルを追加するときには、この出来事を見ることができます:JSplitPaneにアイテムが表示されない

import java.awt.event.*; 
import javax.swing.*; 
import java.util.*; 
import java.util.logging.Logger; 

public class FProgressDisplay extends JFrame { 
    private final static Logger LOGGER = Logger.getLogger(FProgressDisplay.class.getName()); 
    private List<PanelTaskProgress> tasks; 
    JTextArea txtLog; 
    JButton btnNew; 
    JButton btnAbort; 
    JButton btnClose; 
    static int i; 
    JPanel taskPanel; 

    public static void main(String[] args) { 
     try { 
      FProgressDisplay frame = new FProgressDisplay(); 
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      frame.setVisible(true); 

     } catch (Exception e) { 
      e.printStackTrace(); 
      throw new RuntimeException("Failed to initialize application."); 
     } 
    } 
    /** 
    * Create the frame. 
    */ 
    public FProgressDisplay() { 
     setTitle("Mask tool - Progress"); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     // should be done AFTER components are added 
     //pack(); 
     getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS)); 
     taskPanel = new JPanel(); 
     taskPanel.setLayout(new BoxLayout(taskPanel, BoxLayout.Y_AXIS)); 

     JPanel panel = new JPanel(); 
     getContentPane().add(panel); 

     btnNew = new JButton("New"); 
     panel.add(btnNew); 
     btnNew.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent ae) { 
       addTask(++i, "Task " + i); 
      } 
     }); 

     btnAbort = new JButton("Abort"); 
     panel.add(btnAbort); 

     btnClose = new JButton("Close"); 
     panel.add(btnClose); 

     txtLog = new JTextArea(10,0); 
     txtLog.setLineWrap(true); 
     getContentPane().add(txtLog); 

     tasks = new ArrayList<PanelTaskProgress>(); 

     JScrollPane scrollPane = new JScrollPane(taskPanel); 
     getContentPane().add(scrollPane); 

     for(i = 0; i < 10; i++) { 
      addTask(i, "Task"+i); 
     } 
     pack(); 
    } 

    public void addTask(long id, String fileName) { 
     PanelTaskProgress newTaskPanel = new PanelTaskProgress(id, fileName); 
     tasks.add(newTaskPanel); 
     taskPanel.add(newTaskPanel); 
     validate(); 
     //repaint(); 
     LOGGER.info("Added new panel"); 
    } 

    public class PanelTaskProgress extends JPanel { 
     private static final long serialVersionUID = 1L; 
     JLabel lblTaskDescription; 
     JLabel lblProgress; 
     private long id; 
     /** 
     * Create the panel. 
     */ 
     public PanelTaskProgress(long id, String fileName) { 
      try { 
       //setLayout(null); 

       lblTaskDescription = new JLabel(id + " " + fileName); 
       //lblTaskDescription.setPreferredSize(new Dimension(632, 14)); 
       add(lblTaskDescription); 

       lblProgress = new JLabel("0%"); 
       lblProgress.setHorizontalAlignment(SwingConstants.CENTER); 
       //lblProgress.setBounds(664, 11, 51, 14); 
       add(lblProgress); 

       LOGGER.info("Created new panel; Id: " + id + "; File: " + fileName); 
      } catch (Exception e) { 
       LOGGER.severe("Error creating new panel; " + e.getMessage()); 
      } 
     } 
    } 
} 

私は、それぞれが独自の領域に残っているし、必要に応じてスクロールを追加します。上記の例にJSplitPaneを追加しようとしましたが、両方のペインは空のままです。ここで

import java.awt.event.*; 
import javax.swing.*; 
import javax.swing.plaf.SplitPaneUI; 

import java.util.*; 
import java.util.logging.Logger; 

public class FProgressDisplay extends JFrame { 
    private final static Logger LOGGER = Logger 
      .getLogger(FProgressDisplay.class.getName()); 
    private List<PanelTaskProgress> tasks; 
    JTextArea txtLog; 
    JButton btnNew; 
    JButton btnAbort; 
    JButton btnClose; 
    static int i; 
    JPanel taskPanel; 

    private JSplitPane splitPane; 

    public static void main(String[] args) { 
     try { 
      FProgressDisplay frame = new FProgressDisplay(); 
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      frame.setVisible(true); 

     } catch (Exception e) { 
      e.printStackTrace(); 
      throw new RuntimeException("Failed to initialize application."); 
     } 
    } 

    /** 
    * Create the frame. 
    */ 
    public FProgressDisplay() { 
     setTitle("Mask tool - Progress"); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     // should be done AFTER components are added 
     // pack(); 
     getContentPane().setLayout(
       new BoxLayout(getContentPane(), BoxLayout.Y_AXIS)); 
     taskPanel = new JPanel(); 
     taskPanel.setLayout(new BoxLayout(taskPanel, BoxLayout.Y_AXIS)); 

     JPanel panel = new JPanel(); 
     getContentPane().add(panel); 

     btnNew = new JButton("New"); 
     panel.add(btnNew); 
     btnNew.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent ae) { 
       addTask(++i, "Task " + i); 
      } 
     }); 

     btnAbort = new JButton("Abort"); 
     panel.add(btnAbort); 

     btnClose = new JButton("Close"); 
     panel.add(btnClose); 

     txtLog = new JTextArea(10, 0); 
     txtLog.setLineWrap(true); 
     //getContentPane().add(txtLog); 

     tasks = new ArrayList<PanelTaskProgress>(); 

     JScrollPane scrollPane = new JScrollPane(taskPanel); 
     //getContentPane().add(scrollPane); 
     splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, txtLog, scrollPane); 
     splitPane.setDividerLocation(150); 

     for (i = 0; i < 10; i++) { 
      addTask(i, "Task" + i); 
     } 
     pack(); 
    } 

    public void addTask(long id, String fileName) { 
     PanelTaskProgress newTaskPanel = new PanelTaskProgress(id, fileName); 
     tasks.add(newTaskPanel); 
     taskPanel.add(newTaskPanel); 
     validate(); 
     // repaint(); 
     LOGGER.info("Added new panel"); 
    } 

    public class PanelTaskProgress extends JPanel { 
     private static final long serialVersionUID = 1L; 
     JLabel lblTaskDescription; 
     JLabel lblProgress; 
     private long id; 

     /** 
     * Create the panel. 
     */ 
     public PanelTaskProgress(long id, String fileName) { 
      try { 
       // setLayout(null); 

       lblTaskDescription = new JLabel(id + " " + fileName); 
       // lblTaskDescription.setPreferredSize(new Dimension(632, 14)); 
       add(lblTaskDescription); 

       lblProgress = new JLabel("0%"); 
       lblProgress.setHorizontalAlignment(SwingConstants.CENTER); 
       // lblProgress.setBounds(664, 11, 51, 14); 
       add(lblProgress); 

       LOGGER.info("Created new panel; Id: " + id + "; File: " 
         + fileName); 
      } catch (Exception e) { 
       LOGGER.severe("Error creating new panel; " + e.getMessage()); 
      } 
     } 
    } 
} 

は、ソリューションです:

package layout.sscce; 

import java.awt.BorderLayout; 
import java.awt.event.*; 
import javax.swing.*; 
import java.util.*; 
import java.util.logging.Logger; 

public class FProgressDisplay extends JFrame { 
    private final static Logger LOGGER = Logger 
      .getLogger(FProgressDisplay.class.getName()); 
    private List<PanelTaskProgress> tasks; 
    JTextArea txtLog; 
    JButton btnNew; 
    JButton btnAbort; 
    JButton btnClose; 
    static int i; 
    JPanel taskPanel; 

    private JSplitPane splitPane; 

    public static void main(String[] args) { 
     try { 
      FProgressDisplay frame = new FProgressDisplay(); 
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      frame.setVisible(true); 

     } catch (Exception e) { 
      e.printStackTrace(); 
      throw new RuntimeException("Failed to initialize application."); 
     } 
    } 

    /** 
    * Create the frame. 
    */ 
    public FProgressDisplay() { 
     setTitle("Mask tool - Progress"); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     // should be done AFTER components are added 
     // pack(); 
//  getContentPane().setLayout(
//    new BoxLayout(getContentPane(), BoxLayout.Y_AXIS)); 
     getContentPane().setLayout(
       new BorderLayout()); 

     taskPanel = new JPanel(); 
     taskPanel.setLayout(new BoxLayout(taskPanel, BoxLayout.Y_AXIS)); 

     JPanel buttonPanel = new JPanel(); 
     getContentPane().add(buttonPanel); 

     btnNew = new JButton("New"); 
     buttonPanel.add(btnNew); 
     btnNew.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent ae) { 
       addTask(++i, "Task " + i); 
      } 
     }); 

     btnAbort = new JButton("Abort"); 
     buttonPanel.add(btnAbort); 

     btnClose = new JButton("Close"); 
     buttonPanel.add(btnClose); 

     txtLog = new JTextArea(10, 30); 
     txtLog.setLineWrap(true); 
     //getContentPane().add(txtLog); 

     tasks = new ArrayList<PanelTaskProgress>(); 

     JScrollPane taskScrollPane = new JScrollPane(taskPanel); 
     JScrollPane textScrollPane = new JScrollPane(txtLog); 

     splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, textScrollPane, taskScrollPane); 
     splitPane.setDividerLocation(150); 

     for (i = 0; i < 10; i++) { 
      addTask(i, "Task" + i); 
     } 



     getContentPane().add(buttonPanel, BorderLayout.NORTH); 
     getContentPane().add(splitPane, BorderLayout.CENTER); 

     pack(); 
    } 

    public void addTask(long id, String fileName) { 
     PanelTaskProgress newTaskPanel = new PanelTaskProgress(id, fileName); 
     tasks.add(newTaskPanel); 
     taskPanel.add(newTaskPanel); 
     validate(); 
     // repaint(); 
     LOGGER.info("Added new panel"); 
    } 

    public class PanelTaskProgress extends JPanel { 
     private static final long serialVersionUID = 1L; 
     JLabel lblTaskDescription; 
     JLabel lblProgress; 
     private long id; 

     /** 
     * Create the panel. 
     */ 
     public PanelTaskProgress(long id, String fileName) { 
      try { 
       // setLayout(null); 

       lblTaskDescription = new JLabel(id + " " + fileName); 
       // lblTaskDescription.setPreferredSize(new Dimension(632, 14)); 
       add(lblTaskDescription); 

       lblProgress = new JLabel("0%"); 
       lblProgress.setHorizontalAlignment(SwingConstants.CENTER); 
       // lblProgress.setBounds(664, 11, 51, 14); 
       add(lblProgress); 

       LOGGER.info("Created new panel; Id: " + id + "; File: " 
         + fileName); 
      } catch (Exception e) { 
       LOGGER.severe("Error creating new panel; " + e.getMessage()); 
      } 
     } 
    } 
} 

答えて

2

問題がBoxLayoutです。コンポーネント間のスペースを割り当てようとすると、それは厄介なことになります。たぶんBorderLayoutが良いでしょう。 NORTHにボタンを追加し、CENTERにscrollPaneを追加します。

それとも、次の操作を行うことができ、あなたのコードを使用して:

 txtLog = new JTextArea(10, 30); // changed 
     txtLog.setLineWrap(true); 
     getContentPane().add(txtLog); 

     tasks = new ArrayList<PanelTaskProgress>(); 

     JScrollPane scrollPane = new JScrollPane(taskPanel); 
     scrollPane.setPreferredSize(txtLog.getPreferredSize()); // added 
+0

感謝。私は1つ小さな追加しました。 textAreaは、scrollPaneの内部にも配置されます。 – Yoav

関連する問題