3 Attachment(s)
What in SWINGS name do i use to get this outcome?!
(Credit: Copeg has given good insight so far, and primarily offered the layout manager wrapper trick, which does work!)
EDIT: I have decided that Copeg's solution isn't really a hack, and is common practice, so I am using the wrapper trick.
I want to now add a invisible layer on top of this to more or less protect it from user interaction.
My program builds this:
Attachment 1079
it is a bunch of individual 80x80 JPanels(called Tiles) inside a GridLayout JPanel(called BattleGrid).
I need to place something over top of the BattleGrid that is invisible but I can add JPanels (Tiles) to by position and those will be visible, and will stay over the BattleGrid(in relative position) when ever the user moves the entire JFrame.
Thanks,
Jonathan
Re: What in SWINGS name do i use to get this outcome?!
I'm assuming you're painting the grid onto some component (i.e. a JPanel)?
The best solution is to override the paintComponent() method in such a way that it dynamically calculates what needs to be painted. Swing will automatically repaint on a resize, regardless of how it's resized (either manually or managed by the LayoutManager).
ex.: Draw a 10x10 grid that fills the JPanel.
Code java:
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Test extends JPanel
{
@Override
public void paintComponent(Graphics g)
{
for (int i = 0; i <= getWidth() / 10; ++i)
{
g.drawLine(10 * i, 0, 10 * i, getHeight());
}
for (int j = 0; j <= getHeight() / 10; ++j)
{
g.drawLine(0, 10 * j, getWidth(), 10 * j);
}
}
public static void main(String[] args)
{
JFrame f = new JFrame();
f.add(new Test());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
As far as maintaining the right toolbar to a static size, you could try looking into using the following combination:
1. Have the "main" LayoutManager which contains the grid and the toolbar be a GridBagLayout. I believe if you set the weightx of the grid to 1 and the weighty of the toolbar to 0 then you'll have a static width toolbar and the grid will fill everything else.
2. Set the main toolbar component to have a BoxLayout. I'm not too familiar with this type of LayoutManager so you'll have to play around with it, but from the looks of it you should be able to specify the maximum and minimum sizes of components you add to it, so in theory setting the max/min size equal to each other results in a fixed size component.
edit: A better solution might be to use a Toolbar. Again, this isn't something I'm familiar with so you'll have to do some investigation yourself, but it looks promising and likely more functional than the half-hacked solution I gave above.
Re: What in SWINGS name do i use to get this outcome?!