I am trying to build a matching game with icons attached to each button. Although, this isn't close to being finished, I have a problem with filling the panel with buttons.
With this code I get a grey colored frame. If i comment out the 3 methods i use under "//execution" the panel will be all black (which is how i am testing to see if the buttons are filling the space or not.)
For some reaso,n my buttons aren't being populated onto the panel.
I need some help!!! Where am I going wrong?
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MemoryMainFrame extends JFrame implements ActionListener {
JButton button[] = new JButton[16];
private static final int SIXTEEN_BUTTONS = 16;
JPanel mainPanel = new JPanel();
double dim = Math.sqrt(SIXTEEN_BUTTONS);
int numOfColumns = (int) (dim);
int numOfRows = (int) (dim);
public static void main(String[] args) {
new MemoryMainFrame();
}
public MemoryMainFrame() {
this.setTitle("MemoryGame!!!");
this.setSize(400, 400);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
mainPanel.setBackground(Color.BLACK);
mainPanel.setLayout(new GridBagLayout());
// execution
FillButtonArray(SIXTEEN_BUTTONS);
AddButtonListener(SIXTEEN_BUTTONS);
ButtonToPanel(SIXTEEN_BUTTONS);
this.add(mainPanel);
}
public void FillButtonArray(int numOfButtons) {
int i = 0;
while (i < numOfButtons) {
button[i] = new JButton("asdf");
}
}
public void AddButtonListener(int numOfButtons) {
int i = 0;
while (i < numOfButtons) {
button[i].addActionListener(this);
}
}
public void ButtonToPanel(int numOfButtons) {
int n = 0;
GridBagConstraints gbc = new GridBagConstraints();
for (int i = 0; i < numOfColumns; i++) {
for (int j = 0; j < numOfRows; j++) {
gbc.gridx = i;
gbc.gridy = j;
n++;
button[n].setBorder(BorderFactory.createLineBorder(
Color.DARK_GRAY, 2));
mainPanel.add(button[n]);
}
}
}
public void actionPerformed(ActionEvent arg0) {
JFrame j = new JFrame();
j.setSize(300, 300);
j.setVisible(true);
}
}
I use the "asdf" as a test to see if the buttons work as well.
also, the actionperformed was a test as well. That part of the code is irrelevant.
You're creating your GridBagConstraints but you're not using them.
Change this:
mainPanel.add(button[n]);
to this:
// passes both the component and the GBC into the container
mainPanel.add(button[n], gbc);
Edit
You've also got a never-ending loop here:
while (i < numOfButtons) {
button[i] = new JButton("asdf");
}
and likewise for the AddButtonListener(...) method.
You'll want to fix this by using either a for loop or else changing i within the loop.
Also per Andrew Thompson's comment, you're setting the JFrame visible too early, before all components have been added.
Also your use of Math.sqrt then casting the result to int is very risky and risks getting unexpected results. Just declare the side length to 8 and square the int if you need to.
For an example of GridBagLayout, please check out:
import java.awt.Color;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import javax.swing.*;
#SuppressWarnings("serial") // avoid extending JFrame if possible
public class MemoryMainPanel extends JPanel {
private static final int ROWS = 8;
private static final Color BACKGROUND = Color.black;
private static final int I_GAP = 5;
private static final Insets BTN_INSETS = new Insets(I_GAP, I_GAP, I_GAP, I_GAP);
private JButton[][] buttons = new JButton[ROWS][ROWS];
public MemoryMainPanel() {
setBackground(BACKGROUND);
setLayout(new GridBagLayout());
for (int row = 0; row < buttons.length; row++) {
for (int col = 0; col < buttons[row].length; col++) {
JButton btn = new JButton(new ButtonAction(row, col));
add(btn, createGbc(row, col));
buttons[row][col] = btn;
}
}
}
private GridBagConstraints createGbc(int y, int x) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = x;
gbc.gridy = y;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.insets = BTN_INSETS;
return gbc;
}
private class ButtonAction extends AbstractAction {
private int row;
private int col;
public ButtonAction(int row, int col) {
super("asdf");
this.row = row;
this.col = col;
}
#Override
public void actionPerformed(ActionEvent e) {
String text = String.format("Column, Row: [%d, %d]", col + 1, row + 1);
Component parentComponent = MemoryMainPanel.this;
String message = text;
String title = "Button Pressed";
int messageType = JOptionPane.PLAIN_MESSAGE;
Icon icon = null;
JOptionPane.showMessageDialog(parentComponent, message, title, messageType, icon);
}
}
private static void createAndShowGui() {
MemoryMainPanel mainPanel = new MemoryMainPanel();
JFrame frame = new JFrame("MemoryMainPanel");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
Related
I am working with a list of elements that I want to rank. Ideally the program would randomly choose 2 elements to compare and present them, sort the list with the new ranking, then select 2 more so the user could pick the winner over and over again until the user wanted to stop. The parts I am having trouble with are:
getting the button to interact with the object to change its rank and
getting the JFrame to close and reopen with new elements to compare
I created a class called action that is supposed to handle the clicks:
public class action extends JFrame implements ActionListener{
private JButton b1;
private JButton b2;
private JButton b3;
private JPanel jp;
public action(Bond one, Bond two){
//JFrame mf = new JFrame("My Frame");
//JPanel jp = new JPanel();
jp = new JPanel();
setSize(500,500);
setVisible(true);
setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
b1 = new JButton(one.name+"\nRating: "+one.rating);
b1.addActionListener(this);
b2 = new JButton(two.name);
b2.addActionListener(this);
b3 = new JButton("Tie");
b3.addActionListener(this);
jp.add(b1);
jp.add(b2);
jp.add(b3);
add(jp);
}
public void actionPerformed(ActionEvent e){
int clickCount = 0;
if (clickCount > 3) {
System.exit(0);
}
JOptionPane jo = new JOptionPane();
if(e.getSource() == b1){
// jo.showMessageDialog(null, "button 1");
clickCount++;
//System.exit(0);
}
if(e.getSource() == b2){
jo.showMessageDialog(null, "button 2");
clickCount++;
//System.exit(0);
}
if(e.getSource() == b3){
jo.showMessageDialog(null, "button 3");
clickCount++;
//System.exit(0);
}
}
}
And here is how I am calling the action class from my main:
while(compCount < 3){
//these generate random numbers that correspond with the index of the elements to be compared
//comp is the name of the list of objects
int r1 = r.nextInt(comp.size());
int r2 = r.nextInt(comp.size());
int t;
if(r1 == r2){
continue;
}
else{
action a = new action(comp.get(r1), comp.get(r2));
compCount++;
}
}
Currently this just creates 3 popup windows that do nothing when I click the buttons.
Any help or insight would be appreciated!
The OP wants to make a complex Swing GUI. He didn't provide a minimal, runnable, example, so I created my own.
The best way to make a complex Swing GUI is to construct it one small piece at a time. By testing each small piece in isolation, you too can construct a complex GUI.
Here's the GUI I created.
The object of this game is to choose your favorite characters from the lower case characters of the English alphabet. You can left-click on the Pass button if neither character is among your favorites.
I'd already written the JPanel to display a character for a previous Stack Overflow answer.
The first thing I did was create the main class. I named this class FavoriteCharacter.
I called the SwingUtilities invokeLater method to ensure that the Swing components were created and executed on the Event Dispatch Thread.
Next, I wrote the JFrame code. The JFrame code is nearly identical for every Swing GUI I create. The JFrame code is located in the run method of the FavoriteCharacter class.
Next, I wrote the model class, the LetterMap class. Whenever I create a complex Swing GUI, I use the model / view / controller pattern. This model is pretty simple. I have a map that contains the lower case characters of the English alphabet, along with vote counts.
Once I had the model working, I went back to creating the view. Each small part of the GUI is contained in a method of the FavoriteCharacter class. That way, I could test each small piece of the view individually. No throwing spaghetti code against the wall and seeing what sticks for me.
The actual drawing of the characters happens in a LetterPanel class. All Swing custom painting must be done in the paintComponent method of a JPanel.
After I finished the GUI, I worked on the two controller classes, PassListener and VoteListener. PassListener calls a method in the FavoriteCharacters class to select another pair of characters. VoteListener updates the LetterMap model class, displays the vote total for the selected character in a JOptionPane, and selects another pair of letters.
Here's the runnable, example code.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.TreeMap;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class FavoriteCharacter implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new FavoriteCharacter());
}
private char[] letterPair;
private JFrame frame;
private LetterMap letterMap;
private LetterPanel letterPanel1;
private LetterPanel letterPanel2;
public FavoriteCharacter() {
this.letterMap = new LetterMap();
this.letterPair = letterMap.pickTwo();
}
#Override
public void run() {
frame = new JFrame("Favorite Character");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createTitlePanel(),
BorderLayout.BEFORE_FIRST_LINE);
frame.add(createMainPanel(letterPair),
BorderLayout.CENTER);
frame.add(createSkipPanel(),
BorderLayout.AFTER_LAST_LINE);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel createTitlePanel() {
JPanel panel = new JPanel();
JLabel label = new JLabel("Vote for your favorite character");
label.setHorizontalAlignment(JLabel.CENTER);
panel.add(label);
return panel;
}
private JPanel createMainPanel(char[] letterPair) {
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.LINE_AXIS));
panel.add(Box.createHorizontalStrut(10));
letterPanel1 = new LetterPanel(Color.WHITE, letterPair[0]);
panel.add(createLetterPanel(letterPanel1, "0"));
panel.add(Box.createHorizontalStrut(10));
letterPanel2 = new LetterPanel(Color.WHITE, letterPair[1]);
panel.add(createLetterPanel(letterPanel2, "1"));
panel.add(Box.createHorizontalStrut(10));
return panel;
}
private void updateLetterPanels() {
letterPanel1.setLetter(letterPair[0]);
letterPanel2.setLetter(letterPair[1]);
letterPanel1.repaint();
letterPanel2.repaint();
}
private void updateLetterPair() {
letterPair = letterMap.pickTwo();
System.out.println(Arrays.toString(letterPair));
updateLetterPanels();
}
private JPanel createLetterPanel(LetterPanel letterPanel,
String actionCommand) {
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets = new Insets(0, 10, 10, 10);
gbc.weightx = 0d;
panel.add(letterPanel, gbc);
gbc.gridy++;
gbc.weightx = 1d;
JButton button = new JButton("Vote");
button.setActionCommand(actionCommand);
button.setHorizontalAlignment(JButton.CENTER);
button.addActionListener(new VoteListener());
panel.add(button, gbc);
return panel;
}
private JPanel createSkipPanel() {
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets = new Insets(0, 18, 10, 18);
gbc.weightx = 1d;
JButton button = new JButton("Pass");
button.setHorizontalAlignment(JButton.CENTER);
button.addActionListener(new PassListener());
panel.add(button, gbc);
return panel;
}
public class LetterPanel extends JPanel {
private static final long serialVersionUID = 1L;
private char letter;
private Color backgroundColor;
private Font font;
public LetterPanel(Color backgroundColor, char letter) {
this.backgroundColor = backgroundColor;
this.letter = letter;
this.font = getFont().deriveFont(96f)
.deriveFont(Font.BOLD);
this.setBorder(BorderFactory.createLineBorder(
Color.GREEN, 6));
this.setPreferredSize(new Dimension(120, 200));
}
public void setLetter(char letter) {
this.letter = letter;
}
public char getLetter() {
return letter;
}
public LetterPanel getLetterPanel() {
return this;
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(backgroundColor);
g2d.fillRect(0, 0, getWidth(), getHeight());
g2d.setColor(Color.BLACK);
drawCenteredString(g2d, Character.toString(letter),
font);
}
/**
* Draw a String centered in the middle of the panel.
*
* #param g2d The Graphics2D instance.
* #param text The String to draw.
* #param font The Font to draw with.
*/
public void drawCenteredString(Graphics2D g2d,
String text, Font font) {
FontMetrics metrics = g2d.getFontMetrics(font);
int x = (getWidth() - metrics.stringWidth(text)) / 2;
int y = ((getHeight() - metrics.getHeight()) / 2) +
metrics.getAscent();
g2d.setFont(font);
g2d.drawString(text, x, y);
}
}
public class PassListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent event) {
updateLetterPair();
}
}
public class VoteListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent event) {
int index = Integer.valueOf(event.getActionCommand());
char c = letterPair[index];
letterMap.addVote(c);
int votes = letterMap.getVotes(c);
showMessageDialog(c, votes);
}
private void showMessageDialog(char c, int votes) {
String text = "The character '" + c + "' has ";
text += Integer.toString(votes);
if (votes == 1) {
text += " vote.";
} else {
text += " votes.";
}
JOptionPane.showMessageDialog(frame, text);
updateLetterPair();
}
}
public class LetterMap {
private Map<Character, Integer> letterMap;
private Random random;
public LetterMap() {
this.letterMap = createLetterMap();
this.random = new Random();
}
private Map<Character, Integer> createLetterMap() {
Map<Character, Integer> letterMap = new TreeMap<>();
for (int i = 0; i < 26; i++) {
Character c = (char) (i + 'a');
letterMap.put(c, 0);
}
return letterMap;
}
public char[] pickTwo() {
int index1 = random.nextInt(letterMap.size());
int index2 = random.nextInt(letterMap.size());
while (index2 == index1) {
index2 = random.nextInt(letterMap.size());
}
char[] output = new char[2];
Set<Character> letterSet = letterMap.keySet();
Iterator<Character> iter = letterSet.iterator();
int count = 0;
int index3 = 0;
while (iter.hasNext() && index3 < 2) {
Character key = iter.next();
if (count == index1 || count == index2) {
if (index3 < 2) {
output[index3++] = key;
}
}
count++;
}
return output;
}
public void addVote(char c) {
Integer vote = getVotes(c);
letterMap.put(c, ++vote);
}
public int getVotes(char c) {
return letterMap.get(c);
}
}
}
I have a grid layout that is 9x9 and generates Jtextareas to fill it. If the user presses a button i want the grid layout to become empty again so i can refill it again but with no relation to what it previously was filled with.
is there some sort of command like gridlayout.delete() or something?
I'm guessing that you want to clear the text components that are held by the GridLayout-using container (you don't tell us, and please understand that this is key information about your question). If so, put them into a collection such as an ArrayList and iterate through the list calling setText("") within the loop.
If you're using Java 8, then this "for loop" can be replaced with a forEach(...) call on a Stream. For example, if you have an ArrayList like so:
List<JTextComponent> textComponentList = new ArrayList<>();
Then you can clear all the text components it holds with this call:
textComponentList.stream().forEach(tc -> tc.setText(""));
For example:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
import javax.swing.text.JTextComponent;
#SuppressWarnings("serial")
public class ClearGrid extends JPanel {
private static final int ROWS = 9;
private static final int COLS = ROWS;
private static final int GAP = 2;
private static final Font FONT = new Font(Font.SANS_SERIF, Font.BOLD, 32);
private static final int FIELD_COLS = 2;
List<JTextComponent> textComponentList = new ArrayList<>();
public ClearGrid() {
JPanel gridPanel = new JPanel(new GridLayout(ROWS, COLS, GAP, GAP));
gridPanel.setBackground(Color.BLACK);
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
JTextField textField = new JTextField(FIELD_COLS);
textField.setFont(FONT);
textField.setHorizontalAlignment(JTextField.CENTER);
textComponentList.add(textField);
gridPanel.add(textField);
}
}
JPanel buttonPanel = new JPanel();
buttonPanel.add(new JButton(new ClearAllAction("Clear All", KeyEvent.VK_C)));
setLayout(new BorderLayout());
add(gridPanel);
add(buttonPanel, BorderLayout.PAGE_END);
}
private class ClearAllAction extends AbstractAction {
public ClearAllAction(String name, int mnemonic) {
super(name);
putValue(MNEMONIC_KEY, mnemonic);
}
#Override
public void actionPerformed(ActionEvent e) {
textComponentList.stream().forEach(tc -> tc.setText(""));
}
}
private static void createAndShowGui() {
JFrame frame = new JFrame("ClearGrid");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new ClearGrid());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
createAndShowGui();
});
}
}
So here is my code, which displays a 9x9 grid:
import javax.swing.*;
import java.awt.*;
public class SudokuGrid extends JFrame {
private static final int ROWS = 9;
private static final int COLUMNS = 9;
int fontSize = 30;
public static void main(String[] args) {
SudokuGrid makeSudokuGrid = new SudokuGrid();
} // end of main
// constructor SudokuGrid
public SudokuGrid() {
JTextField[][] inputBoxes = new JTextField[ROWS][COLUMNS];
Font font = new Font("Helvetica", Font.BOLD, fontSize);
setLayout(new GridLayout(ROWS, COLUMNS));
// set frame size
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// outer loop to create the rows
for (int rows = 0 ; rows < ROWS ; rows++) {
// inner loop to create the columns
for (int columns = 0 ; columns < COLUMNS ; columns++) {
// make text fields empty
inputBoxes[rows][columns] = new JTextField("");
// add text fields to the frame
add(inputBoxes[rows][columns]);
// center text in each text box
inputBoxes[rows][columns].setHorizontalAlignment(JTextField.CENTER);
// apply font to each text box
inputBoxes[rows][columns].setFont(font);
} // end of columns loop
} // end of rows loop
// make frame visible
getContentPane().setBackground(Color.RED);
setVisible(true);
} // end of constructor SudokuGrid
} // end of class SudokuGrid
What I am trying to do is to draw a line every third row. So every third text box, there should be a thick line spanning all columns. Hope that makes sense.
Any help is greatly apprecaited. Thank you!
The simple answer is, GridLayout isn't going to do what you want, it's simply not flexible enough, instead...
You could...
Change the layout manager and make use of a JSeparator
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class SudokuGrid {
public static void main(String[] args) {
new SudokuGrid();
}
public SudokuGrid() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public static class TestPane extends JPanel {
private static final int ROWS = 9;
private static final int COLUMNS = 9;
int fontSize = 30;
public TestPane() {
JTextField[][] inputBoxes = new JTextField[ROWS][COLUMNS];
Font font = new Font("Helvetica", Font.BOLD, fontSize);
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.gridy = 0;
GridBagConstraints split = new GridBagConstraints();
split.fill = GridBagConstraints.BOTH;
split.weightx = 1;
split.gridx = 0;
split.gridwidth = GridBagConstraints.REMAINDER;
// outer loop to create the rows
for (int rows = 0; rows < ROWS; rows++) {
gbc.gridy++;
// inner loop to create the columns
for (int columns = 0; columns < COLUMNS; columns++) {
gbc.gridx = columns;
// make text fields empty
inputBoxes[rows][columns] = new JTextField(1);
// add text fields to the frame
add(inputBoxes[rows][columns], gbc);
// center text in each text box
inputBoxes[rows][columns].setHorizontalAlignment(JTextField.CENTER);
// apply font to each text box
inputBoxes[rows][columns].setFont(font);
} // end of columns loop
if ((rows + 1) % 3 == 0) {
System.out.println("Split");
split.gridy = gbc.gridy + 1;
gbc.gridy += 2;
JSeparator sep = new JSeparator(JSeparator.HORIZONTAL);
add(sep, split);
}
} // end of rows loop
}
}
}
You could...
Make your own "split" component through the use of custom painting
public static class HorizontalSplit extends JPanel {
public HorizontalSplit() {
setOpaque(false);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(0, 3);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
int y = (getHeight() - 3) / 2;
BasicStroke stroke = new BasicStroke(3, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
g2d.setStroke(stroke);
g2d.drawLine(0, y, getWidth(), y);
g2d.dispose();
}
}
Which would simply replace the JSeparator...
if ((rows + 1) % 3 == 0) {
System.out.println("Split");
split.gridy = gbc.gridy + 1;
gbc.gridy += 2;
JPanel sep = new HorizontalSplit();
add(sep, split);
}
You could...
Use a compound layout and a MatteLayout...
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.MatteBorder;
public class SudokuGrid {
public static void main(String[] args) {
new SudokuGrid();
}
public SudokuGrid() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public static class TestPane extends JPanel {
private static final int ROWS = 9;
private static final int COLUMNS = 9;
int fontSize = 30;
public TestPane() {
JTextField[][] inputBoxes = new JTextField[ROWS][COLUMNS];
Font font = new Font("Helvetica", Font.BOLD, fontSize);
setLayout(new GridBagLayout());
GridBagConstraints groupContraint = new GridBagConstraints();
groupContraint.fill = GridBagConstraints.BOTH;
groupContraint.weightx = 1;
groupContraint.weighty = 1;
groupContraint.gridwidth = GridBagConstraints.REMAINDER;
JPanel group = new JPanel(new GridLayout(3, COLUMNS));
group.setBorder(new MatteBorder(0, 0, 1, 0, Color.BLACK));
// outer loop to create the rows
for (int rows = 0; rows < ROWS; rows++) {
// inner loop to create the columns
for (int columns = 0; columns < COLUMNS; columns++) {
// make text fields empty
inputBoxes[rows][columns] = new JTextField(1);
// add text fields to the frame
group.add(inputBoxes[rows][columns]);
// center text in each text box
inputBoxes[rows][columns].setHorizontalAlignment(JTextField.CENTER);
// apply font to each text box
inputBoxes[rows][columns].setFont(font);
} // end of columns loop
if ((rows + 1) % 3 == 0) {
add(group, groupContraint);
group = new JPanel(new GridLayout(3, COLUMNS));
group.setBorder(new MatteBorder(0, 0, 1, 0, Color.BLACK));
}
} // end of rows loop
}
}
}
In my FlashCardPanel class, I have a subpanel,LabelPanel, with a Grid Bag Layout. It consists of a constructor with an edit button, a button to "flip" the card, and the label to display the term/definition. My problem is that every time I click my "Flip" Button to display the definition of my term, the flip button will change size, usually matching the length of the definition.
Images of the problem
http://postimg.org/gallery/ymww3axq/
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class FlashCardPanel extends JPanel{
private String term;
private String definition;
// shows the current text whether it is a term or definition
private JLabel currentLabel;
private static String NO_CARDS = "This set is empty";
//current card being displayed
private FlashCard currentCard;
//new card that is added to the deck
private FlashCard newCard;
// true = term is showing; false = definition is showing
private boolean termShowing = true;
private AddNewCard frame;
private CardSet cardSet;
private int cardIndex = 0;
private ButtonPanel bPanel;
private LabelPanel lPanel;
private static JButton flipButton;
private static JButton nextButton;
private static JButton prevButton;
private static JButton addCard;
private static JButton deleteCard;
private static JButton editButton;
public FlashCardPanel(CardSet cardSet) {
this.cardSet = cardSet;
this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
term = cardSet.get(0).getTerm();
definition = cardSet.get(0).getDefintion();
currentCard = cardSet.get(0);
createButtons();
lPanel = new LabelPanel();
bPanel = new ButtonPanel();
add(lPanel);
add(bPanel);
}
public FlashCardPanel() {
this.setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
add(lPanel);
add(bPanel);
}
private class LabelPanel extends JPanel {
public LabelPanel() {
this.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(15,15,15,15);
currentLabel = new JLabel(term);
currentLabel.setText(cardSet.get(0).getTerm());
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0;
c.gridx = 0;
c.gridy = 0;
add(editButton,c);
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 0;
c.gridwidth = 0;
c.weightx = 0;
c.gridx = 2;
c.gridy = 0;
add(flipButton,c);
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 40;
c.weightx = 0;
c.gridwidth = 3;
c.gridx = 1;
c.gridy = 3;
add(currentLabel,c);
}
}
private class ButtonPanel extends JPanel {
public ButtonPanel() {
this.setLayout(new GridLayout(2,2));
add(prevButton);
add(nextButton);
add(addCard);
add(deleteCard);
}
}
/*
* creates buttons for the panel. Should be called before
* any subpanel is created.
*/
private void createButtons()
{
flipButton = new JButton(" Flip Card ");
flipButton.addActionListener(new ButtonListener());
flipButton.setActionCommand("1");
nextButton = new JButton(" Next Card ");
nextButton.addActionListener(new ButtonListener());
nextButton.setActionCommand("2");
prevButton = new JButton(" Previous Card ");
prevButton.addActionListener(new ButtonListener());
prevButton.setActionCommand("3");
addCard = new JButton(" Add Card ");
addCard.addActionListener(new ButtonListener());
addCard.setActionCommand("4");
deleteCard = new JButton(" Delete Card ");
deleteCard.addActionListener(new ButtonListener());
deleteCard.setActionCommand("5");
editButton = new JButton("Edit");
editButton.addActionListener(new ButtonListener());
editButton.setActionCommand("6");
}
private class ButtonListener implements ActionListener
{
#Override
public void actionPerformed(ActionEvent e) {
int action = Integer.parseInt(e.getActionCommand());
switch(action){
case 1:
flipCard();
break;
case 2:
nextCard();
break;
case 3:
previousCard();
break;
case 4:
createFrame();
break;
case 5:
deleteCard();
case 6:
createFrame(term,definition);
}
}
}
private void flipCard()
{
if (!cardSet.isEmpty()) {
if (termShowing) {
termShowing = false;
currentLabel.setText(definition);
}
else {
termShowing = true;
currentLabel.setText(term);
}
}
else {
currentLabel.setText(NO_CARDS);
term = "";
definition = "";
JOptionPane.showMessageDialog(this, "This set is empty");
}
}
private void nextCard()
{
if (!cardSet.isEmpty()) {
if (cardIndex == cardSet.size()-1)
cardIndex = 0;
else
cardIndex++;
term = cardSet.get(cardIndex).getTerm();
definition = cardSet.get(cardIndex).getDefintion();
if(termShowing)
currentLabel.setText(term);
else
currentLabel.setText(definition);
currentCard = cardSet.get(cardIndex);
}
else JOptionPane.showMessageDialog(this, "This set is empty");
}
private void previousCard()
{
if (!cardSet.isEmpty()) {
if (cardIndex == 0)
cardIndex = cardSet.size()-1;
else
cardIndex--;
term = cardSet.get(cardIndex).getTerm();
definition = cardSet.get(cardIndex).getDefintion();
if(termShowing)
currentLabel.setText(term);
else
currentLabel.setText(definition);
currentCard = cardSet.get(cardIndex);
}
else JOptionPane.showMessageDialog(this, "This set is empty");
}
/*
* adding a card
*/
private void createFrame() {
frame = new AddNewCard(100,100,this);
}
/*
* editing an existing card
*/
private void createFrame(String t, String d)
{
frame = new AddNewCard(100,100,this,t,d);
}
public void addNewCard(String t, String d) {
newCard = new FlashCard(t,d);
cardSet.add(newCard);
if (cardSet.isEmpty()) currentLabel.setText(newCard.getTerm());
}
public void editCard(String t, String d) {
currentCard.setTerm(t);
currentCard.setDefinition(d);
if (termShowing) currentLabel.setText(t);
else currentLabel.setText(d);
}
/*
* Deletes current card on display
*/
private void deleteCard()
{
if (!cardSet.isEmpty()) {
// if on the last card of the set
if (cardIndex == cardSet.size()-1) cardIndex--;
cardSet.remove(currentCard);
if (!cardSet.isEmpty()) {
currentCard = cardSet.get(cardIndex);
currentLabel.setText(cardSet.get(cardIndex).getTerm());
}
else currentLabel.setText(NO_CARDS);
}
else JOptionPane.showMessageDialog(this, "This set is empty");
}
}
You have several options including:
using a nested JPanels each with its own layout. For instance the buttons could be placed into a GridLayout JPanel, and this placed into a BorderLayout JPanel with the label BorderLayout.CENTER
I suggest that the long definition text be displayed within a JTextArea, not a JLabel. If you make it non-editable and remove borders, it could look like a JLabel.
If you go this route, you will want to turn on word wrap on the JTextArea.
You could swap JTextArea with JLabel (for the term) using a CardLayout.
Note, for future questions, please pare down your problem. For instance, if this were my question, I'd create something like the code below, small, self contained, runnable and demonstrates the problem:
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import javax.swing.*;
public class FlashCardPanel2 extends JPanel {
private static final long serialVersionUID = 1L;
private static final int PREF_W = 400;
private static final int PREF_H = PREF_W;
private JLabel currentLabel;
private String term = "Term";
private String definition = "Definition: This will be a very long String to "
+ "illustrate the problem that you are having, and to try to help you get "
+ "a solution";
private JButton editButton = new JButton("Edit");
private JButton flipButton = new JButton(new FlipAction("Flip"));
public FlashCardPanel2() {
this.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(15, 15, 15, 15);
currentLabel = new JLabel(term);
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0;
c.gridx = 0;
c.gridy = 0;
add(editButton, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 0;
c.gridwidth = 0;
c.weightx = 0;
c.gridx = 2;
c.gridy = 0;
add(flipButton, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 40;
c.weightx = 0;
c.gridwidth = 3;
c.gridx = 1;
c.gridy = 3;
add(currentLabel, c);
}
#Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}
private class FlipAction extends AbstractAction {
public FlipAction(String name) {
super(name);
int mnemonic = (int) name.charAt(0);
putValue(MNEMONIC_KEY, mnemonic);
}
#Override
public void actionPerformed(ActionEvent e) {
String text = currentLabel.getText();
text = (text.equals(term)) ? definition : term;
currentLabel.setText(text);
}
}
private static void createAndShowGui() {
JFrame frame = new JFrame("FlashCardPanel2");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new FlashCardPanel2());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
And here's a potential solution with CardLayout and GridLayout and BorderLayout:
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import javax.swing.*;
public class FlashCardPanel3 extends JPanel {
private static final long serialVersionUID = 1L;
private static final String CURRENT_LABEL = "current label";
private static final String DEFINITION = "definition";
private JLabel currentLabel;
private JTextArea currentDefinitionArea = new JTextArea(6, 20);
private CardLayout cardLayout = new CardLayout();
private JPanel cardHolder = new JPanel(cardLayout);
private String term = "Term";
private String definition = "Definition: This will be a very long String to "
+ "illustrate the problem that you are having, and to try to help you get "
+ "a solution";
private JButton editButton = new JButton("Edit");
private JButton flipButton = new JButton(new FlipAction("Flip"));
public FlashCardPanel3() {
currentDefinitionArea.setOpaque(false);
currentDefinitionArea.setText(definition);
currentDefinitionArea.setWrapStyleWord(true);
currentDefinitionArea.setLineWrap(true);
currentDefinitionArea.setEditable(false);
currentDefinitionArea.setFocusable(false);
currentLabel = new JLabel(term, SwingConstants.CENTER);
cardHolder.add(currentLabel, CURRENT_LABEL);
cardHolder.add(new JScrollPane(currentDefinitionArea), DEFINITION);
JPanel buttonPanel = new JPanel(new GridLayout(1, 2, 5, 0));
buttonPanel.add(editButton);
buttonPanel.add(flipButton);
setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
setLayout(new BorderLayout(5, 5));
add(buttonPanel, BorderLayout.PAGE_START);
add(cardHolder);
}
private class FlipAction extends AbstractAction {
public FlipAction(String name) {
super(name);
int mnemonic = (int) name.charAt(0);
putValue(MNEMONIC_KEY, mnemonic);
}
#Override
public void actionPerformed(ActionEvent e) {
// String text = currentLabel.getText();
// text = (text.equals(term)) ? definition : term;
// currentLabel.setText(text);
cardLayout.next(cardHolder);
}
}
private static void createAndShowGui() {
JFrame frame = new JFrame("FlashCardPanel2");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new FlashCardPanel3());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
I am trying to accomplish the following with GridBagLayout:
The frame will receive a collection of "fields" (JLabel, JTextField pairs), I want to arrange them in a 'Grid-like' fashion where a row will contain 2 such pairs (JLabel1 JField1 JLabel2 JField2). When a row has these four components, the next components are added to another row.
EDIT: I would like the components to start at the top of the panel
My code produces the following layout. I would like the components to be laid out more compactly (especially the vertical distance)
Here is the code:
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Test extends JFrame{
private JPanel _panel;
public Test() {
this.setDefaultCloseOperation(HIDE_ON_CLOSE);
this.setResizable(true);
this.setVisible(true);
Dimension size = new Dimension(600,600);
this.setMinimumSize(size);
this.setSize(size);
this.setPreferredSize(size);
_panel = new JPanel();
this.add(_panel);
}
public static void main(String[] args) {
Test t = new Test();
String[] labels = {"label1", "label2","label3","label4","label5"};
String[] fieldValues = {"value1","value2","value3","value4","value5"};
t.createFields(labels,fieldValues);
}
private void createFields(String[] labels, String[] fieldValues) {
_panel.setLayout(new GridBagLayout());
int col = 0;
int row = -1;
for(int i=0; i < labels.length;i++) {
JLabel label = new JLabel(labels[i] + ":", JLabel.TRAILING);
JTextField field = new JTextField(fieldValues[i]);
Dimension size = new Dimension(200,30);
field.setPreferredSize(size);
label.setLabelFor(field);
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.weighty = 1;
c.weightx = 1;
c.ipadx = 0;
c.ipady = 0;
c.insets = new Insets(0,0,0,0);
c.anchor = GridBagConstraints.LINE_START;
c.gridx = col%4;
if(i%2 == 0) row++;
c.gridy = row;
_panel.add(label,c);
col++;
c.gridx = col%4;
_panel.add(field,c);
col++;
}
this.setVisible(true);
}
If you don't mind your elements being in center of panel (vertically and horizontally), then remove
c.weighty = 1;
c.weightx = 1;
from your code.
If center is wrong place, add
GridBagConstraints c = new GridBagConstraints();
c.gridx=4;
c.gridy=labels.length;
c.weightx=1;
c.weighty=1;
_panel.add(new JLabel(),c);
after your loop
If you want your GUI to be that size, but have the components in a compact size, then place them in their own JPanel, one that uses GridBagLayout, and then add that JPanel to your main GUI JPanel. If you want the components to fill the width, then have the main JPanel use BorderLayout, and add your GBL using JPanel BorderLayout.NORTH or .SOUTH whatever your need is.
For example:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.*;
public class GridBagExample extends JPanel {
private static final int PREF_W = 600;
private static final int PREF_H = PREF_W;
public GridBagExample() {
super(new BorderLayout());
add(new GridBagUsingPanel(), BorderLayout.NORTH);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(PREF_W, PREF_H);
}
private static void createAndShowGui() {
JFrame frame = new JFrame("GridBagExample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new GridBagExample());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
class GridBagUsingPanel extends JPanel {
public static final int COLUMNS = 2;
public static final int ROWS = 3;
private static final int TF_COLS = 10;
private static int inset = 5;
private static final Insets INSETS = new Insets(inset, inset, inset, inset);
private static final Insets EXTRA_INSETS = new Insets(inset, inset, inset, 8 * inset);
private static final int EB_GAP = 10;
public GridBagUsingPanel() {
super(new GridBagLayout());
setBorder(BorderFactory.createEmptyBorder(EB_GAP, EB_GAP, EB_GAP, EB_GAP));
for (int r = 0; r < ROWS; r++) {
for (int c = 0; c < COLUMNS; c++) {
addComponent(r, c);
}
}
}
private void addComponent(int r, int c) {
int count = 1 + r * COLUMNS + c;
JLabel label = new JLabel("label " + count);
JTextField textField = new JTextField("value " + count, TF_COLS);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 2 * c;
gbc.gridy = r;
gbc.anchor = GridBagConstraints.WEST;
gbc.fill = GridBagConstraints.BOTH;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.insets = INSETS;
gbc.weightx = 0.1;
gbc.weighty = 1.0;
add(label, gbc);
gbc.gridx++;
gbc.anchor = GridBagConstraints.EAST;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1.0;
if (c != COLUMNS - 1) {
gbc.insets = EXTRA_INSETS;
}
add(textField, gbc);
}
}