Reputation: 5084
Hi I am trying to change JScrollPane
content like
scrollPanel = new JScrollPane(new Welcome().display());
for(final JMenuItem i : items) {
i.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String txt = i.getText();
if(txt.equals("Open")) {
scrollPanel = new JScrollPane(new Open().display());
} else if(txt.equals("Save")) {
scrollPanel = new JScrollPane(new Save().display());
} else if(txt.equals("Save as")) {
scrollPanel = new JScrollPane(new SaveAs().display());
} else if(txt.equals("Close")) {
new Close().ask();
scrollPanel = new JScrollPane();
}
scrollPanel.revalidate();
scrollPanel.repaint();
}
});
}
scrollPanel.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPanel.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
menuBar.add(menu);
frame.setJMenuBar(menuBar);
frame.add(scrollPanel);
frame.pack();
frame.setSize(640, 480);
frame.setVisible(true);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
But its not working.
Each class Open, Save, SaveAs return JPane
with JLabel
inside.
Ex.
public class Open {
public JPanel display() {
JPanel panel = new JPanel();
panel.add(new JLabel("Open"));
return panel;
}
}
Upvotes: 2
Views: 113
Reputation: 691635
You're not changing the scrollpane content. You're assigning a new JScrollPane instance to the scrollPanel
field. The scroll pane you have added to the frame and which is displayed stays exactly as it is.
If you want to change the scroll pane contents, you should use
scrollPanel.setViewportView(new Open().display());
Upvotes: 3