I use Jlabel to show image, but i have a problem with scrollbars now. Scrollbars don't appear. Let's I show my code:
public AVFrame(String title, ArrayList<String> imageList) throws HeadlessException {
super(title);
this.imageList = imageList;
AddRootPanel(imageList);
//Config frame
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(FRAME_WIDTH, FRAME_HEIGHT);
addWindowListener(new WindowAdapter() {
/**
* Invoked when a window is in the process of being closed.
* The close operation can be overridden at this point.
*/
@Override
public void windowClosing(WindowEvent e) {
rp.stopListening();
System.exit(0);
}
});
//pack();
//setResizable(false);
setLocationRelativeTo(null); // center the window
setVisible(true);
}
/**
* Method add root panel into frame
*/
private void AddRootPanel(ArrayList<String> imageList) {
rp = new RootPanel(imageList);
Container container = getContentPane();
container.add(rp);
}
**
* Creates a new <code>JPanel</code> with a double buffer
* and a flow layout.
*/
public RootPanel(ArrayList imageList) {
this.imageList = imageList;
listener = new AVListener(this);
controller = new Controller();
controller.addListener(listener);
addImageLabel();
showImage((File) this.imageList.get(0));
}
private void addImageLabel() {
imgLabel = new JLabel();
JScrollPane scrollPane = new JScrollPane(imgLabel);
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
add(scrollPane);
}
public void showImage(File imagePath) {
try {
bufferedImage = ImageIO.read(imagePath);
} catch (IOException e) {
e.printStackTrace();
}
ImageIcon icon = new ImageIcon(bufferedImage);
imgLabel.setIcon(icon);
If anyone need my code see: https://github.com/VitaliyPetrov/AIR_WORK/tree/master/Air%20Viewer
P.S. My global idea is create app, that will be can resize, rotate images. If anyone, have suggestions or tips how improve my code. I'll be thankful.
Add JScrollPane
directly into the JFrame's Content Pane
instead of adding JScrollPane
first into JPanel
.
Do in this way
private void AddRootPanel(ArrayList<String> imageList) {
rp = new RootPanel(imageList);
Container container = getContentPane();
container.add(rp.getScrollpane());
}
// Not require to extend JPanel for RootPanel class if it contains only single component
class RootPanel{
private JScrollPane scrollpane;
...
private void addImageLabel() {
imgLabel = new JLabel();
scrollPane= new JScrollPane(imgLabel);
...
}
public JScrollPane getScrollpane(){
return scrollPane;
}
}