JTable不完全可见

时间:2013-07-26 19:28:30

标签: java swing jtable jsplitpane

我正在向JPanel添加两个JSplitPane个对象(面板包含表格),由于某种原因,该表格不完全可见。我想添加一个屏幕截图供参考,但堆栈溢出不允许我这样做:(。无论如何,与此相关的代码如下:请提出一些解决方案。

 frame.setTitle(PAGE2);
          final String[] stream_column_names = { "IP Address", "Port", "IP Address", "Port", "Transport" };
          final String[] packet_column_names = { "#", "Direction", "Preview" };
          final JScrollPane pane1 = new JScrollPane();
          final JScrollPane pane2 = new JScrollPane();
          final JLabel label1 = new JLabel();
          final JLabel label2 = new JLabel();

          splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, streamPanel, packetPanel);
          splitPane.setOneTouchExpandable(true);
          splitPane.setDividerLocation(350);


          streamPanel = new JPanel();
          streamPanel.setLayout(new BorderLayout());

          pane1.setLayout(new ScrollPaneLayout());  
          label1.setText("STREAM:");
label1.setVerticalAlignment(SwingConstants.CENTER);
          label1.setHorizontalAlignment(SwingConstants.LEFT);
          streamInfoTable = new JTable();        
          pane1.setViewportView(streamInfoTable);
          streamInfoTable.setModel(streamTableModel);
          streamTableModel.setColumnIdentifiers(stream_column_names);

          streamPanel.add(label1, BorderLayout.NORTH);
          streamPanel.add(pane1);
          streamPanel.getPreferredSize();
          splitPane.add(streamPanel);

          packetPanel = new JPanel();
          packetPanel.setLayout(new BorderLayout());

          pane2.setLayout(new ScrollPaneLayout());

          label2.setText("PACKET:");
          label2.setVerticalAlignment(SwingConstants.CENTER);
          label2.setHorizontalAlignment(SwingConstants.LEFT);

          packetInfoTable = new JTable();

          pane2.setViewportView(packetInfoTable);
          packetInfoTable.setModel(packetTableModel);
          packetInfoTable.setRowSelectionAllowed(false);
          packetInfoTable.setColumnSelectionAllowed(false);
          packetTableModel.setColumnIdentifiers(packet_column_names);

         packetPanel.add(label2, BorderLayout.NORTH);
          packetPanel.add(pane2);
          packetPanel.getPreferredSize();
          splitPane.add(packetPanel);

          centerPanel.add(splitPane);

          frame.add(centerPanel, BorderLayout.CENTER);
          frame.setVisible(true);

          jDesktopPane.add(frame);

1 个答案:

答案 0 :(得分:3)

您的centerPanel JPanel看起来正在使用其默认布局,对于JPanel是FlowLayout,这是一种不会调整其显示内容的布局,因此您可能无法显示包含JTable的整个JScrollPane。

考虑给你的centerPanel一个BorderLayout,或者它只是持有JSplitPane而不是其他东西,然后将它们全部放在一起,只需将JSplitPane添加到你的frame的BorderLayout.CENTER位置。在显示之前,不要忘记在JFrame上调用pack()

再次,如果仍然卡住,请将问题提炼为sscce并在此处发布最新代码。

编辑:不确定为什么你给你的JScrollPane一个布局,因为它自己的默认布局应该可以正常工作。