1

I have a method which is popping up a JFrame/JScrollPane which has 2 columns, one for the keys of a hashmap and another column for the values of the hashmap. The values populate in editable text fields. I am trying to add an ok and cancel button to the JFrame, however after making the changes I do not see any changes in my application. (not receiving any errors either, runtime or compilation). Any idea why my button are not showing?

private static List<JTextField> showFrames(Map<String, String> longToShortNameMap) {
    JFrame frame = new JFrame("Data Changed");
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.setSize(400, 500);
    frame.setResizable(true);
    frame.setLocationRelativeTo(null);

    JPanel panel = new JPanel(new GridLayout(0, 2));
    List<String> keys = new ArrayList(longToShortNameMap.keySet());
    List<JTextField> textFields = new ArrayList<>();
    for (String key : keys) {
        JLabel label = new JLabel(key);
        JTextField textField = new JTextField(longToShortNameMap.get(key));
        panel.add(label);
        panel.add(textField);
        textFields.add(textField);
    }

    JScrollPane scrollPane = new JScrollPane(panel);
    JButton okButton = new JButton("ok"); //added for ok button
    JButton cancelButton = new JButton("cancel");//added for cancel button
    okButton.setVisible(true);//added for ok button
    cancelButton.setVisible(true);//added for cancel button
    scrollPane.add(okButton);//added for ok button
    scrollPane.add(cancelButton);//added for cancel button
    scrollPane.setVisible(true);
    scrollPane.setSize(500, 500);
    frame.add(scrollPane);
    return textFields; //make clicking ok button return this, this method should return void
}

I have also tried adding the buttons directly to the JFrame instead of the JScrollPane which yields the same result: no change and no error (Note: these buttons should be below the JScrollPane)

If I add the buttons to the panel, then the buttons DO appear however I have to scroll to the bottom of my JScrollPane to see them which is not desirable.

GregH
  • 5,125
  • 8
  • 55
  • 109
  • 1
    See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) Use a `JDialog` for this.. *"..trying to add an ok and cancel button.."* But `JOptionPane` would be simpler, since it can be configured to show an OK/Cancel button combo. – Andrew Thompson Aug 06 '15 at 13:27
  • You have several problems, foremost that you are not using JScrollPane correctly. You should check out JOptionPane for auto-adding OK Cancel buttons, and otherwise practice your swing tutorials to learn how swing works – ControlAltDel Aug 06 '15 at 13:31
  • 1
    `JScrollPane scrollPane = new JScrollPane(panel); .. scrollPane.add(okButton); ..` A `JScrollPane` is designed to hold exactly one component. Since that one component might be a `JPanel` the panel itself might also contain other panels and components, but they should be added to that single component. – Andrew Thompson Aug 06 '15 at 13:32

1 Answers1

3

You need to give your JFrame a layout to which you place your scroll view and a panel with buttons. Here is an example:

Before adding anything to your frame, write

frame.getContentPane().setLayout(new BorderLayout());

Create a panel for buttons

JPanel panel = new JPanel(); //Flow layout by default
//If you want to anchor the buttons to the right you might try
panel.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
panel.add(okButton);
panel.add(cancelButton);

then add scrollPane like this

frame.getContentPane().add(scrollPane, BorderLayout.CENTER);

and the buttons in the panel

frame.getContentPane().add(panel, BorderLayout.NORTH);
milez
  • 2,201
  • 12
  • 31