I am trying to develop a Jpanel of JPanels in Java
the outer JPanel has Layout FlowLayout.LEFT and the inner JPanels have Layout FlowLayout.LEADING
i added a labels to the inner jpanels
the should add to the outer panel as a Columns and rows of lables
but that does not work the labels added to the outer panel as a one row i mean left FowLayout for all
I stored the labels in a linkedlist of linkedlist of labels called board
here is the code for filling the labels linked list
for(int i =0 ; i <7 ;i++)
{
LinkedList<JLabel> list =new LinkedList<JLabel>();
for(int j=0 ; j< 5; j++)
{
JLabel lab = new JLabel();
lab.setIcon(add_icon);
lab.addMouseListener(listener);
lab.setTransferHandler(new TransferHandler("icon"));
list.add(lab);
}
board.add(list);
}
then i added the labels to the outer jpanel called container
Here is the code
container.setLayout(new FlowLayout(FlowLayout.LEFT));
for(int i =0 ; i < board.size() ;i++)
{
panel =new JPanel();
panel.setLayout(new FlowLayout(FlowLayout.LEADING));
for(int j=0 ; j< board.get(i).size(); j++)
{
panel.add(board.get(i).get(j));
}
container.add(panel);
//
}
add(container);
validate();
repaint();
here is the output it seems that the flow-layout leading does not work
How can i do it ? What should i do to add the labels as rows and columns ?
FlowLayout can't stack components vertically. Use BoxLayout instead. Try the following code:
container.setLayout(new FlowLayout(FlowLayout.LEFT));
for(int i = 0; i < board.size(); i++){
panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
for(int j = 0; j < board.get(i).size(); j++)
panel.add(board.get(i).get(j));
container.add(panel);
}
add(container);
validate();
repaint();
I found it can be done by usuing GridBagLayout
container.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill=GridBagConstraints.HORIZONTAL;
for(int i =0 ; i < board.size() ;i++)
{
panel =new JPanel();
panel.setLayout(new GridBagLayout());
GridBagConstraints c1 = new GridBagConstraints();
c1.fill=GridBagConstraints.VERTICAL;
c1.weighty=-.9;
// panel.setBounds(0, 0, 400, 300);
for(int j=0 ; j< board.get(i).size(); j++)
{
c1.gridx = j;
c1.gridy = 0;
c1.insets= new Insets(-5,0,0,-5);
panel.add(board.get(i).get(j),c1);
}
c.weightx =- 5;
c.gridx = 0;
c.gridy =i ;
c.insets=new Insets(-5,0,-5,0);
container.add(panel,c);
//
}
add(container);
validate();
repaint();
Related
I've added two 10x10 grids in a window but I cannot get them to appear in a smaller size. They just fill the entire window. How do I place both of them neatly in the middle in a smaller size so that there's room for some labels and buttons? I've pasted the code below for reference.
public static void main(String[] args) {
window = new JFrame();
window.setTitle("Battleship.exe");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setPreferredSize(new Dimension(600, 600));
P1_container = new JPanel(new GridLayout(10,10));
P1_container.setBorder(BorderFactory.createLineBorder(Color.black, 5));
compContainer = new JPanel(new GridLayout(10,10));
compContainer.setBorder(BorderFactory.createLineBorder(Color.black, 5));
grid = new JPanel[10][10];
for (int i =0; i< 10; i++) {
for (int j =0; j< 10; j++) {
grid[i][j] = new JPanel();
grid[i][j].setBackground(Color.white);
grid[i][j].setBorder(BorderFactory.createLineBorder(Color.blue, 2));
grid[i][j].setPreferredSize(new Dimension(25,25));
P1_container.add(grid[i][j]);
}
}
enemyGrid = new JPanel[10][10];
for (int i =0; i< 10; i++) {
for (int j =0; j< 10; j++) {
enemyGrid[i][j] = new JPanel();
enemyGrid[i][j].setBackground(Color.white);
enemyGrid[i][j].setBorder(BorderFactory.createLineBorder(Color.red, 2));
enemyGrid[i][j].setPreferredSize(new Dimension(25, 25));
compContainer.add(enemyGrid[i][j]);
}
}
GridLayout layout = new GridLayout(1, 2);
layout.setHgap(150);
mainPanel = new JPanel(layout);
mainPanel.add(P1_container);
mainPanel.add(compContainer);
window.add(mainPanel);
window.pack();
window.setVisible(true);
}
Your code had 24 compile errors. Once I fixed the compile errors, I modified one line and commented out one line to get this GUI.
Here's the complete runnable code.
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class BattleshipGUI {
public static void main(String[] args) {
JFrame window = new JFrame();
window.setTitle("Battleship.exe");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// window.setPreferredSize(new Dimension(600, 600));
JPanel P1_container = new JPanel(new GridLayout(10,10));
P1_container.setBorder(BorderFactory.createLineBorder(Color.black, 5));
JPanel compContainer = new JPanel(new GridLayout(10,10));
compContainer.setBorder(BorderFactory.createLineBorder(Color.black, 5));
JPanel[][] grid = new JPanel[10][10];
for (int i =0; i< 10; i++) {
for (int j =0; j< 10; j++) {
grid[i][j] = new JPanel();
grid[i][j].setBackground(Color.white);
grid[i][j].setBorder(BorderFactory.createLineBorder(Color.blue, 2));
grid[i][j].setPreferredSize(new Dimension(25,25));
P1_container.add(grid[i][j]);
}
}
JPanel[][] enemyGrid = new JPanel[10][10];
for (int i =0; i< 10; i++) {
for (int j =0; j< 10; j++) {
enemyGrid[i][j] = new JPanel();
enemyGrid[i][j].setBackground(Color.white);
enemyGrid[i][j].setBorder(BorderFactory.createLineBorder(Color.red, 2));
enemyGrid[i][j].setPreferredSize(new Dimension(25, 25));
compContainer.add(enemyGrid[i][j]);
}
}
GridLayout layout = new GridLayout(0, 2);
layout.setHgap(150);
JPanel mainPanel = new JPanel(layout);
mainPanel.add(P1_container);
mainPanel.add(compContainer);
window.add(mainPanel);
window.pack();
window.setVisible(true);
}
}
The key to using Swing layout managers is to nest your containers.
Nest mainPanel into another JPanel, one that uses a layout that helps you achieve your goal, say a BorderLayout with mainPanel placed into the BorderLayout.CENTER position. Then add the other gui components to this same outer JPanel at other BorderLayout locations.
If you don't want the mainPanel to expand, then use a different outer layout that does not expand, such as FlowLayout.
The button on the Panel was supposed to be below the last row of ovals but what it does was add a column:
Here is my Code
Panel p1 = new Panel();
JButton shiftLeft = new JButton("Shift Left");
JButton shiftRight = new JButton("Shift Right");
p1.setLayout(new GridLayout(Rows, Columns));
for (int i=0; i<Rows; i++) {
for (int j = 0; j < Columns; j++) {
arcs[i][j] = new ArcsPanel(i, j);
p1.add(arcs[i][j]);
arcs[i][j].addMouseListener(me);
}
}
p1.add(shiftRight);
add(p1, BorderLayout.CENTER);
this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setVisible(true);
The button on the Panel was supposed to be below the last row of ovals
A GridLayout always adds components in rows/columns.
If you want the button separate from the GridLayout then you need to add the button to the frame directly:
//p1.add(shiftRight);
add(p1, BorderLayout.CENTER);
add(shiftRight, BorderLayout.PAGE_END);
Or if you don't want the button resized, then you need to wrap it in another panel first:
JPanel buttonPanel = new JPanel();
buttonPanel.add( shiftRight );
add(buttonPanel, BorderLayout.PAGE_END);
The point is to achieve desired layout you can use multiple panels each with different layouts. You are not force to use a single panel or layout manager.
i have added the scroll bar but it does not scroll up and down the frame and i do not see the upper label i have created .
kindly give me any suggestion so i can scroll up and down in my frame.
i have a question also that when my frame size is fixed and i want to create
many labels and text fields that i must have to scroll down so in that case i have to increase its frame size or not
public static void main(String[] args)
{
JFrame frame = new JFrame("jframe");
JLabel[] labels=new JLabel[50];
for (int i=0;i<labels.length;i++)
{
labels[i]=new JLabel("Column" + i);
}
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints cst = new GridBagConstraints();
JScrollBar vbar=new JScrollBar(JScrollBar.VERTICAL, 30, 40, 0, 500);
for(int i =0 ; i<50 ;i++)
{
cst.fill = GridBagConstraints.HORIZONTAL;
cst.gridx = 0;
cst.gridy = i;//
cst.gridwidth = 2;
panel.add(labels[i],cst);
}
frame.getContentPane().add(vbar, BorderLayout.EAST);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1300,700);
frame.getContentPane().add(panel);
frame.setVisible(true);
}
why don't you use a JScrollPane and put your panel on it?
take a look at: https://docs.oracle.com/javase/tutorial/uiswing/components/scrollpane.html
public static void main(String[] args)
{
JFrame frame = new JFrame("jframe");
JLabel[] labels = new JLabel[50];
for(int i = 0; i < labels.length; i++)
{
labels[i] = new JLabel("Column" + i);
}
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints cst = new GridBagConstraints();
// JScrollBar vbar = new JScrollBar(JScrollBar.VERTICAL,30,40,0,500);
for(int i = 0; i < 50; i++)
{
cst.fill = GridBagConstraints.HORIZONTAL;
cst.gridx = 0;
cst.gridy = i;//
cst.gridwidth = 2;
panel.add(labels[i],cst);
}
JScrollPane scrollPane = new JScrollPane(panel);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
frame.getContentPane().add(scrollPane,BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1300,700);
frame.setVisible(true);
}
I am currently trying to create a moving character in a Map using AWT and Swings.
I need help on trying to create a GridLayout on my Image. I have tried using setLayout(new GridLayout()) and it just doesn't work.
If possible, I would also like to incorporate a KeyListener to this.
Currently, Here's my Code for the Image:
public class GUI extends JFrame{
GUI(){
setLayout(new FlowLayout());
ImageIcon image1 = new ImageIcon("path-to-backgroundImage");
JLabel label1 = new JLabel(image1);
add(label1);
}
public static void main(String[] args) {
GUI gui = new GUI();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setVisible(true);
gui.pack();
gui.setTitle("Map");
}
}
And Here's the GridLayout I had with the code, but it doesn't work:
setLayout(new GridLayout(ROWS, COLS));
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
JLabel label = new JLabel(cellIcon);
add(label);
}
}
Help is always highly appreciated!
JLabel label1 = new JLabel(image1);
add(label1);
You add the label to the frame, which is reasonable:
setLayout(new GridLayout(ROWS, COLS));
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
JLabel label = new JLabel(cellIcon);
add(label);
}
}
But then you change the layout of the frame and add more labels to the frame. Your code doesn't make sense. If you want to add the grid of labels to the background image then the code should be:
label1.setLayout(new GridLayout(ROWS, COLS));
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
JLabel label = new JLabel(cellIcon);
label1.add(label);
}
}
If you need more help then poste a proper SSCCE that demonstrates the problem. The random lines of code don't tell us the context of how the code is used.
I have a GroupLayout object
GroupLayout uniquechart[] = new GroupLayout[1000];
That object saves a GroupLayout set, to draw this into JPanelResult I'm doing the following, but I can't put the charts vertically (uniquechart[0] at the top and below uniquechart[1], and so).
for (int m = 0; m < disrup.countInterruptions(); m++) {
JPanelResult.setLayout(uniquechart[m]);
}
UPDATE 1
Working with JPanels
verticalPanel = new javax.swing.JPanel();
JPanel uniquechart[] = new JPanel[1000];
verticalPanel.setLayout(new FlowLayout());
verticalPanel.setLayout(new BoxLayout(verticalPanel, BoxLayout.Y_AXIS));
for (int m = 0; m < disrup.countInterruptions(); m++) {
verticalPanel.add(uniquechart[m]);
jScrollPane4.setViewportView(verticalPanel);
}