I want to create a grid of buttons. Between the buttons there should not be a space, so that the buttons touch the next buttons.
import javax.swing.*;
import java.awt.*;
import javax.swing.border.*;
public class Bild extends JFrame {
public static class createButton extends JPanel {
public createButton() {
JButton b = new JButton();
Border border = new LineBorder(Color.BLACK, 1);
b.setBorder(border);
b.setBackground(Color.WHITE);
b.setPreferredSize (new Dimension(10,10));
this.add(b);
}
}
public Bild() {
GridLayout layout = new GridLayout(10,10,0,0);
this.setLayout(layout);
for (int i = 0; i < 100; i++) {
this.add(new createButton());
}
}
}
import javax.swing.*;
import java.awt.*;
public class Main{
public static void main (String[] args) {
JFrame frame = new Bild();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,500);
frame.setVisible(true);
}
}
with the grid layout I tried to get a 10x10 grid of buttons. But although I created the GridLayout with the parameters 10,10,0,0 there is a space between the buttons.
Where is my mistake?
It's because createButton is actually a JPanel, which has a FlowLayout as it's layout manager by default. Make createButton a method that returns a JButton and the button will span across the whole space within the cell. (You can remove the setPreferredSize()). If you want the buttons to remain 10x10 pixels and be centered within the frame: Set the layout manager of the main panel to a GridBagLayout and add a JPanel with the GridLayout to it. Maybe also override getPreferredSize() of the buttons rather than calling setPreferredSize().
Buttons spanning across whole screen:
class Bild extends JFrame {
public JButton createButton() {
JButton b = new JButton();
Border border = new LineBorder(Color.BLACK, 1);
b.setBorder(border);
b.setBackground(Color.WHITE);
return b;
}
public Bild() {
setLayout(new GridLayout(10, 10));
for (int i = 0; i < 100; i++) {
add(createButton());
}
}
}
Buttons remaining 10x10:
class Bild extends JFrame {
public JButton createButton() {
JButton b = new JButton() {
#Override
public Dimension getPreferredSize() {
return new Dimension(10, 10);
}
};
Border border = new LineBorder(Color.BLACK, 1);
b.setBorder(border);
b.setBackground(Color.WHITE);
return b;
}
public Bild() {
JPanel panel = new JPanel(new GridLayout(10, 10));
for (int i = 0; i < 100; i++) {
panel.add(createButton());
}
setLayout(new GridBagLayout());
add(panel);
}
}
Related
This project revolves around a game that is a variation of TicTacToe called SOS. One of the requirements is that the game grid needs to have two size options. The smaller grid is 5x5 and the larger is 8x8.
My goal is to have the grid size change based off which radio button is selected. In my code below I have a commented out method to change the GRID_SIZE variable based off which radio button is selected. But it does not work where it is currently and I am struggling to come up with the solution. The other problem related to the grid size changing that I think I'll have is, I do not believe the way I create the grid now will allow for it to change live as the radio buttons are pushed.
I will need to be keeping track of what gets played in each cell of the grid (whether a player is placing an S or an O) So my thought is maybe there is a better way to create the grid for both the GUI and as a storage method for the moves played.
This project is my first java project and first GUI project of this depth. It is also the major project for one of my last classes to graduate so I'm taking this seriously and could really use the help. I know my code is probably not great, I'm here to improve so any help is welcomed.
package practice;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
#SuppressWarnings({ "serial", "unused"})
public class SOS_GUI extends JFrame {
public int GRID_SIZE = 8;
public Grid grid;
public SOS_GUI() {
GameBoard();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setTitle("SOS Practice");
this.setLocationRelativeTo(null);
setVisible(true);
}
public void GameBoard(){
// CONTENT PANE FOR HOLDING ALL GUI COMPONENTS
Container ContentPane = getContentPane();
// PANEL FOR GAME GRID
JPanel gameBoardCanvas = new JPanel();
gameBoardCanvas.setLayout(new GridLayout(GRID_SIZE, GRID_SIZE));
for (int x = 0; x < GRID_SIZE; x++) {
for (int y = 0; y < GRID_SIZE; y++) {
final Grid cell = new Grid(x, y);
gameBoardCanvas.add(cell);
}
}
// FOUR PANELS SURROUNDING GAME GRID
JPanel TopPanel = new JPanel();
JPanel BottomPanel = new JPanel();
JPanel LeftPanel = new JPanel();
JPanel RightPanel = new JPanel();
JLabel SpacerLabel = new JLabel(" || ");
// GAME MODE OOPTIONS - SIMPLE OR GENERAL
JLabel GameModeLabel = new JLabel("Game Mode :");
JRadioButton SimpleGameButton = new JRadioButton("Simple", true);
JRadioButton GeneralGameButton = new JRadioButton("General");
ButtonGroup GameModeButtons = new ButtonGroup();
GameModeButtons.add(SimpleGameButton);
GameModeButtons.add(GeneralGameButton);
// BOARD SIZE BUTTONS - SMALL(5X5) OR LARGE(8X8)
JLabel SizeOptionLabel = new JLabel("Board Size :");
JRadioButton SmallGridButton = new JRadioButton("Small", true);
JRadioButton LargeGridButton = new JRadioButton("Large");
ButtonGroup GridSizeButtons = new ButtonGroup();
GridSizeButtons.add(SmallGridButton);
GridSizeButtons.add(LargeGridButton);
// PLAY LETTER SETTINGS
JRadioButton PlayS_Button = new JRadioButton("S", true);
JRadioButton PlayO_Button = new JRadioButton("O");
ButtonGroup PlayLetterButtons = new ButtonGroup();
PlayLetterButtons.add(PlayS_Button);
PlayLetterButtons.add(PlayO_Button);
// BLUE PLAYER SETTINGS
JLabel BluePlayerLabel = new JLabel("Blue Player");
JRadioButton BlueHumanButton = new JRadioButton("Human", true);
JRadioButton BlueComputerButton = new JRadioButton("Computer");
ButtonGroup BluePlayerButtons = new ButtonGroup();
BluePlayerButtons.add(BlueHumanButton);
BluePlayerButtons.add(BlueComputerButton);
// RED PLAYER SETTINGS
JLabel RedPlayerLabel = new JLabel("Red Player");
JRadioButton RedHumanButton = new JRadioButton("Human");
JRadioButton RedComputerButton = new JRadioButton("Computer", true);
ButtonGroup RedPlayerButtons = new ButtonGroup();
RedPlayerButtons.add(RedHumanButton);
RedPlayerButtons.add(RedComputerButton);
// ADDING COMPONENTS TO TOP PANEL
TopPanel.add(GameModeLabel);
TopPanel.add(SimpleGameButton);
TopPanel.add(GeneralGameButton);
TopPanel.add(SpacerLabel);
TopPanel.add(SizeOptionLabel);
TopPanel.add(SmallGridButton);
TopPanel.add(LargeGridButton);
// ADDING COMPONENTS TO BOTTOM PANEL
BottomPanel.add(PlayS_Button);
BottomPanel.add(PlayO_Button);
// ADDING COMPONENTS TO LEFT PANEL
LeftPanel.add(BluePlayerLabel);
LeftPanel.add(BlueHumanButton);
LeftPanel.add(BlueComputerButton);
// ADDING COMPONENTS TO RIGHT PANEL
RightPanel.add(RedPlayerLabel);
RightPanel.add(RedHumanButton);
RightPanel.add(RedComputerButton);
// ADDING PANELS TO CONTENT PANE
ContentPane.setLayout(new BorderLayout());
ContentPane.add(TopPanel, BorderLayout.NORTH);
ContentPane.add(BottomPanel, BorderLayout.SOUTH);
ContentPane.add(LeftPanel, BorderLayout.WEST);
ContentPane.add(RightPanel, BorderLayout.EAST);
ContentPane.add(gameBoardCanvas, BorderLayout.CENTER);
TopPanel.setPreferredSize(new Dimension(50, 50));
BottomPanel.setPreferredSize(new Dimension(50, 50));
LeftPanel.setPreferredSize(new Dimension(100, 100));
RightPanel.setPreferredSize(new Dimension(100, 100));
ContentPane.setPreferredSize(new Dimension(550, 500));
}
// CLASS SETTING UP HOW THE GRID WILL BE CREATED
class Grid extends JPanel {
public static final int CELL_SIZE = 1;
private int xPos;
private int yPos;
public JLabel gridLabel;
public Grid (int x, int y) {
xPos = x;
yPos = y;
gridLabel = new JLabel("");
gridLabel.setFont(new Font("Serif", Font.BOLD, 40));
add(gridLabel);
setOpaque(true);
setLayout(new FlowLayout());
setBorder(BorderFactory.createBevelBorder(CELL_SIZE));
setBackground(new Color(200, 200, 200));
setPreferredSize(new Dimension(CELL_SIZE, CELL_SIZE));
}
}
/* POSSIBLE FUNCTION TO SET GRID_SIZE BASED OFF RADIO BUTTON INPUT? DOESNT WORK HERE
public getGridSize() {
if (GameBoard().SmallGridButton.isSelected() == true) {
GRID_SIZE = 5;
}
else if (GameBoard().LargeGridButton.isSelected() == true) {
GRID_SIZE = 8;
}
return GRID_SIZE;
}
*/
public static void main(String[] args) {
new SOS_GUI();
}
}
screenshot of the smaller 5x5 grid
screenshot of larger 8x8 grid
Again, I suggest that if you want to use components as your grid cell, that you either swap views (JPanels) using a CardLayout, or you swap out the grid cells when a JRadioButton is pressed.
I suggest:
Adding an ActionListener to the JRadioButton to be notified when it is pressed.
If you will swap components, then create a JPanel to hold the grid cells, say called gridHolder, and remove all components when the button is pressed.
Then add a new GridLayout layout manager to this JPanel, with constraints set depending on whih JRadioButton has been pressed.
Then re-adding grid cell components to this holder JPanel
Then relaying out all components in the GUI and resizing it by calling pack() on the top-level window, here a JFrame.
In the example below, I use JLabels to hold the grid cells since it is trivial to add text to these.
I store the row and column of each grid cell using the .putClientProperty(...) method and likewise can retrieve values using the .getClientProperty(...) method.
I call createGrid(...) in the constructor to create the grid with the default, small, size.
I call the same method whenever a JRadioButton has been pressed.
For example:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Window;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;
import javax.swing.border.BevelBorder;
#SuppressWarnings("serial")
public class SosGrid2 extends JPanel {
private static final int SMALL_SIZE = 5;
private static final int LARGE_SIZE = 8;
private static final String[] SIZES = { "Small", "Large" };
private static final Dimension CELL_SIZE = new Dimension(60, 60);
private static final Color GRID_BG = new Color(200, 200, 200);
private static final String ROW = "row";
private static final String COL = "col";
private JPanel gridHolder = new JPanel();
private ButtonGroup gridSizeGroup = new ButtonGroup();
public SosGrid2() {
JPanel radioButtonPanel = new JPanel();
for (String size : SIZES) {
JRadioButton radioButton = new JRadioButton(size);
radioButton.setSelected(size.equals(SIZES[0]));
radioButton.setActionCommand(size);
gridSizeGroup.add(radioButton);
radioButtonPanel.add(radioButton);
radioButton.addActionListener(e -> radioListener());
}
createGrid(SMALL_SIZE);
setLayout(new BorderLayout());
add(gridHolder);
add(radioButtonPanel, BorderLayout.PAGE_END);
}
private void createGrid(int gridSize) {
gridHolder.removeAll();
gridHolder.setLayout(new GridLayout(gridSize, gridSize));
for (int row = 0; row < gridSize; row++) {
for (int col = 0; col < gridSize; col++) {
JLabel gridCell = createGridCell(row, col);
gridHolder.add(gridCell);
}
}
}
// factory method to create grid cell JLabels.
private JLabel createGridCell(int row, int col) {
JLabel label = new JLabel("", SwingConstants.CENTER);
label.setFont(label.getFont().deriveFont(Font.BOLD, 32f));
label.setOpaque(true);
label.setBackground(GRID_BG);
label.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
label.setPreferredSize(CELL_SIZE);
label.putClientProperty(ROW, row);
label.putClientProperty(COL, col);
label.addMouseListener(new MyMouseListener());
return label;
}
private class MyMouseListener extends MouseAdapter {
#Override
public void mousePressed(MouseEvent e) {
JLabel gridCell = (JLabel) e.getSource();
int row = (int) gridCell.getClientProperty(ROW);
int col = (int) gridCell.getClientProperty(COL);
String message = String.format("Row: %d, Col: %d", row, col);
String title = "Cell Pressed";
int type = JOptionPane.PLAIN_MESSAGE;
JOptionPane.showMessageDialog(SosGrid2.this, message, title, type);
String text = gridCell.getText();
if (text.isEmpty()) {
gridCell.setText("X");
} else {
gridCell.setText("");
}
}
}
private void radioListener() {
ButtonModel btnModel = gridSizeGroup.getSelection();
if (btnModel != null) {
int gridSize = btnModel.getActionCommand().equals(SIZES[0]) ? SMALL_SIZE : LARGE_SIZE;
createGrid(gridSize);
Window jframe = SwingUtilities.getWindowAncestor(this);
jframe.pack();
jframe.setLocationRelativeTo(null);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
SosGrid2 mainPanel = new SosGrid2();
JFrame frame = new JFrame("GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});
}
}
i'm learning JSwing and i discovered the GridBagLayout.
I'm trying to create a simple calculator, i did it with adding multiple JPanel setting each preferedSize but when i resize the window frame the panels won't resize too.
Then i found out the GridBagLayout.
But this i what i get: Wrong calculator with GridBagLayout
import javax.swing.*;
import java.awt.*;
public class Calc extends JFrame {
private final int WIDTH = 300;
private final int HEIGHT = 450;
public Calc(){
setSize(WIDTH, HEIGHT);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
mainPanel.add(createButtons(), BorderLayout.SOUTH);
add(mainPanel);
}
private JPanel createButtons(){
JPanel panel = new JPanel();
GridBagLayout layout = new GridBagLayout();
panel.setLayout(layout);
GridBagConstraints g = new GridBagConstraints();
g.gridx = 0;
g.gridy = 0;
for(int i = 0; i < 9; i++){
panel.add(new JButton(""+i), g);
g.gridx++;
if(g.gridx == 3) {
g.gridx = 0;
g.gridy++;
}
}
return panel;
}
public static void main(String... args){
Calc calc = new Calc();
calc.setVisible(true);
}
}
it should be something like this:
Right calculator
i tried:
to set an anchor... but it doesn't work,
to create multiple JPanel(one with GridLayout) but doesn't work
if you don't want to spoon code, it's okay.. but from where should i start?
Edit:
I figure out how to arrange the buttons... but i can't set the header to fill all the x-axis:
Code:
import javax.swing.*;
import java.awt.*;
public class ButtonPanel extends JPanel {
JPanel top;
JPanel left;
JPanel right;
private class CButton extends JButton{
private Operation operation;
public CButton(){
}
}
public ButtonPanel(){
initComponent();
initLayout();
}
private void initLayout() {
GridBagLayout layout = new GridBagLayout();
this.setLayout(layout);
layout.columnWeights = new double[] {3,1};
layout.rowWeights = new double[] {1, 1};
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
c.fill = GridBagConstraints.BOTH;
c.weightx = 5;
this.add(top, c);
c.gridy++;
c.weighty=1;
this.add(left, c);
c.gridx++;
this.add(right, c);
}
private void initComponent() {
top = new JPanel();
top.setLayout(new GridLayout(1, 3));
for(int i = 0; i < 3; i++){
top.add(new JButton("bbb"));
}
left = new JPanel();
left.setLayout(new GridLayout(3,3));
for(int i = 0; i < 9; i++){
left.add(new JButton(""+i));
}
right = new JPanel();
right.setLayout(new GridLayout(3,1));
for(int i = 0; i < 3; i++){
JButton btn = new JButton("aa");
right.add(btn);
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("test");
frame.setLayout(new BorderLayout());
frame.add(new ButtonPanel(), BorderLayout.SOUTH);
frame.setSize(300, 450);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
It should be: Image
You can probably do everything within a single panel, having some buttons that span over multiple columns.
So I give you a different example to layout buttons using a single GridBagLayout, here you can define your button arrangement as an array of values, check if it could be a good starting point for your project.
package test;
import static test.Calculator.Buttons.*;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Calculator extends JPanel {
//Here define all possible buttons with labels
public enum Buttons {
PERCENT("%"), CE("CE"), CLEAR("C"),
ONE("1"), TWO("2"), THREE("3"), FOUR("4"), FIVE("5"), SIX("6"), SEVEN("7"), EIGHT("8"), NINE("9"), ZERO("0"),
ADD("+"), SUB("-"), MULT("x"), DIV("/"), RESULT("="), DECPOINT(".");
protected String text;
Buttons(String txt) {
this.text=txt;
}
public String getText() {
return text;
}
};
//This array contains your keypad layout, contiguous repeated elements will span across multiple columns (e.g. ZERO).
protected Buttons[][] keyPad = {
{PERCENT, CE, CLEAR, DIV},
{SEVEN, EIGHT, NINE, MULT},
{FOUR, FIVE, SIX, ADD},
{ONE, TWO, THREE, SUB},
{ZERO, ZERO, DECPOINT, RESULT}
};
Map<JButton, Buttons> sourceMap=new HashMap<>();
ActionListener padListener=new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
onKeyPadPressed(sourceMap.get(e.getSource()));
}
};
public Calculator() {
setLayout(new GridBagLayout());
GridBagConstraints c=new GridBagConstraints();
c.weightx=1.0;
c.weighty=1.0;
c.fill=GridBagConstraints.BOTH;
for (int y=0;y<keyPad.length;y++) {
for (int x=0;x<keyPad[y].length;) {
Buttons b=keyPad[y][x];
if (b==null) {
continue;
}
JButton btn=new JButton(b.getText());
c.gridx=x;
c.gridy=y;
c.gridwidth=0;
while(x<keyPad[y].length&&keyPad[y][x]==b) {
c.gridwidth++;
x++;
}
add(btn,c);
sourceMap.put(btn,b);
btn.addActionListener(padListener);
}
}
}
//Callback method, whenever a button is clicked you get the associated enum value here
protected void onKeyPadPressed(Buttons b) {
System.out.println("Pressed "+b);
switch (b) {
// case ZERO:
// .... here your logic
}
}
public static void main(String[] args) {
JFrame frame=new JFrame("Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new Calculator());
frame.pack();
frame.setVisible(true);
}
}
The code above produces this result, but it's really easy to add/remove buttons and change the layout.
I have a JPanel and I'm adding 25 JPanels to the inside in a grid. I want a border around each panel so you can clearly distinguish between each element. Padding would work as well. The way I'm adding panels to the board if I try to add a border it will apply it to the larger panel containing the elements instead.
public class LightsOutView
{
GridLayout experimentLayout = new GridLayout(5, 5);
// Creates layout of the GUI
public LightsOutView ()
{
JFrame frame = new JFrame();
frame.setTitle("Lights Out");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setContentPane(makeContents());
frame.setVisible(true);
}
/**
* Creates the blank game board. Returns the panel
*
* #return JPanel
*/
public JPanel makeContents ()
{
// Create a panel to hold the 5x5 grid
JPanel board = new JPanel(new GridLayout(5, 5));
board.setLayout(experimentLayout);
// Add the components to the panel
for (int i = 0; i < 25; i++)
{
board.add(new JPanel()).setBackground(Color.YELLOW);
}
// Return the panel
return board;
}
}
How do I add a border around each element. Will I need to change how I'm adding panels to the grid?
One way:
Change your GridLayout to something like:
GridLayout experimentLayout = new GridLayout(5, 5, gap, gap);
where gap is some small int value, say 1 to 3 (for pixels). Then give the background container a background color, and it will show through the gaps. You'll also want to give the background JPanel a line border with the same gap width.
For example:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.*;
#SuppressWarnings("serial")
public class GridWithBorders extends JPanel {
private static final int SIDES = 6;
private static final int SIDE_LENGTH = 60;
private static final int GAP = 3;
private static final Color BG = Color.BLACK;
private static final Color CELL_COLOR = Color.GREEN.darker();
public GridWithBorders() {
setBackground(BG);
setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
setLayout(new GridLayout(SIDES, SIDES, GAP, GAP));
Dimension prefSize = new Dimension(SIDE_LENGTH, SIDE_LENGTH);
for (int i = 0; i < SIDES; i++) {
for (int j = 0; j < SIDES; j++) {
JPanel cell = new JPanel();
cell.setBackground(CELL_COLOR);
cell.setPreferredSize(prefSize);
add(cell);
}
}
}
private static void createAndShowGui() {
GridWithBorders mainPanel = new GridWithBorders();
JFrame frame = new JFrame("GridWithBorders");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}
I have a array of JButtons that do not want to be visible unless I add another JButton before the loop for the array of buttons.
Window class:
import javax.swing.*;
import java.awt.*;
public class Window extends JFrame {
private Container mContainer = new Container();
public Window()
{
super();
this.setTitle("Calculator");
this.setSize(200, 300);
this.setLocationRelativeTo(null);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mContainer.setBorder(null);
mContainer.setBackground(Color.GRAY);
mContainer.setOpaque(true);
this.setContentPane(mContainer);
//Panels
JPanel panel = new JPanel();
JPanel center = new JPanel();
center.setBackground(Color.GRAY);
center.setBorder(null);
JPanel displayOutput = new JPanel();
displayOutput.setBackground(Color.GRAY);
this.getContentPane().add(panel);
//TextArea
JTextArea textArea = new JTextArea(1, 20);
textArea.setForeground(Color.WHITE);
textArea.setBackground(Color.GRAY);
textArea.setPreferredSize(new Dimension(200, 60));
//Panel Layouts
panel.setLayout(new BorderLayout());
center.setLayout(new GridLayout(5, 4, 2, 2));
//Add other panel elements
displayOutput.add(textArea);
panel.add(displayOutput, BorderLayout.NORTH);
panel.add(center, BorderLayout.CENTER);
//For some reason adding this makes the array of buttons appear
JButton btnNewButton = new JButton("1");
center.add(btnNewButton);
//Create buttons
for(int i = 0; i < 20; i++){
CalcButtons cButtons[] = new CalcButtons[20];
cButtons[i] = new CalcButtons();
//Add buttons to center box below output
center.add(cButtons[i]);
//Sets fourth column of buttons in cyan.
if(((i + 1) % 4) == 0){
cButtons[i].setBackground(Color.CYAN);
}
}
//Add panel to window
getContentPane().add(panel);
this.setVisible(true);
}
}
Container Class:
import java.awt.Graphics;
import javax.swing.JPanel;
public class Container extends JPanel {
public Container() {
super();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
}
}
Button Class:
import javax.swing.*;
import java.awt.Color;
public class CalcButtons extends JButton
{
public CalcButtons()
{
this.setBackground(Color.WHITE);
this.setBorder(null);
}
}
This code then produced this:
However if I remove this:
//For some reason adding this makes the array of buttons appear
JButton btnNewButton = new JButton("1");
center.add(btnNewButton);
It produces this:
The problem is the size of your buttons are 0. You have set the border to null and there is no icon or text set. So when GridLayout calls getPreferredSize() on your buttons they return a size of 0,0 (width, height). When you add the JButton with a 1 in it then all of sudden one component has size. Since Gridlayout makes all components the same size you now can see your buttons. If you want your buttons to be blank but also be drawn set your border to an empty border with a size of 1 or greater on each edge.
I have a JScrollPane with a very high JPanel inside, that is changed dynamically, items being appended at its end. What I want, is to scroll to the bottom of aforementioned JScrollPane in order for the newly appended items to be visible instantly on addition (they are not appended to the scroll pane directly, but to its JPanel, and are private objects, so cannot be referenced.
How can I simply have that scroll pane scroll to the very bottom?
Thanks in advance!
JComponent.scrollRectToVisible(Rectangle). Call that on the JPanel instance.
E.G.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class ScrollToNewLabel {
public static void main(String[] args) {
SwingUtilities.invokeLater( new Runnable() {
public void run() {
JPanel gui = new JPanel(new BorderLayout(3,3));
final JPanel panel = new JPanel(new GridLayout(0,1));
JScrollPane scroll = new JScrollPane(panel);
scroll.setPreferredSize(new Dimension(80,100));
gui.add(scroll, BorderLayout.CENTER);
JButton addLabel = new JButton("Add Label");
gui.add(addLabel, BorderLayout.NORTH);
ActionListener listener = new ActionListener() {
int counter = 0;
public void actionPerformed(ActionEvent ae) {
panel.add(new JLabel("Label " + ++counter));
panel.revalidate();
int height = (int)panel.getPreferredSize().getHeight();
Rectangle rect = new Rectangle(0,height,10,10);
panel.scrollRectToVisible(rect);
}
};
addLabel.addActionListener(listener);
JOptionPane.showMessageDialog(null, gui);
}
});
}
}
Screen shot
E.G. 2
This e.g. is based on Vincent's answer, to use JScrollPane.getVerticalScrollBar().setValue(height). Where height is the preferred height of the panel in pixels.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class ScrollToNewLabel {
public static void main(String[] args) {
SwingUtilities.invokeLater( new Runnable() {
public void run() {
JPanel gui = new JPanel(new BorderLayout(3,3));
final JPanel panel = new JPanel(new GridLayout(0,1));
final JScrollPane scroll = new JScrollPane(panel);
scroll.setPreferredSize(new Dimension(80,100));
gui.add(scroll, BorderLayout.CENTER);
JButton addLabel = new JButton("Add Label");
gui.add(addLabel, BorderLayout.NORTH);
ActionListener listener = new ActionListener() {
int counter = 0;
public void actionPerformed(ActionEvent ae) {
panel.add(new JLabel("Label " + ++counter));
panel.revalidate();
int height = (int)panel.getPreferredSize().getHeight();
scroll.getVerticalScrollBar().setValue(height);
}
};
addLabel.addActionListener(listener);
JOptionPane.showMessageDialog(null, gui);
}
});
}
}
scrollRectToVisible(...) and scrollBar.setValue(...) are the general solutions.
You may be interested in Scrolling a Form which ensures that when you tab to a component the form will scroll automatically to make sure the the component will be visible in the scrollpane. Behind the scenes it uses scrollRectToVisible().
A simple way to move the scrollbar all the way to the bottom is to set its value to 100 like this:
scroll.getVerticalScrollBar().setValue(100);
This causes it to move to the bottom of the viewport. You can add this after the component is added to the panel.
This is how I scroll down programmatically.
I like how it scrolls to the bottom rather smoothly instead of jumping there immediately.
/**
* Scrolls a {#code scrollPane} to its bottom.
*
* #param scrollPane
* the scrollPane that we want to scroll all the way down
*
*/
private void scrollDown(JScrollPane scrollPane) {
JScrollBar verticalBar = scrollPane.getVerticalScrollBar();
int currentScrollValue = verticalBar.getValue();
int previousScrollValue = -1;
while (currentScrollValue != previousScrollValue) {
// Scroll down a bit
int downDirection = 1;
int amountToScroll = verticalBar.getUnitIncrement(downDirection);
verticalBar.setValue(currentScrollValue + amountToScroll);
previousScrollValue = currentScrollValue;
currentScrollValue = verticalBar.getValue();
}
}