I have a class (Game) in which the map panel has been added to JScrollPane, the map panel itself is populated with panels of the MapElement class. At the same time, the map panel is larger than the window size, but JScroll does not scroll through it. How to fix it ? Thanks in advance for your help.

Here is the code for the class code map

public class Map extends JPanel { Timer time = new Timer(); int size = 52; //GameMenu gmen= new GameMenu(); int px, py; int mx, my;// положение мышки Dimension sSize; int shig, swid; boolean shouldmove; MapElement mapelem[][] = new MapElement[60][40]; public Map() { GroupLayout groupLayout = new GroupLayout(this); groupLayout.setHorizontalGroup( groupLayout.createParallelGroup(Alignment.LEADING) .addGap(0, 651, Short.MAX_VALUE) ); groupLayout.setVerticalGroup( groupLayout.createParallelGroup(Alignment.LEADING) .addGap(0, 445, Short.MAX_VALUE) ); setLayout(groupLayout); addMouseListener(new Mouse()); addMouseMotionListener(new Mouse1()); sSize = Toolkit.getDefaultToolkit().getScreenSize(); swid = sSize.width; shig = sSize.height; System.out.println(shig); loadmap(); makemap(); time.schedule(task, 1, 10); } public void savemap() { } public void loadmap() { } TimerTask task = new TimerTask() { public void run() { //movemap(); } }; public void makemap() { for(int x=0;x<60;x++){ for(int y=0;y<40;y++){ createObl(x,y,MapElement.Water,0); } } repaint(); updateUI(); } public void createObl(int x,int y,int category,int own){ if(mapelem[x][y]!=null){ remove(mapelem[x][y]); } mapelem[x][y]=new MapElement(y*100+x,category,own,size); mapelem[x][y].setBounds(x * size + px, y * size + py, size-2, size-2); add(mapelem[x][y]); } } 

Game Class Code

 public class Game extends JPanel { Map map= new Map(); GameMenu gamemen= new GameMenu(); public Game() { Dimension sSize = Toolkit.getDefaultToolkit().getScreenSize(); int swid = sSize.width; int shig = sSize.height; //System.out.println("high map ="+map.getSize().getHeight()); setLayout(null); map.setSize(61*map.size, 41*map.size); JScrollPane scrollPane = new JScrollPane(); scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS); scrollPane.setBounds(0, 75, swid, shig); scrollPane.setViewportView(map); map.setLayout(null); add(scrollPane); gamemen.setBounds(0, 0, swid, 75); add(gamemen); } } 

Here is the MapElement class

 public class MapElement extends JPanel { /** * */ private static final long serialVersionUID = -373173344652368936L; Color Countrycolor; public final static int Ground = -8000054, Water = -777775445, Lake = -898455454, RUS = -222222298, USA = -987546548, UK = -95847245, China = -1216586558, Japan = -547862184, GER = -254789554, Country6 = -66556468, noOwn = -999999845; int x, y, px, py; int size, nomer, category; int own; double oil, gaz, ugol, wood, iron, metal, granit; public MapElement(int nomer,int category,int own, int size) { this.size = size; this.nomer = nomer; if (category == Water) { setBackground(Color.BLUE); } if (category == Lake) { setBackground(new Color(50,160,225)); } if (category == Ground) { switch (own) { case RUS: Countrycolor = Color.RED; break; case USA: Countrycolor = Color.darkGray; break; case GER: Countrycolor = Color.YELLOW; break; case UK: Countrycolor = Color.GREEN; break; case China: Countrycolor = Color.ORANGE; break; case Japan: Countrycolor = Color.CYAN; break; case Country6: Countrycolor = Color.MAGENTA; break; case noOwn: Countrycolor = Color.lightGray; break; case 0: Countrycolor = Color.lightGray; break; } setBackground(Countrycolor); } } public void choseColor() { } } 
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

1 answer 1

Regarding JScrollPane problem is in the following code:

 public Game() { Dimension sSize = Toolkit.getDefaultToolkit().getScreenSize(); int swid = sSize.width; int shig = sSize.height; //System.out.println("high map ="+map.getSize().getHeight()); setLayout(null); map.setSize(61*map.size, 41*map.size); JScrollPane scrollPane = new JScrollPane(); scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS); scrollPane.setBounds(0, 75, swid, shig); scrollPane.setViewportView(map); map.setLayout(null); add(scrollPane); gamemen.setBounds(0, 0, swid, 75); add(gamemen); } 

First, you specify by calling scrollPane.setBounds(0, 75, swid, shig); The scrollPane size scrollPane equal to the screen size and with an offset of 75 in y , so the scrollPane simply does not fit into the screen, and the scroll bar is not visible from the bottom. If you need to place the toolbar and the scroll area below it, use BorderLayout like this:

 setLayout( new BorderLayout() ); add( scrollPane, BorderLayout.CENTER ); add( gamemen, BorderLayout.NORTH ); 

BorderLayout itself will stretch everything considering the preferred size (preferred size).

After that, the scroll bar will appear, but will not work. The documentation says:

By default JScrollPane uses ScrollPaneLayout to handle. ScrollPaneLayout :

  1. If the view implements a combination of the getPreferredScrollableViewportSize , getScrollableTracksViewportWidth and getScrollableTracksViewportHeightis used, otherwise
  2. getPreferredSize is used.

Those. in your case, JScrollPane oriented to getPreferredSize() . The simplest solution would be to write:

 map.setSize(61*map.size, 41*map.size); map.setPreferredSize( map.getSize() ); 

But it is better to override getPreferredSize in your Map .