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.