JScrollPane在动态填充的JPanel上使用null布局

时间:2014-01-15 22:51:32

标签: java swing jscrollpane layout-manager null-layout-manager

我想在我的GUI上使用滚动条。该区域,我希望“可滚动”是没有任何LayoutManager的JPanel,因为内容是静态和动态之间的东西......但也许我的代码的一部分将有所帮助

这是:

pane = getContentPane();

    jpane = new JPanel();
//  jpane.setBounds(0, 75, 680, 255);
    jpane.setLayout(null);

    jsp = new JScrollPane(jpane, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
    jsp.setBounds(0, 75, 680, 255);
    jsp.setPreferredSize(new Dimension(680,255));
pane.add(jsp);

我的JPanel将填充这段代码......

for(int i = 0; i < Integer.parseInt(spaltenzahl.getText()); i++) {
        JTextField spaltennamen_feld = new JTextField();
        spaltennamen_feld.setBounds(10, 10+35*i, 150, 25);
        spaltennamen_arraylist.add(spaltennamen_feld);

        JComboBox<String> datentypen_box = new JComboBox<String>(types);
        datentypen_box.setBounds(170, 10+35*i, 150, 25);
        datentypen_arraylist.add(datentypen_box);

        JComboBox<String> datenzusatz_box = new JComboBox<String>(comb);
        datenzusatz_box.setBounds(330, 10+35*i, 100, 25);
        datenzusatz_arraylist.add(datenzusatz_box);

        jpane.add(spaltennamen_feld);
        jpane.add(datentypen_box);
        jpane.add(datenzusatz_box);
    }

问题:我无法添加我的GUI屏幕截图,因为缺少了ehr ...我在这里忘记了这个必需品的名称^ _ ^ 但我真正的问题是:溢出的内容将不会显示在我的JPanel上,而我的ScrollPane不会滚动。有什么想法吗?

非常感谢:)

1 个答案:

答案 0 :(得分:2)

您的解决方案不属于上述情况。我想你想要使用JTable,一个有三列,最后两个编辑器是JComboBox。


修改
例如:

import java.util.Random;
import java.util.Vector;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

public class Foo2 {

   private static void createAndShowGui() {
      String[] columns = {"Spaltennamen", " Datentypen", "Datenzusatz"};
      String[] dataStrings1 = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" };
      String[] dataStrings2 = {"One", "Two", "Three", "Four", "Five"};
      Random random = new Random();

      DefaultTableModel model = new DefaultTableModel(columns, 0);
      int spaltenzahl = 10;  // this can change
      for (int i = 0; i < spaltenzahl; i++) {
         Vector<String> rowData = new Vector<>();
         rowData.add("Row Number " + (i + 1));
         rowData.add(dataStrings1[random.nextInt(dataStrings1.length)]);
         rowData.add(dataStrings2[random.nextInt(dataStrings2.length)]);
         model.addRow(rowData);
      }

      JTable table = new JTable(model);
      table.getColumn(columns[1]).setCellEditor(new DefaultCellEditor(new JComboBox<>(dataStrings1)));
      table.getColumn(columns[2]).setCellEditor(new DefaultCellEditor(new JComboBox<>(dataStrings2)));
      JScrollPane scrollPane = new JScrollPane(table);


      JFrame frame = new JFrame("Foo");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(scrollPane);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

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