I am trying to position 2 JButtons under a grid of 9x9, but when I add the buttons, the grid is changed to accomodate the buttons, placing everything, including the buttons in a rectangle.
What i want is to have a grid and two buttons below the grid itself.
mainPanel.setBorder(BorderFactory.createLineBorder(Color.black, BORDER_WIDTH));
mainPanel.setLayout(new GridLayout(3, 3));
for (int i = 0; i < subPanels.length; i++)
{
for (int j = 0; j < subPanels[i].length; j++)
{
subPanels[i][j] = new JPanel(new GridLayout(3, 3));
subPanels[i][j].setBorder(
BorderFactory.createLineBorder(Color.black, BORDER_WIDTH));
mainPanel.add(subPanels[i][j]);
}
}
buttonPanel.add(clear);
buttonPanel.add(solve);
buttonPanel.setLayout(new GridLayout(1,1));
mainPanel.add(buttonPanel);
The buttonPanel is what is supposed to be under the mainPanel..
The easiest thing is probably to put your mainPanel into the center of a BorderLayout, and then put the buttonPanel in the south.
Also, you're currently setting the GridLayout for your buttonPanel to 1 row x 1 column. You should probably set the layout before adding the buttons, and set it to 1 row x 2 columns.
Every component on a form has a parent. If you place button1 on form1, button1.parent will be 'form1'.
change the parent of the buttons you want to the appropriate parent
Related
I am trying to figure out the layout for this(the rest of the code is in the early stages) but for this block, I am trying to figure out the best(and doable) way to format it. I want it to be an 8x8 grid that I will eventually populate with the treasure/empty buttons but I also need a title up top as well as some labels and text on the left. I am unsure if I am able to do multiple grids but what I did below is try to create a 1x2 grid and then place two other grids inside, one with the info on the left(3x2), and another with the 8x8 grid for the buttons. I know it's not close to what it needs to be but none of the grids are showing up at all(it's just putting the title and then one column with 8 rows) and I wanna know if I'm even on any sort of right track, or if I'm just making things up at this point. Any tips would be appreciated, or resources about possibly nesting the grids? I can't find anything in my book about That specifically.
private void buildPanel()
{
// Create labels to display the
treasuresLeftLabel = new JLabel("Treasures left: ");
treasuresFoundLabel = new JLabel("Treasures found: ");
triesLeftLabel = new JLabel("Tries left: ");
// Create text fields for each label
treasuresLeftTextField = new JTextField(2);
treasuresLeftTextField.setEditable(false);
treasuresLeftTextField.setText(String.valueOf(20-game.getTreasuresFound()));
treasuresFoundTextField = new JTextField(2);
treasuresFoundTextField.setEditable(false);
treasuresFoundTextField.setText(String.valueOf(game.getTreasuresFound()));
triesLeftTextField = new JTextField(2);
triesLeftTextField.setEditable(false);
triesLeftTextField.setText(String.valueOf(game.getTriesLeft()));
emptyButton = new EmptyButton();
emptyButton.addActionListener(new emptyButtonListener());
treasureButton = new TreasureButton();
treasureButton.addActionListener(new treasureButtonListener());
// new JPanel object referenced by panel
panel = new JPanel();
panel.setBorder(BorderFactory.createTitledBorder("Treasure Hunt"));
// Add a gridlayout to the content pane
panel.setLayout(new GridLayout(1, 2));
panel.setLayout(new GridLayout(3, 2));
panel.add(treasuresLeftLabel);
panel.add(treasuresLeftTextField);
panel.add(treasuresFoundLabel);
panel.add(treasuresFoundTextField);
panel.add(triesLeftLabel);
panel.add(triesLeftTextField);
panel.setLayout(new GridLayout(8, 8));
panel.add(treasureButton);
panel.add(emptyButton);
}
You can't use multiple grids within the same JPanel - one panel, one layout manager.
But you can nest layout managers (and thereby grids) by using nested panels.
For example you could use a BorderLayout for the first panel (containing the title at the top, the info panel on the left and the button panel in the center.
The code to construct those panel then might look like this:
// panel contains the complete UI
panel = new JPanel();
panel.setBorder(BorderFactory.createTitledBorder("Treasure Hunt"));
panel.setLayout(new BorderLayout());
panel.add(new JLabel("This is the Title"), BorderLayout.PAGE_START);
JPanel infoPanel = new JPanel();
infoPanel.setLayout(new GridLayout(3, 2));
infoPanel.add(treasuresLeftLabel);
infoPanel.add(treasuresLeftTextField);
infoPanel.add(treasuresFoundLabel);
infoPanel.add(treasuresFoundTextField);
infoPanel.add(triesLeftLabel);
infoPanel.add(triesLeftTextField);
panel.add(infoPanel, BorderLayout.LINE_START);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(8, 8));
buttonPanel.add(treasureButton);
buttonPanel.add(emptyButton);
for (int i = 0; i < 62; i++) {
buttonPanel.add(new JButton(String.format("%02d", i)));
}
panel.add(buttonPanel, BorderLayout.CENTER);
I have a JPanel in a JScrollPane, and the JPanel contains many small JPanels. But the default position of scrollbar is not at the top, thus the small JPanels isn't shown from the first one. How to let scrollbar's default position be at top?
My code is simplified and looks like this:
JFrame resultFrame = new JFrame("Searching Result");
JPanel listPanel = new JPanel();
listPanel.setLayout(new BoxLayout(listPanel, BoxLayout.Y_AXIS));
for (int i=0; i<5; i++) {
listPanel.add(smallPanel) // smallPanel is created in another class, which is related to i.
}
JScrollPane scrollPanel = new JScrollPane(listPanel);
scrollPanel.setPreferredSize(new Dimension(500, 750));
resultFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
resultFrame.setContentPane(scrollPanel);
resultFrame.pack();
resultFrame.setLocationRelativeTo(null);
resultFrame.setVisible(true);
I'm Writing a game where there are a set of islands and there are 5 workers on each island when i left click on a worker a cursor appears on it and when i right click on somewhere in the island the selected worker moves there..i want a JPanel to show on the right side of the screen every time i left click on a worker.the panel will show the workers stats such as Health and carrying weight but the problem is i can't make the panel appear.this is the code where i left click on a worker:
if(SwingUtilities.isLeftMouseButton(e)){
if(canSelect){
for (int j = 0; j < countries.size(); j++) {
for (int i = 0; i < countries.elementAt(j).getMen().size(); i++) {
if(m==countries.elementAt(j).getMen().elementAt(i).getX() && n==countries.elementAt(j).getMen().elementAt(i).getY()) {
countries.elementAt(j).getMen().elementAt(i).setY(countries.elementAt(j).getMen().elementAt(i).getY()+1);
countries.elementAt(j).getMen().elementAt(i).setSelected(true);
getGraphics().drawImage(arrow.getImage(), (countries.elementAt(j).getMen().elementAt(i).getX()+mapX)*20+5,(countries.elementAt(j).getMen().elementAt(i).getY()+mapY)*20-12,null);
canSelect = false;
}
}
}
}
}
I tried the below code on a jbutton click within JFrame and it didn't worked as expected. (that is to dynamically creating a panel with 2 labels and show in a JFrame)
JPanel p = new JPanel();
p.setLayout(new GridLayout(2, 1));
p.add(new JLabel("Health : 90%"));
p.add(new JLabel("Carrying weight : 4 Kg"));
this.add(p); //JFrame
p.setSize(100, 50);
p.setLocation(100, 100);
p.setVisible(true);
But adding the code this.validate(); at the end of the above code, it worked fine.
if, is this your problem, then it may help you.
Reference Link
May be your JPanel is created by below your existing JPanel so it can't be seen. Place your new JPanel on your existing JPanel . Like this
JPanel nPanel = new JPanel();
nPanel.setBounds(x,y,w,h); //your desired location and size
mainPanel.add(nPanel);
nPanel.setVisible(false);
Your can make it visible again with your event
I am trying to achieve a layout similar to that of a carousel. It needs to have images added horizontally with a checkbox field in the second row. I have a panel within a jscrollpane and individual images are added to the panel as labels. Please see screen shot.
screenshot
When I scroll the pane , the first row containing the images stays well within the panel..but if you notice the second row of checkboxes , it scrolls out of the panel. Here is the code ...
JLabel lab1=new JLabel();
for (int ii=0; ii<imageFiles.length; ii++) {
GridBagConstraints constraint = new GridBagConstraints();
lab1 = new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);
constraint.gridx = ii;
constraint.gridy =0;
jPanel9.add(lab1,constraint);
}
for (int ii=0; ii<imageFiles.length; ii++) {
GridBagConstraints constraint1 = new GridBagConstraints();
constraint1.anchor = GridBagConstraints.SOUTH;
chkbox = new Checkbox("asdasdada");
constraint1.gridx = ii;
constraint1.gridy =1;
jPanel9.add(chkbox, constraint1);
}
Not sure what is wrong..Any help is much appreciated..Thanks..
The problem is that you are mixing AWT components (heavyweight) with Swing components (lightweight). I have 2 recommendations:
Don't mix heavyweight and lightweight components
Try to use lightweight components as much as possible
So in your code, replace Checkbox by JCheckbox and it should work just fine.
I have some tables which should draw from right to left and top to bottom at the frame. Right now I used absolute layout and working with coordination. Is there any BoxLayout or any other Java layout can do it? I should mention that the number of tables is dynamic.
My second question is how can I dock these tables to frame? I mean I want when the frame resize, tables keep their positions on the screen.
Most layout managers will respect the orientation of the component:
panel.setComponentOrientation( ComponentOrientation.RIGHT_TO_LEFT );
panel.add(...);
Or you can always just add the components to the beginning of the container
panel.add(component1, 0);
panel.add(component2, 0);
You may want to use a grid if you are arranging things into a table. All of the elements in a grid should be the same size.
To arrange some items vertically where the size of each row can vary, try this:
static JPanel buildPanel() {
JPanel vPanel = new JPanel();
BoxLayout layout = new BoxLayout(vPanel, BoxLayout.Y_AXIS);
vPanel.setLayout(layout);
JPanel[] rowPanels = new JPanel[5];
int counter=1;
for (int i = 0; i < rowPanels.length; i++) {
rowPanels[i] = new JPanel();
rowPanels[i].setLayout(new FlowLayout(FlowLayout.LEFT, 2, 2));
rowPanels[i].add(new JButton("button " + counter++));
rowPanels[i].add(new JButton("Your button " + counter++));
rowPanels[i].add(new JButton("Shabutton"));
vPanel.add(rowPanels[i]);
}
return vPanel;
}
public static void main(String[] args) {
JFrame gridFrame = new JFrame();
gridFrame.add(buildPanel() );
gridFrame.pack();
gridFrame.setVisible(true);
}
You can prevent the whole JFrame from resizing using gridFrame.setResizable(false);
You can prevent sapce from being added between the rows when the window is resized with a method call like this:
rowPanels[i].setMaximumSize(new Dimentsion(400,32));