I feel as beginner I may have bitten off too much in regards to application building. That said, I am working on developing an application for a friend that will have prompts where each JPanel will provide fields to create an object to be used later. What I would like to have happen is that when the panel loads, it displays one object creation panel and a button to dynamically add a new panel if the user wants to make multiples (the plus button would add the new panel).
I have drawn up something in paint to illustrate this:
By my very limited understanding, I can create a panel to hold these sub-panels, and then add a action listener to the '+' button to create new panels. The only way I could think to implement this is to create a constructor for the panel I want to add. Is this possible? Let me show you what I have:
package com.company;
import java.awt.*;
import javax.swing.*;
/**
* Created by Travis on 3/1/2015.
*/
public class MainSnakeGui extends JFrame{
protected int panelCount;
//row 1
JPanel row1 = new JPanel();
JLabel splitSnakeLabel = new JLabel("Create a Split Snake", JLabel.CENTER);
//row 2
JPanel row2 = new JPanel();
JButton addButton = new JButton("+");
public MainSnakeGui() {
super("Snake Channels");
setSize(550, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridLayout layout = new GridLayout(5, 1, 10, 10);
setLayout(layout);
FlowLayout layout1 = new FlowLayout(FlowLayout.CENTER, 10, 10);
row1.setLayout(layout1);
row1.add(splitSnakeLabel);
add(row1);
GridLayout layout2 = new GridLayout(1, 2, 10, 10);
row2.setLayout(layout2);
row2.add(addButton);
MainSnakeConstructor snakePanel = new MainSnakeConstructor();
row2.add(snakePanel);
add(row2);
setVisible(true);
}
public static void setLookAndFeel () {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Exception e) {
}
}
public static void main(String[] arg) {
MainSnakeGui.setLookAndFeel();
MainSnakeGui frame = new MainSnakeGui();
}
}
Here is the constructor:
package com.company;
import javax.swing.*;
import java.awt.*;
/**
* Created by Travis on 3/1/2015.
*/
public class MainSnakeConstructor extends JFrame {
public MainSnakeConstructor () {
JPanel splitSnakeRow = new JPanel();
JLabel snakeNameLabel = new JLabel("Snake Name");
JLabel channelCountLabel = new JLabel("Channel Count");
JCheckBox artistSuppliedCheckBox = new JCheckBox("Artist Supplied?");
JTextField snakeNameTextField = new JTextField(30);
JTextField channelCountTextField = new JTextField(3);
GridLayout layout = new GridLayout(3,2,10,10);
splitSnakeRow.setLayout(layout);
splitSnakeRow.add(snakeNameLabel);
splitSnakeRow.add(channelCountLabel);
splitSnakeRow.add(artistSuppliedCheckBox);
splitSnakeRow.add(snakeNameTextField);
splitSnakeRow.add(channelCountTextField);
add(splitSnakeRow);
}
}
Think about it differently. You want a button that allows you to add new panels, so you really only need a single button.
From there, you need some kind common panel which provides the functionality you want to the user (the creation panel). Then, when the user clicks the add button, you create a new creation panel and add it to the container been used to display them, for example...
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
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 class TestPane extends JPanel {
public TestPane() {
JButton btnAdd = new JButton("+");
setLayout(new BorderLayout());
JPanel buttons = new JPanel(new FlowLayout(FlowLayout.LEFT));
buttons.add(btnAdd);
add(buttons, BorderLayout.NORTH);
JPanel content = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.weighty = 1;
content.add(new JPanel(), gbc);
add(new JScrollPane(content));
btnAdd.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
CreationPane pane = new CreationPane();
int insertAt = Math.max(0, content.getComponentCount() - 1);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1;
content.add(pane, gbc, insertAt);
content.revalidate();
content.repaint();
}
});
}
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
public static class CreationPane extends JPanel {
private static int count;
public CreationPane() {
setLayout(new GridBagLayout());
add(new JLabel("Make it so " + (count++)));
setBorder(new CompoundBorder(new LineBorder(Color.BLACK), new EmptyBorder(10, 10, 10, 10)));
}
}
}
Now having done all that, I prefer the VerticalLayout manager from SwingLabs, SwingX library, which basically does the same thing...
Related
I'm attempting use flow layout to make a menu screen that orients with a single column, but whenever I add a button, it adds it to a single row.
import java.awt.CardLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Proj
{
JPanel card1,card2;
ActionListener listener;
JFrame menu;
JFrame catagories;
JButton menu1,addOrTake,cata,payd,showd;
//JButton
public Proj(){
card2=new JPanel();
menu = new JFrame("Card Layout");
catagories = new JFrame();
//final Container contentPane = menu.getContentPane();
final CardLayout layout = new CardLayout();
menu.setLayout(layout);
card1=new JPanel();
menu1 = new JButton("");
menu1.setIcon(new ImageIcon("C:/Users/sabar/Menu.jpg"));
menu1.setSize(60,600);
menu1.setVisible(true);
addOrTake = new JButton();
addOrTake.setIcon(new ImageIcon("C:/Users/Public/Pictures/Sample Pictures/Penguins.jpg"));
cata = new JButton("");
cata.setIcon(new ImageIcon("C:/Users/Public/Pictures/Sample Pictures/Penguins.jpg"));
showd = new JButton("");
showd.setIcon(new ImageIcon("C:/Users/Public/Pictures/Sample Pictures/Penguins.jpg"));
payd = new JButton("");
payd.setSize(60, 600);
payd.setIcon(new ImageIcon("C:/Users/Public/Pictures/Sample Pictures/Penguins.jpg"));
payd.setVisible(true);
card1.add(menu1);
card1.add(addOrTake);
card1.add(cata);
card1.add(showd);
card1.add(payd);
menu1.addActionListener(listener);
addOrTake.addActionListener(listener);
cata.addActionListener(listener);
showd.addActionListener(listener);
payd.addActionListener(listener);
listener = new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {}};
// TODO Auto-generated method stub
menu.setSize(60, 600);
menu.setVisible(true);
JButton poo=new JButton("Poo");
poo.setSize(60,600);
card2.add((poo));
card2.add(new JButton("Pee"));
card2.add(new JButton("Per"));
card2.add(new JButton("POt"));
card2.setVisible(false);
menu.add(card2);
menu.add(card1);
catagories.pack();
menu.pack();
card1.setVisible(false);
}
public static void main(String[]args)
{
Proj poop =new Proj();
}
}
Problems:
You're adding components to menu, the CardLayout-using container, without use of a 2nd parameter String constant. Without this, it might be hard to easily swap "cards" (components).
If you want a single column type of layout, you need to use a layout manager that allows this, and the JPanel default layout isn't it. Better options:
GridLayout(0, 1) for variable number of rows, one column
BoxLayout(Container, BoxLayout.PAGE_AXIS)
GridBagLayout with the right GridBagConstraint gridx and gridy attributes.
Start by using a layout manager which is capable of achiving your results.
Start by taking a look at Laying Out Components Within a Container, How to Use GridLayout and How to Use GridBagLayout for some ideas.
import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Proj {
JPanel card1, card2;
ActionListener listener;
JButton menu1, addOrTake, cata, payd, showd;
//JButton
JFrame menu;
public Proj() {
menu = new JFrame("Card Layout");
//final Container contentPane = menu.getContentPane();
final CardLayout layout = new CardLayout();
menu.setLayout(layout);
card1 = new JPanel(new GridBagLayout());
card2 = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.fill = GridBagConstraints.HORIZONTAL;
menu1 = new JButton("Mneu");
menu1.setVisible(true);
addOrTake = new JButton("Add or take");
cata = new JButton("Cata");
showd = new JButton("ShowD");
payd = new JButton("payd");
card1.add(menu1, gbc);
card1.add(addOrTake, gbc);
card1.add(cata, gbc);
card1.add(showd, gbc);
card1.add(payd, gbc);
menu1.addActionListener(listener);
addOrTake.addActionListener(listener);
cata.addActionListener(listener);
showd.addActionListener(listener);
payd.addActionListener(listener);
listener = new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
}
};
// TODO Auto-generated method stub
JButton poo = new JButton("Poo");
card2.add((poo), gbc);
card2.add(new JButton("Pee"), gbc);
card2.add(new JButton("Per"), gbc);
card2.add(new JButton("POt"), gbc);
menu.add(card2, "Card2");
menu.add(card1, "Card1");
layout.show(menu.getContentPane(), "Card1");
menu.pack();
menu.setLocationRelativeTo(null);
menu.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
Proj poop = new Proj();
}
});
}
}
I'd also have a closer look at How to Use CardLayout for more details about how it works, because you're using it wrong
I am looking to develop a word guessing game for JAVA but I am having a bit of difficulty developing the GUI. I am not trying to develop a simple GUI that just using one text field, I would like to develop one that is more mobile-like so to say. Thus I would like to white-boxes show up as place holders for the characters in the word. It would something like the image below.
Essentially players will be able to drag and drop characters from below (I'll probably have them be buttons or something) into the place holder boxes to be compared to the character of the word at that location. I have tried making an array list of text fields but that failed horribly. Anyone have any suggestions on how to proceed?
Updated as per answer provided by MadProgrammer
After using some of MadProgrammer's code I came up with the following package structure for my views package.
> Views
>> GameBoard.java
>> HighScorePanel.java
>> MainPanel.java
>> ScorePanel.java
>> StatisticPanel.java
>> TimerPanel.java
>> ViewConfig.java
>> WordPanel.java
Essentially just a bunch of panels that are put together in the GameBoard.java class. The main issues that I am having is with the MainPanel.java class, and keeping the aspect ratios. So below is an image of the view so far, it looks half decent but the word hint & category are right up against the text fields, and the text fields not wide enough.
If I were to enlarge this frame, the spacing between the components would stay exactly the same and would not scale up. So essentially I looking for a way to dynamically set the spacing between components. I have placed the code for GameBoard.java, MainPanel.java and StatisticPanel.java below. GameBoard is composed of MainPanel and StatisticsPanel. StatisticsPanel is composed of ScorePanel, TimerPanel and HighScorePanel. Finally MainPanel is composed of the bits that MadProgrammer suggested earlier.
GameBoard.java
package views;
import java.awt.BorderLayout;
import javax.swing.JFrame;
public class GameBoard extends JFrame{
private StatisticsPanel sp = new StatisticsPanel();
private MainPanel mp = new MainPanel();
public GameBoard() {
// set the title for the game board
setTitle(ViewConfig.DEFAULT_GAME_TITLE);
// add panel components to board
add(sp, BorderLayout.PAGE_START);
add(mp, BorderLayout.CENTER);
// set the board size
setSize(ViewConfig.DEFAULT_FRAME_WIDTH, ViewConfig.DEFAULT_FRAME_HEIGHT);
// set the default close operation
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
GameBoard gb = new GameBoard();
// set visible
gb.setVisible(true);
}
}
StatisticsPanel.java
package views;
import java.awt.FlowLayout;
import javax.swing.JPanel;
public class StatisticsPanel extends JPanel {
private ScorePanel sp = new ScorePanel();
private TimerPanel tp = new TimerPanel();
private HighScorePanel hsp = new HighScorePanel();
protected StatisticsPanel() {
// Create a layout for the panel, flow layout in this case as each component should line up
// horizontally
FlowLayout panelLayout = new FlowLayout(FlowLayout.CENTER, (ViewConfig.DEFAULT_FRAME_WIDTH/6), 4);
// Set the panel's layout to the newly created layout
setLayout(panelLayout);
// add components
add(sp);
add(tp);
add(hsp);
}
}
MainPanel.java
package views;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class MainPanel extends JPanel {
private JButton submitAns, clearAns;
private JLabel wordHint, wordCategory;
private WordPanel wp = new WordPanel(ViewConfig.DEFAULT_GUESS_WORD.length());
protected MainPanel() {
// create the buttons to submit and clear answer
submitAns = new JButton(ViewConfig.DEFAULT_SUBMIT);
clearAns = new JButton(ViewConfig.DEFAULT_CLEAR);
// Create new default labels for the word hint and category
wordHint = new JLabel(ViewConfig.DEFAULT_WORD_HINT);
wordCategory = new JLabel(ViewConfig.DEFAULT_WORD_CATEGORY);
// Set layout manger to GridBagLayout
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.insets = new Insets(4, 4, 4, 4);
// add all components to panel with following format:
// Word Hint > Input Fields > Word Category > Buttons
add(wordHint, gbc);
add(wp, gbc);
add(wordCategory, gbc);
// Create secondary JPanel to group buttons together, give it flow layout and add button panel to parent
JPanel buttonPanel = new JPanel();
FlowLayout buttonPanelLayout = new FlowLayout(FlowLayout.CENTER, 10, 4);
buttonPanel.add(submitAns, buttonPanelLayout);
buttonPanel.add(clearAns, buttonPanelLayout);
// add button panel to parent
add(buttonPanel);
}
protected JLabel getWordHint() {
return wordHint;
}
protected void setWordHint(String wordHint) {
this.wordHint.setText(wordHint);
}
protected JLabel getWordCategory() {
return wordCategory;
}
protected void setWordCategory(String wordCategory) {
this.wordCategory.setText(wordCategory);
}
}
One idea might be to use a series of compounding layouts which you can build up to meet your requirements.
At the heart would a JPanel, which would present specially crafted JLabels capable of displaying the characters of the word, for example...
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FontMetrics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
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 GuessingGame());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class GuessingGame extends JPanel {
public GuessingGame() {
setLayout(new GridBagLayout());
JLabel label = new JLabel("<html>A few sentences giving either a definition or a<br> few hints about what the word could be</html>");
WordPane wordPane = new WordPane("stackoverflow");
JLabel wordCategory = new JLabel("Word Category");
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.insets = new Insets(4, 4, 4, 4);
add(label, gbc);
add(wordPane, gbc);
JPanel pnlWordCategory = new JPanel(new GridLayout(1, 6, 4, 4));
pnlWordCategory.add(new LetterLabel("A"));
pnlWordCategory.add(new LetterLabel("B"));
pnlWordCategory.add(new LetterLabel("C"));
pnlWordCategory.add(new LetterLabel("D"));
pnlWordCategory.add(new LetterLabel("W"));
pnlWordCategory.add(new LetterLabel("G"));
add(wordCategory, gbc);
add(pnlWordCategory, gbc);
}
}
public class WordPane extends JPanel {
private String word;
private List<JLabel> labels;
public WordPane(String text) {
setLayout(new GridLayout(1, text.length(), 4, 4));
labels = new ArrayList<>(text.length());
for (int index = 0; index < text.length(); index++) {
LetterLabel label = new LetterLabel();
labels.add(label);
add(label);
}
}
}
public class LetterLabel extends JLabel {
public LetterLabel(String text) {
this();
setText(text);
}
public LetterLabel() {
setBorder(new CompoundBorder(new LineBorder(Color.GRAY), new EmptyBorder(4, 4, 4, 4)));
}
#Override
public Dimension getPreferredSize() {
Insets insets = getInsets();
Dimension size = new Dimension();
FontMetrics fm = getFontMetrics(getFont());
size.width = (insets.left + insets.right) + fm.stringWidth("M");
size.height = (insets.top + insets.bottom) + fm.getHeight();
return size;
}
}
}
Now, you could use the same idea, but use JTextFields instead of JLabels
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FontMetrics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
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 GuessingGame());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class GuessingGame extends JPanel {
public GuessingGame() {
setLayout(new GridBagLayout());
JLabel label = new JLabel("<html>A few sentences giving either a definition or a<br> few hints about what the word could be</html>");
WordPane wordPane = new WordPane("stackoverflow");
JLabel wordCategory = new JLabel("Word Category");
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.insets = new Insets(4, 4, 4, 4);
add(label, gbc);
add(wordPane, gbc);
JPanel pnlWordCategory = new JPanel(new GridLayout(1, 6, 4, 4));
pnlWordCategory.add(new LetterLabel("A"));
pnlWordCategory.add(new LetterLabel("B"));
pnlWordCategory.add(new LetterLabel("C"));
pnlWordCategory.add(new LetterLabel("D"));
pnlWordCategory.add(new LetterLabel("W"));
pnlWordCategory.add(new LetterLabel("G"));
add(wordCategory, gbc);
add(pnlWordCategory, gbc);
}
}
public class WordPane extends JPanel {
private String word;
private List<JTextField> fields;
public WordPane(String text) {
setLayout(new GridLayout(1, text.length(), 4, 4));
fields = new ArrayList<>(text.length());
for (int index = 0; index < text.length(); index++) {
JTextField field = new JTextField(2);
fields.add(field);
add(field);
}
}
}
public class LetterLabel extends JLabel {
public LetterLabel(String text) {
this();
setText(text);
}
public LetterLabel() {
setBorder(new CompoundBorder(new LineBorder(Color.GRAY), new EmptyBorder(4, 4, 4, 4)));
}
#Override
public Dimension getPreferredSize() {
Insets insets = getInsets();
Dimension size = new Dimension();
FontMetrics fm = getFontMetrics(getFont());
size.width = (insets.left + insets.right) + fm.stringWidth("M");
size.height = (insets.top + insets.bottom) + fm.getHeight();
return size;
}
}
}
Now, control. In order to manage the process you would need some kind of model or controller which would be capable of taking the input from the WordPane and validating it and then notifying the WordPane that the guess was valid or not so it could update the UI, but that's beyond the scope of the question
Updated
There are a number of ways you might achieve dragging characters from one part to another part, none of the simple.
Probably the simplest is to wrap the String value of your control in a Transferable and update the field once it's dropped on it, something like this for example
I have encountered a problem whilst working the JFrame's, and JButtons. I am trying to center my JButton, however when I do so, it covers almost the entire screen. Any idea's on what's causing this?
Here is a picture on how it looks :
And here is my code :
package character;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.io.IOException;
/**
* Created by Niknea on 6/28/14.
*/
public class characterSelector{
JFrame cselectorButtons, clogo;
JLabel logo, characterName, label;
JButton male, female;
public characterSelector(){
this.createCharacterSelector();
}
public void createCharacterSelector() {
try {
label = new JLabel(new ImageIcon(ImageIO.read(getClass()
.getResource("/resources/Grass_Background.jpg"))));
cselectorButtons = new JFrame("SupremeSoccer");
logo = new JLabel(new ImageIcon(ImageIO.read(this.getClass().getResource("/resources/Character_Selector_Image.png"))));
characterName = new JLabel("<Character Name>");
characterName.setFont(new Font(characterName.getFont().getName(),
Font.HANGING_BASELINE, 50));
/*
Buttons
*/
male = new JButton("Male");
////******************////
//// END OF BUTTONS ////
////****************////
cselectorButtons.add(logo);
cselectorButtons.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cselectorButtons.setContentPane(label);
cselectorButtons.setLayout(new BorderLayout());
cselectorButtons.add(logo, BorderLayout.NORTH);
cselectorButtons.add(male, BorderLayout.CENTER);
cselectorButtons.pack();
cselectorButtons.setLocationRelativeTo(null);
cselectorButtons.setVisible(true);
} catch (IOException exp) {
exp.printStackTrace();
}
}
}
Thanks again.
Any idea's on what's causing this?
This is the default behaviour of BorderLayout. The component at the CENTER position will occupy the maximum amount of space the is available from the parent component, when the other (NORTH, SOUTH, EAST, WEST) positions have been taken into account
Depending on what you are trying to achieve you might consider creating another JPanel (set it's opaque state to false so it's transparent) and use something like GridLayout or GridBagLayout instead.
Take a look at A Visual Guide to Layout Managers for some more ideas
Updated
So based on your linked code, I changed
part2 = new JPanel();
to
part2 = new JPanel(new java.awt.GridBagLayout());
And got...
Updated with additional example
Start by breaking down your requirements into individual containers and focus on the layout requirements for each individual, then build them all back into a single container.
This will make changing them later much easier and also make controlling them much easier...
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class ExampleLayout {
public static void main(String[] args) {
new ExampleLayout();
}
public ExampleLayout() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private HeaderPane header;
private ScorePane score;
private CharacterSelectionPane characterSelection;
public TestPane() {
setLayout(new BorderLayout());
JLabel background = new JLabel();
try {
BufferedImage img = ImageIO.read(getClass().getResource("/Grass.jpg"));
background.setIcon(new ImageIcon(img));
} catch (IOException ex) {
ex.printStackTrace();
}
add(background);
background.setLayout(new GridBagLayout());
header = new HeaderPane();
score = new ScorePane();
characterSelection = new CharacterSelectionPane();
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.weightx = 1;
gbc.fill = GridBagConstraints.BOTH;
background.add(header, gbc);
background.add(score, gbc);
gbc.weighty = 1;
background.add(characterSelection, gbc);
}
}
public class HeaderPane extends JPanel {
public HeaderPane() {
setLayout(new BorderLayout());
JLabel label = new JLabel("Character Selection");
label.setForeground(Color.WHITE);
label.setFont(label.getFont().deriveFont(Font.BOLD, 48f));
label.setHorizontalAlignment(JLabel.CENTER);
add(label);
setOpaque(false);
}
}
public class ScorePane extends JPanel {
public ScorePane() {
JLabel label = new JLabel("[-][-[]-][-]");
label.setForeground(Color.YELLOW);
add(label);
setOpaque(false);
}
}
public class CharacterSelectionPane extends JPanel {
private JButton btMale;
private JButton btFemale;
private JTextField tfName;
private JButton btContinue;
public CharacterSelectionPane() {
setOpaque(false);
setLayout(new GridBagLayout());
btMale = new JButton("Male");
btFemale = new JButton("Female");
btContinue = new JButton("Continue");
tfName = new JTextField(10);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets = new Insets(4, 4, 4, 4);
gbc.fill = GridBagConstraints.HORIZONTAL;
add(btMale, gbc);
gbc.gridx++;
add(btFemale, gbc);
gbc.gridx = 0;
gbc.gridy++;
add(new JLabel("Name:"), gbc);
gbc.gridx++;
add(tfName, gbc);
gbc.gridx = 0;
gbc.gridy++;
gbc.gridwidth = GridBagConstraints.REMAINDER;
add(btContinue, gbc);
}
}
}
Use a BoxLayout or (easier) GridBagLayout for the bottom area, as seen in this answer.
I want to stop the text field from running off the GUI or print the text field on a new "line".
Here is the code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Window extends JFrame {
private JTextField TextField0;
private JTextField TextField1;
private JCheckBox CheckBox0;
private JPanel panel;
//CONSTRUCTOR
public Window() {
super("Checkbox");
setLayout(new FlowLayout());
panel = new JPanel();
add(panel, BorderLayout.CENTER);
TextField0 = new JTextField("Add field",15);
panel.add(TextField0);
TextField1 = new JTextField("Add field", 15);
CheckBox0 = new JCheckBox("");
HandlerClass handler = new HandlerClass();
TextField0.addActionListener(handler);
}
public class HandlerClass implements ActionListener {
public void actionPerformed(ActionEvent event) {
if(event.getSource()==TextField0) {
CheckBox0.setText(String.format("%s",event.getActionCommand()));
panel.remove(TextField0);
panel.add(CheckBox0);
panel.add(TextField1);
panel.revalidate();
panel.repaint();
}
}
}
}
There's not really anything you can do...
JTextField will allow the text to overflow the viewable area of the textfield (trimming the text on the screen)
You could try using a JTextArea, which supports multi line text
You could also try packing the frame
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestTextField {
public static void main(String[] args) {
new TestTextField();
}
public TestTextField() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
String text = "This is a long piece of text that seems to go on and on and on and on an on....and some more...";
JTextField field = new JTextField(10);
JTextArea ta1 = new JTextArea(10, 2);
JTextArea ta2 = new JTextArea(10, 2);
field.setText(text);
ta1.setText(text);
ta2.setText(text);
configure(ta1);
configure(ta2);
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
frame.add(field, gbc);
frame.add(ta1, gbc);
frame.add(new JScrollPane(ta2), gbc);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
protected void configure(JTextArea ta) {
ta.setWrapStyleWord(true);
ta.setLineWrap(true);
}
});
}
}
Set the layout of your panel to a Boxlayout instead of a Borderlayout. See the following link for the different layouts and how to use them:
http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
Trying to build out a GUI for my game but no matter what layout I work with I can't get the nest of panels to do what I like
My goal is this
http://i182.photobucket.com/albums/x202/NekoLLX/CharGenmockup-1.jpg
http://i182.photobucket.com/albums/x202/NekoLLX/CharGenmockup2.jpg
And Building off the Mad ones excelent revision I've got my left side as i like it but now the right eludes me
The general idea is that clicking on the title bars of the left menu will colapase (set visible to false) the content panes associated with them
//http://docs.oracle.com/javase/7/docs/api/java/awt/Color.html
//http://stackoverflow.com/questions/16430922/working-with-nested-panels
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.*;
public class JaGCharCreation {
//set inital size of window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int initalWidth = (int) screenSize.width - 50;
int initalHeight = (int) screenSize.height - 50;
public static void main(String[] args) {
new JaGCharCreation ();
}
//set up thread safe invoking for GUI
public JaGCharCreation () {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
//frame.setLocationRelativeTo(null);
frame.setVisible(true);
// Give the frame an initial size.
frame.setSize(initalWidth, initalHeight);
}
});
}
//main panel to hold all others
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridLayout(0, 2));
add(createLeftPane());
add(createRightPane());
}//end of class for master frame
protected JPanel createLeftPane() {
JLabel CharName = new JLabel("Character Name");
CharName.setFont(new Font("Impact", Font.BOLD, 30));
CharName.setBorder(new EmptyBorder(0, 81,0, 00));
JPanel panel = new JPanel(new BorderLayout());
panel.setBorder(new EmptyBorder(10, 10, 10, 10));
panel.setBackground(Color.RED);
JPanel content = new JPanel(new GridBagLayout());
content.setOpaque(false);
JPanel view3D = new JPanel();
view3D.setBackground(Color.DARK_GRAY);
JPanel view2D = new JPanel();
view2D.setBackground(Color.PINK);
JPanel viewIsometric = new JPanel();
viewIsometric.setBackground(Color.YELLOW);
JPanel viewData = new JPanel();
viewData.setBackground(Color.MAGENTA);
JPanel top = new JPanel(new GridLayout(0, 2));
top.setBorder(new EmptyBorder(0, 80,0, 80));
top.setBackground(Color.GREEN);
top.add(view3D);
top.add(view2D);
JPanel bottom = new JPanel(new GridLayout(2, 0));
bottom.setBorder(new EmptyBorder(0, 80,0, 80));
bottom.setBackground(Color.GREEN);
bottom.add(viewIsometric);
bottom.add(new JScrollPane(viewData));
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.weighty = 0.5f;
gbc.weightx = 1f;
gbc.fill = GridBagConstraints.BOTH;
content.add(top, gbc);
content.add(bottom, gbc);
panel.add(content);
panel.add(CharName, BorderLayout.NORTH);
return panel;
}//end left pane
protected JPanel createRightPane() {
JPanel panel = new JPanel(new BorderLayout());
panel.setBackground(Color.BLUE);
//set up our image for the title bars
ImageIcon icon = new ImageIcon("GradientDetail.png");
Image img = icon.getImage();
img = img.getScaledInstance(initalWidth/2, 40, java.awt.Image.SCALE_SMOOTH);
final ImageIcon iconSM = new ImageIcon(img);
JPanel name_panel = new JPanel(new BorderLayout())
{
protected void paintComponent(Graphics g)
{
// Dispaly image at full size
g.drawImage(iconSM.getImage(), 0, 0, null);
super.paintComponent(g);
}
};
name_panel.setOpaque( false );
JLabel label = new JLabel(" Character Name");
label.setFont(new Font("Impact", Font.BOLD, 30));
label.setForeground(Color.white);
label.setOpaque(false);
JPanel name_panel_text = new JPanel(new BorderLayout());
name_panel_text.setBackground(Color.WHITE);
name_panel.add(label, BorderLayout.NORTH);
panel.add(name_panel_text);
panel.add(name_panel);
return panel;
}//end right pane
//bassed from http://stackoverflow.com/questions/7340001/determine-clicked-jpanel-component-in-the-mouselistener-event-handling
public class MouseAdapterMod extends MouseAdapter {
// usually better off with mousePressed rather than clicked
public void mousePressed(MouseEvent e) {
if (e.getSource() == "name_panel"){
}
}
}
}//end master panel set
}//end master class
Something along the lines of ...
To be honest, I tried following your code, but got lost, so I re-wrote it...
Basically, you relying on setSize which is going to be ignored and changed by the layout managers as they see fit.
This example uses GridBagLayout and weighty to adjust the space allocated to the top (2D/3D) views and the bottom views, but you should also take a look at overriding the getPreferredSize of the final components, to provide better hints to the layout managers.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridLayout(0, 2));
add(createLeftPane());
add(createRightPane());
}
protected JPanel createLeftPane() {
JPanel panel = new JPanel(new BorderLayout());
panel.setBorder(new EmptyBorder(10, 10, 10, 10));
panel.setBackground(Color.RED);
JPanel content = new JPanel(new GridBagLayout());
content.setOpaque(false);
JPanel view3D = new JPanel();
view3D.setBackground(Color.DARK_GRAY);
JPanel view2D = new JPanel();
view2D.setBackground(Color.PINK);
JPanel viewIsometric = new JPanel();
viewIsometric.setBackground(Color.YELLOW);
JPanel viewData = new JPanel();
viewData.setBackground(Color.MAGENTA);
JPanel top = new JPanel(new GridLayout(0, 2));
top.setBorder(new LineBorder(Color.GREEN, 2));
top.add(view3D);
top.add(view2D);
JPanel bottom = new JPanel(new GridLayout(2, 0));
bottom.add(viewIsometric);
bottom.add(new JScrollPane(viewData));
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.weighty = 0.5f;
gbc.weightx = 1f;
gbc.fill = GridBagConstraints.BOTH;
content.add(top, gbc);
content.add(bottom, gbc);
panel.add(content);
panel.add(new JLabel("Character name"), BorderLayout.NORTH);
return panel;
}
protected JPanel createRightPane() {
JPanel panel = new JPanel();
panel.setBackground(Color.BLUE);
return panel;
}
}
}