1 Reply Latest reply: Dec 19, 2012 7:22 AM by TPD-Opitz RSS

    Adding scroll or resizing to a panel

    980631
      Hi,
      I am new to swing and need to add a scroller to a data entry screen. I had tried different ways by searching online but no luck. Could anyone help me with it? I guess I need add code in one of the following method.
      Thank you in advance,
      Wennie

      public AddTableItemDialog(Frame owner, DataEntryScreen dataScreen, HashMap defaultValueFields) {
      super(owner, "Add new record", true);
      this.dataScreen = dataScreen;

      getRootPane().putClientProperty("defeatSystemEventQueueCheck",
      Boolean.TRUE);
      this.getContentPane().setLayout(gridBag);

      buildEntryPanel((JPanel) this.getContentPane());
      pack();

      this.addWindowListener(new WindowAdapter() {

      /*@Override*/
      public void windowClosing(WindowEvent e) {
      focusFirstField(AddTableItemDialog.this.getContentPane());
      AddTableItemDialog.this.dataScreen.clearForm();
      userResponse = CANCEL_OPTION;
      }
      });

      ((JPanel) this.getContentPane()).getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("ESCAPE"), "hideAddDialog");
      ((JPanel) this.getContentPane()).getActionMap().put("hideAddDialog", hideAddDialog);

      this.setLocationRelativeTo(owner);
      this.setResizable(false);
      this.defaultValues = defaultValueFields;
      FilterQuestionManager.getInstance().processScreen(this.dataScreen);
      }

      private void buildEntryPanel(JPanel mainPanel) {
      final JLabel instructions = new JLabel("Please complete the following fields:");
      instructions.setFont(Utilities.get12ptVerdanaFont());

      c.anchor = GridBagConstraints.WEST;
      c.insets = new Insets(4, 4, 4, 4);
      c.gridwidth = GridBagConstraints.REMAINDER; //end row
      gridBag.setConstraints(instructions, c);
      mainPanel.add(instructions);

      for (int i = 0; i < dataScreen.size(); i++) {
      Question quest = dataScreen.getQuestion(i);
      final Component qData = (Component) quest.getInputField();
      final QuestionLabel questPanel = quest.getLabel();

      qData.setFont(Utilities.get11ptVerdanaFont());

      //filling default values
      if (quest.getDefaultValue() != null) {
      QuestionType qt = quest.getQuestionType();
      logger.log(java.util.logging.Level.FINE, "showing qt.toString" + qt.toString());
      if (qt.equals(QuestionType.SELECT)) {
      logger.log(java.util.logging.Level.FINE, "showing qt.toString" + qt.toString());
      String key = quest.getDefaultValue();
      String value = AnswerOptions.lookupSelectValue(
      quest.getQuestionId(),
      key);
      quest.getInputField().setData(new Option(key, value));
      } else if (qt.equals(QuestionType.DATE_TIME)) {
      logger.log(java.util.logging.Level.FINE, "showing qt.toString" + qt.toString());
      try {
      java.util.Date defaultDate = df.parse(quest.getDefaultValue());
      quest.getInputField().setData(new Timestamp(defaultDate.getTime()));
      } catch (Exception err) {
      String msg = "Could not parse and set default date for question: " + quest + " value entered: " + quest.getDefaultValue();
      System.out.println(msg);
      logger.log(java.util.logging.Level.WARNING, msg);
      logger.log(java.util.logging.Level.SEVERE, err.getMessage());
      err.printStackTrace();
      }
      } else {
      quest.getInputField().setData(quest.getDefaultValue());
      }

      }

      c.fill = GridBagConstraints.NONE;
      c.weightx = 0.0;
      c.gridwidth = 1;
      gridBag.setConstraints(questPanel, c);
      mainPanel.add(questPanel);

      c.gridwidth = GridBagConstraints.REMAINDER; //end row
      c.fill = GridBagConstraints.HORIZONTAL;
      c.weightx = 1.0;
      gridBag.setConstraints(qData, c);
      mainPanel.add(qData);
      }

      final JPanel navPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));

      Border inside = BorderFactory.createEmptyBorder(10, 0, 8, 0);
      Border outside = BorderFactory.createMatteBorder(1, 0, 0, 0, Color.GRAY);
      navPanel.setBorder(BorderFactory.createCompoundBorder(outside, inside));

      JButton okButton = new JButton("Ok");
      okButton.setActionCommand(OK_OPTION);
      okButton.addActionListener(this);
      okButton.setMargin(new Insets(2, 23, 2, 23));
      okButton.setHorizontalTextPosition(JButton.CENTER);
      okButton.setDefaultCapable(true);
      navPanel.add(okButton);

      JButton cancelButton = new JButton("Cancel");
      cancelButton.setActionCommand(CANCEL_OPTION);
      cancelButton.addActionListener(this);
      cancelButton.setHorizontalTextPosition(JButton.CENTER);
      navPanel.add(cancelButton);

      c.gridwidth = 2;
      c.anchor = GridBagConstraints.CENTER;
      gridBag.setConstraints(navPanel, c);
      mainPanel.add(navPanel);
      }