I have the following code:
import java.awt.*; import javax.swing.*; /**@name DApplet version 0.9 * @made by Vinvar la Lece */ public class TestForum extends JFrame { TestForum() { this.setSize(1024, 768); this.setTitle("DApplet"); this.setBackground(Color.LIGHT_GRAY); this.getContentPane().add(new DApplet(true), BorderLayout.CENTER); this.setDefaultCloseOperation(EXIT_ON_CLOSE); } public static void main(String[] args) { new DKApplic().setVisible(true); } private static final long serialVersionUID = 1; /* * Private class called in TestForum() */ private class DApplet extends JApplet { boolean isDeelVanApplicatie; JScrollPane mainScrollFrame; public DApplet() { this(false); } DApplet(boolean app) { this.isDeelVanApplicatie = app; if (app) { this.init(); } } public void init() { this.setLayout(new BorderLayout()); // Creating and adding playfield DPlayfield playfield = new DPlayfield(); // The playfield can be larger then the screen. // Therefore, we need to add a scrollable layer beneath the playfield on which we add the playfield mainScrollFrame = new JScrollPane(playfield); // Make sure that you can scroll within the playfield playfield.setViewport(mainScrollFrame.getViewport()); // Add the scrollpane this.add(mainScrollFrame, BorderLayout.CENTER); // Extra's to be deleted when it works mainScrollFrame.setHorizontalScrollBarPolicy(mainScrollFrame.HORIZONTAL_SCROLLBAR_ALWAYS); } private static final long serialVersionUID = 1L; } /* * Private class called in DApplet() -> init() */ private class DPlayfield extends JComponent { JViewport viewport; int boxSize = 50, numberOfRows = 20, numberOfColumns = 10; int width = numberOfRows * boxSize - boxSize; int height = numberOfColumns * boxSize - boxSize; public DPlayfield() { JLabel label = new JLabel(); label.setText("Welkom. Nog meer tekst om alles op te vullen en scrollpane te testen. Is dit genoeg?"); this.add(label); } public void setViewport(JViewport vp) { this.viewport = vp; } @Override public void paintComponent(Graphics g) { super.paintComponent(g); update(g); } public void update(Graphics g) { drawGrid(g); } private void drawGrid(Graphics g) { g.setColor(Color.BLACK); for (int i = 0; i < numberOfRows; i++) { g.drawLine(i * boxSize, 0, i * boxSize, height); } for (int i = 0; i < numberOfColumns; i++) { g.drawLine(0, i * boxSize, width, i * boxSize); } } private static final long serialVersionUID = 1L; } }
I simplified the code so it is just my problem with the grid.
I want that scrollbars appear if the screensize is smaller then the grid. Unfortunally, I can't get it working (and I was working on it this whole weekend). Is there anybody who has an idea?