Question

i want a scrollpane on my GUI. The area, i want to have "scrollable" is a JPanel without any LayoutManager, because the content is something between static and dynamic... but maybe a part of my code will help

Here it is:

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

My JPanel will be filled with this piece of code...

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

Problem: i can't add a screenshot of my GUI, cause of missing ehr... i forgot the name of this required things here ^_^ But i the real problem is: overflowing content won't be displayed on my JPanel and my ScrollPane does not scroll. Any idea?

Thanks alot :)

Was it helpful?

Solution

Your solution is none of the above. I think that you want to use a JTable, one with three columns, the last two whose editor is a JComboBox.


Edit
For example:

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();
         }
      });
   }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top