I have a JPanel with GridBagLayout that is resizing properly when it is not in JScrollPane. But when I add it to the JScrollPane and try to resize the frame the resizing of the content just does not work and it acts like in AbsoluteLayout.
[Code of JPanel and JScrollPane][1]
//ACTIVITIES PANEL
aPanel = new JPanel();
aPanel.setPreferredSize(new Dimension(width, height/100*78));
aPanel.setMaximumSize(new Dimension(width,height/100*78));
aPanel.setMinimumSize(new Dimension(width,height/100*78));
//aPanel.setBackground(new Color(32,64,128));
aPanel.setLayout(new GridBagLayout());
gbcAP = new GridBagConstraints();
gbcAP.weightx = 1;
gbcAP.weighty = 1;
gbcAP.insets = new Insets(0,0,0,0);
gbcAP.anchor = GridBagConstraints.CENTER;
scrollPane = new JScrollPane(aPanel);
scrollPane.setPreferredSize(new Dimension(width,height/100*78));
scrollPane.setMinimumSize(new Dimension(width/2,(height/100*78)/2));
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
mainPanel.add(scrollPane,BorderLayout.CENTER);
loadPanel();
[This is method load JPanel within JScrollPane][2]
public void loadPanel() {
ArrayList<Activity> activities = activityController.getAllActivities();
int size = activities.size();
for(int i = 0 ; i != size; i++) {
int x = i;
int y = 0;
if(x < 5) {
gbcAP.gridx = x;
gbcAP.gridy = y;
aPanel.add(getMiniPanel(activities.get(i)),gbcAP);
scrollPane.repaint();
scrollPane.revalidate();
System.out.println("im here");
}
else {
x = 0;
i++;
gbcAP.gridx = x;
gbcAP.gridy = y;
aPanel.add(getMiniPanel(activities.get(i)),gbcAP);
}
}
}
Panel when its maximazed[1]
[1]: https://i.stack.imgur.com/tUaS1.png
Panel when its resized, JScrollPane even disappears
[2]: https://i.stack.imgur.com/NsQ14.png
UPDATE: THE PROBLEM IS SOLVED
I solved it by adding resizeListener to JPanel
public void componentResized(ComponentEvent e) {
aPanel.setPreferredSize(new Dimension(scrollPane.getWidth(), aPanel.getHeight()));
aPanel.revalidate();
aPanel.repaint();
scrollPane.repaint();
scrollPane.revalidate();
}
Related
My task is to create checked board on JPAnel. For that purpose I'm trying to fill in parent JPanel with JPanels that has borders, but for some reason code doesnt give desired result and no error shown to do investigation why. Here is the code:
private static class GlassView extends JFrame {
private static int width = 600;
private static int height = 750;
public GlassView() {
this.setSize(width, height);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void workingFrame() {
int cols = 0;
int rows = 0;
String frameName = "Bot World";
WorkFrame workF = new WorkFrame(0, 0, frameName);
wfFrame = workF.newFrame();
wfFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
wfFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
wfFrame.setVisible(true);
JSplitPane splitPane = new JSplitPane();
splitPane.setSize(width, height);
splitPane.setDividerSize(0);
splitPane.setDividerLocation(150);
splitPane.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
JPanel panelLeft = createLftPanel();
JPanel panelRight = createRightPanel();
splitPane.setLeftComponent(panelLeft);
splitPane.setRightComponent(panelRight);
wfFrame.add(splitPane);
}
}
Here is the code for the panelRight which needs to be cheked:
public static JPanel createRightPanel() {
JPanel panel = new JPanel();
int rows = 100;
int cols = 100;
panel.setLayout(new GridLayout(rows, cols));
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
JPanel pane = new JPanel();
pane.add(new JTextField("both"));
pane.setBorder(BorderFactory.createLineBorder(Color.black));
panel.add(new JButton(""));
}
}
return panel;
}
Any help will be appreciated. Thank you
Ok, my (second) guess is that the frame isn't laid out again after the call to
wfFrame.setVisible(true);
For me the example below:
public class Framed {
public static void workingFrame() {
JFrame frame = new JFrame();
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.getContentPane().add(createRightPanel(10, 10));
frame.revalidate(); // <-- HERE
}
public static JPanel createRightPanel(int rows, int cols) {
JPanel panel = new JPanel(new GridLayout(rows, cols));
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
JPanel pane = new JPanel();
pane.setBackground((i+j)%2==0?Color.black:Color.white);
panel.add(pane);
}
}
return panel;
}
public static void main(String... none) throws Exception {
workingFrame();
}
}
Shows a checker grid, but is you remove the call to
frame.revalidate(); // <-- HERE
Then the gird is not displayed (until you do something with the frame that causes it to lay out again). Better than calling revalidate() though may be to call setVisible only after all the components have been added.
I am trying to create a 9x9 grid of JTextFields using GridLayout, however I am unable to remove the gap between the JTextFields within the GridLayout.
I have tried setting the horizontal and vertical gap to 0 using setHgap and setVgap methods, but the gap still exists, where am I going wrong ?
This is what I have tried so far,
class BoardGUI extends JFrame implements ActionListener {
private JPanel centerPanel, bottomPanel;
private JTextField grid[][];
private JButton loadBtn, saveBtn, solveBtn;
private GridLayout gridLayout;
private final int N = 9;
private final int MAX_HEIGHT = 450;
private final int MAX_WIDTH = 500;
public BoardGUI() {
super("Solver v2.0");
centerPanel = new JPanel();
bottomPanel = new JPanel();
grid = new JTextField[N][N];
for(int i=0; i<N; i++)
for(int j=0; j<N; j++)
grid[i][j] = new JTextField(1);
loadBtn = new JButton("Load");
saveBtn = new JButton("Save");
solveBtn = new JButton("Solve");
gridLayout = new GridLayout(N,N);
gridLayout.setVgap(0);
gridLayout.setHgap(0);
initLayout();
registerEventListeners();
}
private void initLayout() {
centerPanel.setLayout(gridLayout);
for(int i=0; i<N; i++)
for(int j=0; j<N; j++)
centerPanel.add(grid[i][j]);
bottomPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
bottomPanel.add(loadBtn);
bottomPanel.add(saveBtn);
bottomPanel.add(solveBtn);
this.setLayout(new BorderLayout());
this.getContentPane().add(centerPanel, BorderLayout.CENTER);
this.getContentPane().add(bottomPanel, BorderLayout.SOUTH);
this.setSize(MAX_WIDTH, MAX_HEIGHT);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
private void registerEventListeners() {
loadBtn.addActionListener(this);
saveBtn.addActionListener(this);
solveBtn.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
}
}
Image:
Could you help me understand how to set left vertical alignment to the checkboxes. I mean each checkbox on its own row.
Tried everything I could imagine and have come to the end of my wit.
public class DisplayFrame extends JFrame {
public DisplayFrame(ArrayList<String> contents){
DisplayPanel panel = new DisplayPanel(contents, whiteList);
add(panel);
}
private void displayAll(){
DisplayFrame frame = new DisplayFrame(contents, whiteList);
GridBagLayout gbl = new GridBagLayout();
frame.setLayout(gbl);
frame.setVisible(true);
...
}
public class DisplayPanel extends JPanel {
ArrayList<JCheckBox> cbArrayList = new ArrayList<JCheckBox>();
ArrayList<String> contents;
...
public DisplayPanel(ArrayList<String> contents){
...
createListOfCheckBoxes();
}
private void createListOfCheckBoxes() {
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = contents.size() + 1;
gbc.gridheight = contents.size() + 1;
for (int i = 0; i < contents.size(); i++){
gbc.gridx = 0;
gbc.gridy = i;
String configuration = contents.get(i);
JCheckBox currentCheckBox = new JCheckBox(configuration);
if (whiteList.contains(configuration)){
currentCheckBox.setSelected(true);
}
currentCheckBox.setVisible(true);
add(currentCheckBox, gbc);
}
}
"If I were able to proceed without GridBagLayout, that would suit me"
You can just use a BoxLayout. Box is a convenience class for BoxLayout. You can just do
Box box = Box.createVerticalBox();
for (int i = 0; i < contents.size(); i++){
String configuration = contents.get(i);
JCheckBox currentCheckBox = new JCheckBox(configuration);
if (whiteList.contains(configuration)){
currentCheckBox.setSelected(true);
}
box.add(currentCheckBox);
}
Since Box is a JComponent, you can just add the box to a container.
You can see more at How to Use BoxLayout
Simple Example
import javax.swing.*;
public class BoxDemo {
public static void main(String[] args) {
Box box = Box.createVerticalBox();
JCheckBox cbox1 = new JCheckBox("Check me once");
JCheckBox cbox2 = new JCheckBox("Check me twice");
JCheckBox cbox3 = new JCheckBox("Check me thrice");
box.add(cbox1);
box.add(cbox2);
box.add(cbox3);
JOptionPane.showMessageDialog(null, box);
}
}
Im trying to create a chess program in Java. Right now, I have the board done together with the pieces present, which I can move with my mouse by dragging and dropping.
What I need is to add coordinates to the squares at the sides, like on a real board. Doesn't have to be anything fancy, just a visual. Since I drew my board not with Graphics, I don't know how to draw on top of the board I have created :/
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class ChessGame extends JFrame implements MouseListener,
MouseMotionListener {
JLayeredPane layeredPane;
JPanel chessBoard;
JLabel chessPiece;
int xCoordinate;
int yCoordinate;
public ChessGame() {
Dimension boardSize = new Dimension(600, 600);
layeredPane = new JLayeredPane();
getContentPane().add(layeredPane);
layeredPane.setPreferredSize(boardSize);
layeredPane.addMouseListener(this);
layeredPane.addMouseMotionListener(this);
// adding chess board
chessBoard = new JPanel();
layeredPane.add(chessBoard, JLayeredPane.DEFAULT_LAYER);
chessBoard.setLayout(new GridLayout(8, 8));
chessBoard.setPreferredSize(boardSize);
chessBoard.setBounds(0, 0, boardSize.width, boardSize.height);
for (int i = 0; i < 64; i++) {
JPanel square = new JPanel(new BorderLayout());
chessBoard.add(square);
int row = (i / 8) % 2;
if (row == 0)
square.setBackground(i % 2 == 0 ? new Color(238, 221, 187)
: new Color(204, 136, 68));
else
square.setBackground(i % 2 == 0 ? new Color(204, 136, 68)
: new Color(238, 221, 187));
}
// Black pieces on the board
JLabel piece = new JLabel(new ImageIcon(getClass().getResource(
"Rooka8.png")));
JPanel panel = (JPanel) chessBoard.getComponent(0);
panel.add(piece);
piece = new JLabel(new ImageIcon(getClass().getResource("Knightb8.png")));
panel = (JPanel) chessBoard.getComponent(1);
panel.add(piece);
piece = new JLabel(new ImageIcon(getClass().getResource("Bishopc8.png")));
panel = (JPanel) chessBoard.getComponent(2);
panel.add(piece);
piece = new JLabel(new ImageIcon(getClass().getResource("Queend8.png")));
panel = (JPanel) chessBoard.getComponent(3);
panel.add(piece);
piece = new JLabel(new ImageIcon(getClass().getResource("Kinge8.png")));
panel = (JPanel) chessBoard.getComponent(4);
panel.add(piece);
piece = new JLabel(new ImageIcon(getClass().getResource("Bishopf8.png")));
panel = (JPanel) chessBoard.getComponent(5);
panel.add(piece);
piece = new JLabel(new ImageIcon(getClass().getResource("Knightg8.png")));
panel = (JPanel) chessBoard.getComponent(6);
panel.add(piece);
piece = new JLabel(new ImageIcon(getClass().getResource("Rookh8.png")));
panel = (JPanel) chessBoard.getComponent(7);
panel.add(piece);
piece = new JLabel(new ImageIcon(getClass().getResource("Pawna7.png")));
panel = (JPanel) chessBoard.getComponent(8);
panel.add(piece);
piece = new JLabel(new ImageIcon(getClass().getResource("Pb7.png")));
panel = (JPanel) chessBoard.getComponent(9);
panel.add(piece);
piece = new JLabel(new ImageIcon(getClass().getResource("Pawnc7.png")));
panel = (JPanel) chessBoard.getComponent(10);
panel.add(piece);
piece = new JLabel(new ImageIcon(getClass().getResource("Pawnd7.png")));
panel = (JPanel) chessBoard.getComponent(11);
panel.add(piece);
piece = new JLabel(new ImageIcon(getClass().getResource("Pawne7.png")));
panel = (JPanel) chessBoard.getComponent(12);
panel.add(piece);
piece = new JLabel(new ImageIcon(getClass().getResource("Pawnf7.png")));
panel = (JPanel) chessBoard.getComponent(13);
panel.add(piece);
piece = new JLabel(new ImageIcon(getClass().getResource("Pawng7.png")));
panel = (JPanel) chessBoard.getComponent(14);
panel.add(piece);
piece = new JLabel(new ImageIcon(getClass().getResource("Pawnh7.png")));
panel = (JPanel) chessBoard.getComponent(15);
panel.add(piece);
// White pieces on the board
piece = new JLabel(new ImageIcon(getClass().getResource("Pawna2.png")));
panel = (JPanel) chessBoard.getComponent(48);
panel.add(piece);
piece = new JLabel(new ImageIcon(getClass().getResource("Pawnb2.png")));
panel = (JPanel) chessBoard.getComponent(49);
panel.add(piece);
piece = new JLabel(new ImageIcon(getClass().getResource("Pawnc2.png")));
panel = (JPanel) chessBoard.getComponent(50);
panel.add(piece);
piece = new JLabel(new ImageIcon(getClass().getResource("Pawnd2.png")));
panel = (JPanel) chessBoard.getComponent(51);
panel.add(piece);
piece = new JLabel(new ImageIcon(getClass().getResource("Pawne2.png")));
panel = (JPanel) chessBoard.getComponent(52);
panel.add(piece);
piece = new JLabel(new ImageIcon(getClass().getResource("Pawnf2.png")));
panel = (JPanel) chessBoard.getComponent(53);
panel.add(piece);
piece = new JLabel(new ImageIcon(getClass().getResource("Pawng2.png")));
panel = (JPanel) chessBoard.getComponent(54);
panel.add(piece);
piece = new JLabel(new ImageIcon(getClass().getResource("Pawnh2.png")));
panel = (JPanel) chessBoard.getComponent(55);
panel.add(piece);
piece = new JLabel(new ImageIcon(getClass().getResource("Rooka1.png")));
panel = (JPanel) chessBoard.getComponent(56);
panel.add(piece);
piece = new JLabel(new ImageIcon(getClass().getResource("Knightb1.png")));
panel = (JPanel) chessBoard.getComponent(57);
panel.add(piece);
piece = new JLabel(new ImageIcon(getClass().getResource("Bishopc1.png")));
panel = (JPanel) chessBoard.getComponent(58);
panel.add(piece);
piece = new JLabel(new ImageIcon(getClass().getResource("Queend1.png")));
panel = (JPanel) chessBoard.getComponent(59);
panel.add(piece);
piece = new JLabel(new ImageIcon(getClass().getResource("Kinge1.png")));
panel = (JPanel) chessBoard.getComponent(60);
panel.add(piece);
piece = new JLabel(new ImageIcon(getClass().getResource("Bishopf1.png")));
panel = (JPanel) chessBoard.getComponent(61);
panel.add(piece);
piece = new JLabel(new ImageIcon(getClass().getResource("Knightg1.png")));
panel = (JPanel) chessBoard.getComponent(62);
panel.add(piece);
piece = new JLabel(new ImageIcon(getClass().getResource("Rookh1.png")));
panel = (JPanel) chessBoard.getComponent(63);
panel.add(piece);
}
public void mousePressed(MouseEvent e) {
chessPiece = null;
Component c = chessBoard.findComponentAt(e.getX(), e.getY());
if (c instanceof JPanel)
return;
Point parentLocation = c.getParent().getLocation();
xCoordinate = parentLocation.x - e.getX();
yCoordinate = parentLocation.y - e.getY();
chessPiece = (JLabel) c;
chessPiece.setLocation(e.getX() + xCoordinate, e.getY() + yCoordinate);
chessPiece.setSize(chessPiece.getWidth(), chessPiece.getHeight());
layeredPane.add(chessPiece, JLayeredPane.DRAG_LAYER);
}
// move pieces
public void mouseDragged(MouseEvent me) {
if (chessPiece == null)
return;
chessPiece.setLocation(me.getX() + xCoordinate, me.getY() + yCoordinate);
}
// drop a piece when mouse is released
public void mouseReleased(MouseEvent e) {
if (chessPiece == null)
return;
chessPiece.setVisible(false);
Component c = chessBoard.findComponentAt(e.getX(), e.getY());
if (c instanceof JLabel) {
Container parent = c.getParent();
parent.remove(0);
parent.add(chessPiece);
}
else
{
Container parent = (Container) c;
parent.add(chessPiece);
}
chessPiece.setVisible(true);
}
public void mouseClicked(MouseEvent e) {
}
public void mouseMoved(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public static void main(String[] args) {
JFrame frame = new ChessGame();
frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
frame.pack();
frame.setResizable(true);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
You should put your chessBoard JPanel into another BorderLayout-using JPanel.
This container JPanel will hold a GridLayout using JPanel on the left and on the bottom.
And these can hold JLabels which hold your row and column labels.
Edit
On second thought, better for the container JPanel to use a GridBagLayout so that the side JPanels will size correctly.
Edit 2
For example:
import java.awt.*;
import javax.swing.*;
#SuppressWarnings("serial")
public class ChessGame2 extends JPanel {
private static final int RANKS = 8;
private static final int FILES = RANKS;
private static final int SIDE = 80;
private static final Dimension SQUARE_SIZE = new Dimension(SIDE, SIDE);
private static final Color LIGHT_COLOR = new Color(238, 221, 187);
private static final Color DARK_COLOR = new Color(204, 136, 68);
private static final int GAP = 5;
private JPanel chessBoard = new JPanel(new GridLayout(RANKS, FILES));
public ChessGame2() {
for (int rank = 0; rank < RANKS; rank++) {
for (int file = 0; file < FILES; file++) {
JPanel square = new JPanel();
square.setPreferredSize(SQUARE_SIZE);
Color bg = (rank % 2 == file % 2) ? LIGHT_COLOR : DARK_COLOR;
square.setBackground(bg);
chessBoard.add(square);
}
}
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.weightx = 0.0;
gbc.weighty = 0.0;
gbc.fill = GridBagConstraints.BOTH;
gbc.anchor = GridBagConstraints.WEST;
gbc.insets = new Insets(0, 2 * GAP, 0, 2 * GAP);
add(createRankPanel(), gbc);
gbc.gridx = 2;
gbc.anchor = GridBagConstraints.EAST;
add(createRankPanel(), gbc);
gbc.gridx = 1;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.SOUTH;
gbc.insets = new Insets(GAP, 0, GAP, 0);
add(createFilePanel(), gbc);
gbc.gridy = 2;
gbc.anchor = GridBagConstraints.NORTH;
add(createFilePanel(), gbc);
gbc.gridx = 1;
gbc.gridy = 1;
gbc.anchor = GridBagConstraints.CENTER;
gbc.insets = new Insets(0, 0, 0, 0);
add(chessBoard, gbc);
}
private JPanel createFilePanel() {
JPanel filePanel = new JPanel(new GridLayout(1, 0));
for (int i = 0; i < FILES; i++) {
char fileChar = (char) ('A' + i);
filePanel.add(new JLabel(String.valueOf(fileChar), SwingConstants.CENTER));
}
return filePanel;
}
private JPanel createRankPanel() {
JPanel rankPanel = new JPanel(new GridLayout(0, 1));
for (int i = 0; i < RANKS; i++) {
int row = RANKS - i;
rankPanel.add(new JLabel(String.valueOf(row)));
}
return rankPanel;
}
private static void createAndShowGui() {
ChessGame2 mainPanel = new ChessGame2();
JFrame frame = new JFrame("Chess Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
Which displays as:
The panel that you return from the chess board should be your custom class extending JPanel and in there you will want to override paintComponent().
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
java - How would I dynamically add swing component to gui on click?
I want to add array of buttons dynamically.
I tried like this:
this.pack();
Panel panel = new Panel();
panel.setLayout(new FlowLayout());
this.add(panel);
panel.setVisible(true);
for (int i = 0; i < Setting.length; i++) {
for (int j = 0; j < Setting.width; j++) {
JButton b = new JButton(i+"");
b.setSize(30, 30);
b.setLocation(i * 30, j * 30);
panel.add(b);
b.setVisible(true);
}
}
but didn't get anything , what mistake did I make?
Edit
I have jFrame class "choices" on it i have a button , when I press the button, this is supposed to happen:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
structure.Setting s = new Setting(8, 8, 3, 1, 1);
Game g = new Game();
g.setSetting(s);
this.dispose();
g.show();
}
then i go to the Game class (also jFrame class) to the function setSetting and it is like this:
void setSetting(Setting s) {
this.setting = s;
structure.Game game = new structure.Game(setting);
JPanel panel = new JPanel(new GridLayout(5, 5, 4, 4));
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 5; j++) {
JButton b = new JButton(String.valueOf(i));
panel.add(b);
}
}
add(panel);
pack();
setVisible(true);
}
structure.Setting setting;
}
You may use GridLayout to add equal height/width buttons:
public class Game extends JFrame {
private JPanel panel;
public Game(int rows,int cols,int hgap,int vgap){
panel=new JPanel(new GridLayout(rows, cols, hgap, vgap));
for(int i=1;i<=rows;i++)
{
for(int j=1;j<=cols;j++)
{
JButton btn=new JButton(String.valueOf(i));
panel.add(btn);
}
}
add(panel);
pack();
setVisible(true);
}
}
and code in button's handler should be:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
Game g = new Game(5,5,3,3);
}
Note that you can also pass Setting object reference via Game constructor (when you may add widgets dynamically) instead of calling setSetting method.
The JPanel is already under the control of a layout manager, setting the size and position of the buttons is irrelevant, as they will changed once the panel is validated.
Try, instead, adding the panel AFTER you've populated it with buttons..
UPDATED with Example
Without further evidence, we are only guessing...You now have two people who have no issues.
Panel panel = new Panel();
for (int i = 0; i < Setting.length; i++) {
for (int j = 0; j < Setting.width; j++) {
jButton b = new jButton(i + "");
panel.add(b);
}
}
this.add(panel);
public class BadBoy {
public static void main(String[] args) {
new BadBoy();
}
public BadBoy() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
int buttonCount = (int) Math.round(Math.random() * 20);
int columnCount = (int) Math.round(Math.random() * 20);
JPanel buttonPane = new JPanel(new GridLayout(0, columnCount));
for (int i = 0; i < buttonCount; i++) {
JButton b = new JButton(i + "");
buttonPane.add(b);
}
frame.add(buttonPane);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class ButtonPane extends JPanel {
public ButtonPane() {
}
}
}
I am guessing that this extens or is some kind of frame?
First step is to pack the frame by doing this.pack(); sets the frame size ti just fit all objects.
I assume you have set it to visible?
Now you should be able to see the buttons.
If you want a different layout use panel.setLayout(new SomeLayout);
Try to use setBounds(x,y,width,heigth) method
setBound method
for (int i = 0; i < 1; i++) {
for (int j = 0; j <1; j++) {
JButton b = new JButton("button");
b.setBounds(500, 500, 100, 20);
this.add(b);
}
}
this.repaint();
this.validate()