将JPanels添加到JScrollPane

时间:2015-06-18 12:35:03

标签: java swing

我正在尝试在JPanel上显示多个较小的JScrollPane。 为实现此目的,我目前将它们添加到另一个JPanel并将此面板设置为scrollPane的ViewportView。

有没有办法将面板直接添加到滚动窗格?

什么不起作用是这样的:

JScrollPane scrollPane = new JScrollPane();
scrollPane.setPreferredSize(new Dimension(480, 480));
scrollPane.setSize(new Dimension(480, 480));
scrollPane.setMinimumSize(new Dimension(480, 40));
scrollPane.setViewportBorder(null);
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

System.out.println("start");
for (int i=0; i<5;i++)
{
    SingleClientPanel x = new SingleClientPanel();
    x.setLocation(0, 45 *i);
    scrollPane.getViewport().add(x);
}

2 个答案:

答案 0 :(得分:5)

  

为实现这一目标,我目前将它们添加到另一个JPanel并将此面板设置为scrollPane的视口。

不完全。您不会将容器JPanel设为视口,而是视口的视图。视口本身是一个非常专业的容器,它有自己的布局管理器,如果您只是用JPanel替换它,这将会搞砸。

即,

JViewport viewport = myScrollPane.getViewport();
viewport.setView(myContainerJPanel);

或更简洁

myScrollPane.setViewportView(myContainerJPanel);

请注意,这让我担心:x.setLocation(0, 45 *i);并建议在某处使用null布局。无论你做什么,都不要这样做,不要使用null布局,特别是在JScrollPanes中,因为它会把它全部搞砸。

有关更详细的帮助,请考虑创建并发布sscceminimal example program/mcve,其中您将代码压缩到仍然编译和运行的最小位,没有外部依赖(例如需要链接)对于数据库或图像),没有额外的代码与您的问题无关,但仍然可以证明您的问题。另外,请考虑发布所需输出的图像。

例如:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.GridLayout;

import javax.swing.*;

public class ScrollPaneEg extends JPanel {
   private static final int PREF_W = 480;
   private static final int PREF_H = PREF_W;

   public ScrollPaneEg() {
      JScrollPane scrollPane = new JScrollPane();
      scrollPane.setViewportBorder(null);
      scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

      JPanel container = new JPanel(new GridLayout(0, 1)); // 1 column variable
                                                           // number of rows
      for (int i = 0; i < 15; i++) {
         SingleClientPanel x = new SingleClientPanel(String.valueOf(i + 1));
         // x.setLocation(0, 45 *i);
         container.add(x);
      }
      scrollPane.setViewportView(container);

      setLayout(new BorderLayout());
      add(scrollPane, BorderLayout.CENTER);
   }

   @Override
   public Dimension getPreferredSize() {
      if (isPreferredSizeSet()) {
         return super.getPreferredSize();
      }
      return new Dimension(PREF_W, PREF_H);
   }

   private static void createAndShowGui() {
     ScrollPaneEg mainPanel = new ScrollPaneEg();

      JFrame frame = new JFrame("ScrollPaneEg");
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

class SingleClientPanel extends JPanel {
   private static final int PREF_H = 60;

   public SingleClientPanel(String text) {
      setBorder(BorderFactory.createTitledBorder("Single Client"));
      setLayout(new GridBagLayout());
      add(new JLabel("Panel: " + text, SwingConstants.CENTER));
   }

   @Override
   public Dimension getPreferredSize() {
      Dimension superSz = super.getPreferredSize();
      if (isPreferredSizeSet()) {
         return superSz;
      }
      int prefH = Math.max(superSz.height, PREF_H);
      return new Dimension(superSz.width, prefH);
   }
}

另外,请考虑使用JTable来显示表格数据。例如,......

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Window;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.table.AbstractTableModel;

public class ClientOverviewTest {
   private static void createAndShowGui() {
      ClientOverviewPanel2 mainPanel = new ClientOverviewPanel2();

      JFrame frame = new JFrame("ClientOverviewPanel");
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }

}

class ClientOverviewPanel2 extends JPanel {
   private static final int CLIENTS = 5;
   private static final int PREF_W = 600;
   private static final int PREF_H = 200;
   private ClientTableModel model = new ClientTableModel();
   private JTable table = new JTable(model);

   public ClientOverviewPanel2() {
      for (int i = 0; i < CLIENTS; i++) {
         String ip = "127.000.000.001";
         UUID uuid = UUID.randomUUID();
         boolean isLocal = true;
         SingleClient client = new SingleClient(ip, uuid, isLocal);
         model.addRow(client);
      }

      table.getColumnModel().getColumn(1).setPreferredWidth(150); //!!
      table.setPreferredScrollableViewportSize(table.getPreferredSize());

      JScrollPane scrollPane = new JScrollPane(table);
      scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

      setLayout(new BorderLayout());
      add(scrollPane, BorderLayout.CENTER);
      add(new JButton(new OkAction("OK")), BorderLayout.PAGE_END);

   }

   @Override
   public Dimension getPreferredSize() {
      Dimension superSz = super.getPreferredSize();
      if (isPreferredSizeSet()) {
         return superSz;
      }
      int prefW = PREF_W;
      int prefH = Math.min(superSz.height, PREF_H);
      return new Dimension(prefW, prefH);
   }


   private class OkAction extends AbstractAction {
      public OkAction(String name) {
         super(name);
         int mnemonic = (int) name.charAt(0);
         putValue(MNEMONIC_KEY, mnemonic);
      }

      @Override
      public void actionPerformed(ActionEvent e) {
         Component source = (Component) e.getSource();
         Window window = SwingUtilities.getWindowAncestor(source);
         if (window != null) {
            window.dispose();
         }
      }
   }
}

class ClientTableModel extends AbstractTableModel {
   public final static String[] COLUMNS = { "IP", "UUID", "Local" };
   private List<SingleClient> clientList = new ArrayList<>();

   @Override
   public int getColumnCount() {
      return COLUMNS.length;
   }

   @Override
   public int getRowCount() {
      return clientList.size();
   }

   @Override
   public String getColumnName(int column) {
      return COLUMNS[column];
   }

   public void addRow(SingleClient client) {
      clientList.add(client);
      int index = clientList.size() - 1;
      fireTableRowsInserted(index, index);
   }

   @Override
   public Object getValueAt(int rowIndex, int columnIndex) {
      if (rowIndex >= getRowCount() || rowIndex < 0) {
         String text = "for rowIndex: " + rowIndex;
         throw new IllegalArgumentException(text);
      }
      if (columnIndex < 0 || columnIndex >= COLUMNS.length) {
         String text = "for columnIndex: " + columnIndex;
         throw new IllegalArgumentException(text);
      }
      SingleClient client = clientList.get(rowIndex);
      switch (columnIndex) {
      case 0:
         return client.getIp();
      case 1:
         return client.getUuid();
      case 2:
         return client.isLocal();
      }
      return null;
   }

   @Override
   public Class<?> getColumnClass(int columnIndex) {
      if (columnIndex < 0 || columnIndex >= COLUMNS.length) {
         String text = "for columnIndex: " + columnIndex;
         throw new IllegalArgumentException(text);
      }

      switch (columnIndex) {
      case 0:
         return String.class;
      case 1: 
         return UUID.class;
      case 2:
         return Boolean.class;
      }

      // default value
      return super.getColumnClass(columnIndex);
   }

   @Override
   public boolean isCellEditable(int rowIndex, int columnIndex) {
      return columnIndex == 2;
   }

   @Override
   public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
      SingleClient client = clientList.get(rowIndex);
      switch (columnIndex) {
      case 0:
         break;
      case 1:
         break;
      case 2:
         boolean isLocal = (boolean) aValue; 
         client.setLocal(isLocal);

      default:
         break;
      }
   }
}

class SingleClient {
   private String ip;
   private UUID uuid;
   private boolean isLocal;

   public SingleClient(String ip, UUID uuid2, boolean isLocal) {
      this.ip = ip;
      this.uuid = uuid2;
      this.isLocal = isLocal;
   }

   public String getIp() {
      return ip;
   }

   public void setIp(String ip) {
      this.ip = ip;
   }

   public UUID getUuid() {
      return uuid;
   }

   public void setUuid(UUID uuid) {
      this.uuid = uuid;
   }

   public boolean isLocal() {
      return isLocal;
   }

   public void setLocal(boolean isLocal) {
      this.isLocal = isLocal;
   }

   @Override
   public int hashCode() {
      final int prime = 31;
      int result = 1;
      result = prime * result + ((ip == null) ? 0 : ip.hashCode());
      result = prime * result + ((uuid == null) ? 0 : uuid.hashCode());
      return result;
   }

   @Override
   public boolean equals(Object obj) {
      if (this == obj)
         return true;
      if (obj == null)
         return false;
      if (getClass() != obj.getClass())
         return false;
      SingleClient other = (SingleClient) obj;
      if (ip == null) {
         if (other.ip != null)
            return false;
      } else if (!ip.equals(other.ip))
         return false;
      if (uuid == null) {
         if (other.uuid != null)
            return false;
      } else if (!uuid.equals(other.uuid))
         return false;
      return true;
   }

}

答案 1 :(得分:0)

感谢帮助,我能够让它发挥作用。我将添加我的解决方案,包括更改以供参考和进一步评论:

public class SingleClientPanel extends JPanel
{
    private JTextField ipTextfield;
    private JTextField uuidTextField;
    public SingleClientPanel()
    {
        this("127.000.000.001",UUID.randomUUID().toString(),true);
    }
    public SingleClientPanel(String ip, String uuid,boolean isLocal) 
    {
        /*
        removed: 
        setSize(new Dimension(440, 35));
        setPreferredSize(new Dimension(440, 35));
        setMaximumSize(new Dimension(440, 35));
        setMinimumSize(new Dimension(440, 35));*/
        setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
        setLayout(new BoxLayout(this, BoxLayout.X_AXIS));       // added this

        ipTextfield = new JTextField();
        ipTextfield.setHorizontalAlignment(SwingConstants.CENTER);
        ipTextfield.setAlignmentX(Component.LEFT_ALIGNMENT);
        ipTextfield.setFocusable(false);
        ipTextfield.setEditable(false);
        add(ipTextfield);
        ipTextfield.setColumns(15);
        ipTextfield.setText(ip);

        uuidTextField = new JTextField();
        uuidTextField.setHorizontalAlignment(SwingConstants.CENTER);
        uuidTextField.setEditable(false);
        uuidTextField.setFocusable(false);
        add(uuidTextField);
        uuidTextField.setColumns(30);
        uuidTextField.setText(uuid);

        JButton button = new JButton(">");
        button.setEnabled(!isLocal);
        add(button);
        this.revalidate();
    }
}

public class ClientOverviewPanel extends JPanel
{
    public ClientOverviewPanel()
    {
        setLayout(new BorderLayout(0, 0));

        JButton btnOk = new JButton("Ok");
        btnOk.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent arg0)
            {
                 Window x = SwingUtilities.getWindowAncestor(ClientOverviewPanel.this);
                 if(x != null) x.dispose();
            }
        });
        add(btnOk, BorderLayout.SOUTH);

        JScrollPane scrollPane = new JScrollPane();
        /*
        removed: 
        scrollPane.setPreferredSize(new Dimension(480, 480));
        scrollPane.setSize(new Dimension(480, 480));
        scrollPane.setMinimumSize(new Dimension(480, 40));*/
        scrollPane.setViewportBorder(null);
        scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        Box box = new Box(BoxLayout.PAGE_AXIS);  //added

        for (int i=0; i<5;i++)
        {
            SingleClientPanel cpan = new SingleClientPanel();
            //cpan.setLocation(0, 45 *i);  removed  
            box.add(cpan);      //changed
        }

        scrollPane.setViewportView(box);    //changed
        add(scrollPane, BorderLayout.CENTER);
        this.revalidate();
    }
}