I am using a GridBagLayout to create a JPanel, called 'Preset', that gets replicated several times in a JFrame. Each Preset will have multiple rows (JPanels). My goal is that only one line (the first) will show up, but when the edit button is clicked, they will all show. Right now, the edit button works, but there is a massive space between the lines. I want it to be such that when the extra lines are collapsed, each Preset will be directly above each other (no space). You can see in the following picture what I am talking about.
This is how it looks:
This is how I want it to Look:
I am fairly certain I need to do something with the GridBag, but I don't know what. I have read several tutorials and have written it as I thought it should be, but no luck.
package SSCCE;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
public class UI extends JFrame{
private final static int HEIGHT = 600;
private final static int WIDTH = 730;
private JPanel pane; //Pane that stores accounts
private JScrollPane scroller;
private Preset[] branches;
public static void main(String[] args) {
JFrame frame = new UI();
}
public UI(){
//Make the UI close when the exit button is clicked
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Sets the size of the UI
this.setSize(WIDTH, HEIGHT);
//Set the layout and add components to it
this.setLayout(new BorderLayout());
//Reads in the settings and sets up the branches
populateBranches();
pane = new JPanel();
pane.setLayout(new GridLayout(branches.length,1));
for (int i = 0; i < branches.length; i++){
pane.add(branches[i]);
}
//Create the center pane of the BorderLayout
scroller = new JScrollPane(pane);
scroller.createVerticalScrollBar();
this.add(scroller,BorderLayout.CENTER);
//Makes the UI visible
this.setVisible(true);
}
private void populateBranches(){
//Populates the branches array based on what is read in, or not read in from the file
branches = new Preset[15];
for (int i = 0; i < branches.length; i++){
branches[i] = new Preset();
branches[i].setEnabled(false);
}
}
public class Preset extends JPanel{
private JTextField eName;
private JButton edit;
private JButton activate;
private JComboBox printer;
private JPanel line1;
private JPanel line2;
private String branchName;
private String ipAddress;
private boolean enableAll;
public Preset(){
eName = new JTextField(20);
edit = new JButton("Edit");
activate = new JButton("Activate");
JPanel nameContainer = new JPanel();
nameContainer.setLayout(new FlowLayout());
nameContainer.add(eName);
printer = new JComboBox();
//
line1 = new JPanel();
line1.setLayout(new FlowLayout());
line1.add(nameContainer);
line1.add(edit);
line1.add(activate);
//
line2 = new JPanel();
line2.setLayout(new BorderLayout());
line2.add(printer, BorderLayout.WEST);
GridBagLayout grid = new GridBagLayout();
GridBagConstraints cons = new GridBagConstraints();
cons.fill = GridBagConstraints.BOTH;
this.setLayout(grid);
cons.ipady = 100;
cons.ipadx = 100;
cons.weighty = 0D;
cons.gridx = 0;
cons.gridy = 0;
cons.gridwidth = 2;
cons.gridheight = 1;
grid.setConstraints(line1, cons);
this.add(line1);
cons.ipady = 100;
cons.ipadx = 100;
cons.weighty = 0D;
cons.gridx = 0;
cons.gridy = 1;
cons.gridwidth = 2;
cons.gridheight = 1;
grid.setConstraints(line2, cons);
this.add(line2);
//Enable all components
enableAll = true;
}
}
}
Remove every statement that assigns padding in the Y AXIS for the GridBagConstraints of the Preset JPanel, i.e.
cons.ipady = 100;
I don't consider this a "real" answer to your question, but something for you to consider: get rid of gridbag. I don't see anything in your desired layout that requires it. Create each panel with two subpanels; the second subpanel contains the stuff that you want to make invisible by default and display later, and use setVisible() to do that.
I've never gotten comfortable with all of the GridBagConstraints, which seem to interact in ways for which I have no clear model. I avoid GridBagLayout for that reason, so I can't help do this with GridBagLayout. But I don't see why you can't do this with simpler (and less wordy) existing Swing containers and layout managers.
This is a real answer to the background question (how to get the effect you want):
import java.awt.Container;
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.JPanel;
public class LinesOneAndTwo implements ActionListener
{
private static final long serialVersionUID = 1L;
JFrame mainWindow = new JFrame("LinesOneandTwo");
JButton showButton = null;
JButton hideButton = null;
JPanel firstb = new JPanel();
public static void main(String[] args)
{
LinesOneAndTwo main = new LinesOneAndTwo();
main.go();
}
private void go()
{
mainWindow.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
Container contentPane = mainWindow.getContentPane();
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
JPanel first = new JPanel();
JPanel firsta = new JPanel();
JPanel second = new JPanel();
JPanel seconda = new JPanel();
JPanel secondb = new JPanel();
first.setLayout(new BoxLayout(first, BoxLayout.Y_AXIS));
firsta.setLayout(new BoxLayout(firsta, BoxLayout.X_AXIS));
showButton = new JButton("show");
hideButton = new JButton("hide");
firsta.add(showButton);
firsta.add(hideButton);
firstb.setLayout(new BoxLayout(firstb, BoxLayout.X_AXIS));
firstb.add(new JButton("one"));
firstb.add(new JButton("two"));
first.add(firsta);
first.add(firstb);
second.setLayout(new BoxLayout(second, BoxLayout.Y_AXIS));
seconda.setLayout(new BoxLayout(seconda, BoxLayout.X_AXIS));
secondb.setLayout(new BoxLayout(secondb, BoxLayout.X_AXIS));
seconda.add(new JButton("hiya"));
seconda.add(new JButton("there"));
secondb.add(new JButton("not here"));
second.add(seconda);
second.add(secondb);
secondb.setVisible(false);
firstb.setVisible(false);
showButton.addActionListener(this);
hideButton.addActionListener(this);
mainWindow.add(first);
mainWindow.add(second);
mainWindow.pack();
mainWindow.setVisible(true);
}
public void actionPerformed(ActionEvent event)
{
if (event.getSource() == showButton)
{
firstb.setVisible(true);
mainWindow.pack();
}
else if (event.getSource() == hideButton)
{
firstb.setVisible(false);
mainWindow.pack();
}
}
}
Of course, I don't know what's different about the internals of your panels, or how they differ otherwise, but the "show" and "hide" buttons cause the secondary panel within the top panel of the frame to appear and disappear, leaving no gaps.
I still don't like GridBagLayout.
I got the same problem and found a 70% Solution .
If you use rowWeights and set it to 0 for all but your last component (with rowWeights 1.0), nearly all spaces vanish. If your containing Component is larger than all the other components, there remains only the gap before the last component.
GridBagLayout gbl_panelFoo = new GridBagLayout();
gbl_panelFoo.rowWeights = new double[] {0.0, 0.0, 0.0, ... 0.0, 1.0};
Maybe this helps a little
Related
I want to position a JButton "Start" in the lower center of my GUI.
Sadly the GridBagConstraints that I set seem to have no effect.
I'm working with jdk1.8.0_191 and the Eclipse IDE.
EDIT: I switched my snippet to a "minimal reproducible example".
Here the snippet of my code:
package gui.main;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Test{
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(646, 509);
frame.setResizable(false);
frame.setTitle("Adventure Time");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 1;
gbc.gridy = 5;
JButton button = new JButton("Start");
button.setBackground(Color.black);
button.setForeground(Color.white);
button.setPreferredSize(new Dimension(110, 60));
button.setMinimumSize(new Dimension(110, 60));
button.setVisible(true);
panel.add(button, gbc);
panel.setVisible(true);
frame.add(panel);
frame.setVisible(true);
}
}
Here is a picture where I approximately want the "Start" Button instead.
I created a simple, runnable example of a GUI with a start button on the lower fourth of the JPanel. Here's the example GUI.
Use Swing components. Don't extend Swing components, or any other Java class, unless you intend to override one of the methods in the class.
I used a BorderLayout for the JPanel. I used a calculated empty border to shift the start button down from the center.
I set the size of the JPanel in this example. I'm assuming you'll set the size of the game panel based on the size of your image.
Here's the code.
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class StartButtonExample implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new StartButtonExample());
}
#Override
public void run() {
JFrame frame = new JFrame("Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createMainPanel(), BorderLayout.CENTER);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private JPanel createMainPanel() {
JPanel panel = new JPanel(new BorderLayout());
Dimension panelSize = new Dimension(400, 400);
panel.setPreferredSize(panelSize);
JButton button = new JButton("Start");
Dimension buttonSize = button.getPreferredSize();
int height = panelSize.height - buttonSize.height;
int width = panelSize.width - buttonSize.width;
int top = height * 3 / 4;
int left = (width - buttonSize.width) / 2;
int bottom = height / 4;
int right = left;
panel.setBorder(BorderFactory.createEmptyBorder(
top, left, bottom, right));
panel.add(button, BorderLayout.CENTER);
return panel;
}
}
In my amateurish opinion:
I ended up using different insets for my small application.
It is more clean looking in my opinion.
//margin 400 top, rest 3
gbc.insets = new Insets(400,3,3,3);
gbc.gridy = 0;
getWrapperPanel().add(getBtnStart02(), gbc);
//margin 3 at all sides for all following gui objects
gbc.insets = new Insets(3,3,3,3);
gbc.gridy = 1;
getWrapperPanel().add(getBtnLoad(), gbc);
Works like a charm.
I am attempting to make a "sentence randomizer" that, when a button is pressed, makes a grammatically correct sentence, that may not make any sense, by looping different types of words from a separate folder and separate files. It also alternates colors in each panel. I so far am able to get the JButton to show up, but I can't seem to figure out how to get the panels to appear? Here is my code so far for the UI:
package user_interface;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import code.sentence;
import user_interface.RandomButtonListener;
public class sentenceUI {
private sentence _s;
private JButton _rando;
public sentenceUI() {
_s = new sentence(this);
JFrame f = new JFrame("Ryan Ellis' Lab 9");
f.setLayout(new BoxLayout(f.getContentPane(), BoxLayout.Y_AXIS));
JPanel topPanel = new JPanel();
f.add(topPanel);
JPanel lowerPanel = new JPanel();
f.add(lowerPanel);
_rando = new JButton("Random Sentence");
_rando.addActionListener(new RandomButtonListener(_s, this));
lowerPanel.add(_rando);
Color c1 = Color.BLUE;
Color c2 = new Color( 255 - c1.getRed(), 255 - c1.getGreen(), 255 - c1.getBlue());
for(int i = 0; i < 8; i++){
JLabel _l = new JLabel();
_l.setBackground(c1);
_l.setForeground(c2);
Color temp = c1;
c1 = c2;
c2 = temp;
_l.setBorder(BorderFactory.createEmptyBorder(0,0,8,5));
_l.setFont(new Font("Comic Sans", Font.BOLD, 18));
topPanel.add(_l);
}
ArrayList<String> _slst = new ArrayList<String>();
_slst.add("WordLists/adjectives.txt");
_slst.add("WordLists/adverbs.txt");
_slst.add("WordLists/determiners.txt");
_slst.add("WordLists/nouns.txt");
_slst.add("WordLists/verbs.txt");
ArrayList<ArrayList<String>> list = new ArrayList<ArrayList<String>>();
list.add(_slst);
int i = 0;
list.get(i % 5);
f.add(topPanel, BorderLayout.PAGE_START);
f.add(lowerPanel, BorderLayout.PAGE_END);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
}
private void createRButton(String string, JPanel lowerPanel) {
createRButton("Random", lowerPanel);
}
You're adding topPanel twice to the JFrame, here
JPanel topPanel = new JPanel();
f.add(topPanel);
and here:
f.add(topPanel, BorderLayout.PAGE_START);
f.add(lowerPanel, BorderLayout.PAGE_END);
and in the 2nd addition, you're adding it as if the JFrame currently used a BorderLayout, but it's not since you've given it a BoxLayout.
Instead, only add the topPanel once, and in a logical way. Also consider giving your JLabel's some dummy text, such as " " so that they have some size when you first pack() your GUI.
Also, your labels are being added but they have no size and are non-opaque and so can't be seen. For example try this within your for loop to see for yourself:
JLabel _l = new JLabel("Label " + i); // to give labels size
_l.setOpaque(true); // so you can see the background color
_l.setBackground(c1);
_l.setForeground(c2);
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Ships {
public static JPanel init(JPanel radioPanel){
radioPanel.add(addShips(2));
radioPanel.add(addShips(3));
radioPanel.add(addShips(4));
radioPanel.add(addShips(5));
return radioPanel;
}
public static JButton addShips(int size){
JButton but = new JButton();
but.setPreferredSize(new Dimension((40*size),40));
but.setBackground(Color.BLACK);
return but;
}
public static void main(String[] args){
JFrame frame = new JFrame();
frame.setVisible(true);
JPanel radioPanel = new JPanel();
radioPanel.setLayout(new BoxLayout(radioPanel, BoxLayout.Y_AXIS)); \\line 4
init(radioPanel);
frame.getContentPane().add(radioPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
}
}
I see that the buttons are placed in one single line. Need to place the buttons one after the other according to BoxLayout.Y_AXIS. When I remove //line 4, it creates correctly according to FlowLayout.
Try this:
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Ships {
public static JPanel init(JPanel radioPanel){
radioPanel.add(addShips(2));
radioPanel.add(addShips(3));
radioPanel.add(addShips(4));
radioPanel.add(addShips(5));
return radioPanel;
}
public static JButton addShips(int size){
JButton but = new JButton();
Dimension d = new Dimension((40*size),40);
but.setPreferredSize(d);
but.setMinimumSize(d);
but.setMaximumSize(d);
but.setBackground(Color.BLACK);
return but;
}
public static void main(String[] args){
JFrame frame = new JFrame();
JPanel radioPanel = new JPanel();
radioPanel.setLayout(new BoxLayout(radioPanel, BoxLayout.Y_AXIS)); //line 4
init(radioPanel);
frame.getContentPane().add(radioPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
When you pack components in JFrame, your layout manager may not honour preferred size of components, try setting minimum and maximum sizes too.
In vertical layout (y-axis), BoxLayout tries to make all components wide as widest component. As there is no text or icon in all buttons, button will shrink to default size, and all will have same width. So instruct box layout for particular sizes using maximum and minimum sizes.
I have changed your LayoutManager to GridBagLayout and it works fine. Is it suitable for you? :
public class Ships {
public static JPanel init(JPanel radioPanel){
GridBagConstraints c = new GridBagConstraints();
c.anchor = GridBagConstraints.WEST;
radioPanel.add(addShips(2),c);
c.gridy = 1;
radioPanel.add(addShips(6),c);
c.gridy = 2;
radioPanel.add(addShips(4),c);
c.gridy = 3;
radioPanel.add(addShips(5),c);
return radioPanel;
}
public static JButton addShips(int size){
JButton but = new JButton();
but.setPreferredSize(new Dimension((40*size),40));
but.setBackground(Color.BLACK);
return but;
}
public static void main(String[] args){
JFrame frame = new JFrame();
frame.setVisible(true);
JPanel radioPanel = new JPanel();
radioPanel.setLayout(new GridBagLayout());
init(radioPanel);
frame.getContentPane().add(radioPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
}
}
EDIT: change horizontal to vertical align.
Result:
I am wondering about this since we are making a game in Swing and we made our map tiles into jButtons instead of jPanels for whatever reason. Now we want to put units on top of them so the map background is still shown when the unit is on top of them. Anyone know if this is possible?
OK, so I am not sure this is really what you are looking for and how your application is currently set up, but this is an example to have JButtons on top of each other (if this is not what you are looking for, consider posting an SSCCE and give more details). There are other alternatives and better way to handle such thing, but since you asked JButton over a JButton, here is a snippet showing that:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.plaf.ButtonUI;
import javax.swing.plaf.basic.BasicButtonUI;
public class TestButtonGrid {
protected int x;
protected int y;
protected void initUI() throws MalformedURLException {
final JFrame frame = new JFrame();
frame.setTitle(TestButtonGrid.class.getSimpleName());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel panel = new JPanel(new GridBagLayout());
ImageIcon icon = new ImageIcon(new URL("http://manhack-arcade.net/pivot/isdrawing/tile_bluerock.png"));
ImageIcon unit = new ImageIcon(new URL("http://static.spore.com/static/image/500/642/783/500642783372_lrg.png"));
ButtonUI ui = new BasicButtonUI();
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.fill = GridBagConstraints.BOTH;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
gbc.gridx = i;
gbc.gridy = j;
JButton button = new JButton(icon);
button.setBorderPainted(false);
button.setPreferredSize(new Dimension(icon.getIconWidth(), icon.getIconHeight()));
button.setUI(ui);
button.setOpaque(false);
panel.add(button, gbc);
if (i == j) {
// Choose a layout that will center the icon by default
button.setLayout(new BorderLayout());
JButton player = new JButton(unit);
player.setPreferredSize(new Dimension(unit.getIconWidth(), unit.getIconHeight()));
player.setBorderPainted(false);
player.setUI(ui);
player.setOpaque(false);
button.add(player);
}
}
}
frame.add(panel);
frame.setResizable(false);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
try {
new TestButtonGrid().initUI();
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
});
}
}
It might be a better idea to have a panel which contains buttons. Your panel could then use a mouse event listener to perform an action when clicked on.
The advantage of using a jPanel is that you can set a layout within it, which will make positioning your buttons in it much easier.
Well it is possible to put a JButton Over an other JButton you just wont be able to see it.
CardLayout cl = new CardLayout();
JPanel jpnlMain = new JPanel(cl);
jpnlMain.add(new JButton(), "FIRST");
jpnlMain.add(new JButton(), "SECOND");
Then Maybe you could create a Class that Extends Either the Cardlayout or the JPanel to make it show both Items.
EDIT
Made a little Test and HJere is a Button in a Button Buttonception!!
Main class:
import javax.swing.JFrame;
public class Test {
public static void main(String[] arg0){
SpecialButton sp = new SpecialButton();
JFrame jf = new JFrame();
jf.add(sp);
jf.setVisible(true);
jf.pack();
}
}
Special Button Class:
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class SpecialButton extends JButton{
SpecialButton(){
super();
JButton jbtnMid = new JButton();
JLabel jlblMid = new JLabel(new ImageIcon(this.getClass().getResource("/Images/arrowUpIcon.png")));
jbtnMid .add(jlblMid);
this.add(jbtnMid);
this.setVisible(true);
}
}
I'm having an issue in my application. I wish to dynamically create several "tabbed" scrollpanes that hold a semi-large amount of objects called "ImageLabels", which are just labels with (you guessed it) images on them.
I made most of my gui in Netbeans, as it is semi-complicated and I'm too nooby to do it myself.
My problem is this: When adding my scrollpane objects (that contain a JPanel with a GridLayout), and after I attempt to add my imagelabels to the JPanel with the layout (my constructor is set to "new GridLayout(0, 5, 5, 5);" which is unlimited rows, 5 columns, with 5 pxls of space.) nothing shows up, nor does the scrollpanel go into "scroll" mode.
I am completely at a loss and have been trying all day with different layouts. Here's some code I have...
Constructor of the ImageLabel object..
addMouseListener(this);
setVisible(true);
setPreferredSize(new Dimension(32, 32));
How I set up my tileHolderPanel JPanel (the panel holding the ImageLabels)
myLayout = new GridLayout(0, 5, 5, 5);
tileHolderPanel.setLayout(myLayout);
and after constructing all of the ImageLabels I wish to add to each panel, I simply do
tileHolderPanel.add(label);
If it matters, my TileHolderPanel is inside of a scrollpane which is inside a JPanel,and they have the default GroupLayouts which Netbeans generates.
Thank you,
-Luke
Here's a working example with which to compare your code.
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.GroupLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
/** #see http://stackoverflow.com/questions/7801870 */
public class ScrollGroup extends JPanel {
private static final int N = 8;
private static final int NN = N * N;
private static final int GAP = 5;
private static final int SIZE = 32;
public ScrollGroup() {
this.setLayout(new GridLayout(N, N, GAP, GAP));
for (int i = 0; i < NN; i++) {
JLabel label = new JLabel();
label.setOpaque(true);
label.setBackground(Color.getHSBColor((float) i / NN, 1, 1));
label.setPreferredSize(new Dimension(SIZE, SIZE));
this.add(label);
}
}
private void display() {
JFrame f = new JFrame("ScrollGroup");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JScrollPane sp = new JScrollPane(this);
GroupLayout layout = new GroupLayout(f.getContentPane());
f.setLayout(layout);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
layout.setHorizontalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup().addComponent(sp)));
layout.setVerticalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup().addComponent(sp)));
f.pack();
f.setSize(N * SIZE, N * SIZE);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new ScrollGroup().display();
}
});
}
}
I wish to dynamically create ...
When you add components to a visible GUI the basic code should be:
panel.add(...);
panel.revalidate();
If you need more help then you need to post an SSCCE.