Adding a set of GroupLayout into JPanel - java

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);
}

Related

Panel fills the window in Java swing

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.

Sudoku Gui Issues

I am required to create a Suduku Game Board that looks like this:
Here are the requirements I need for this assignment, but am having some issues.
Use two for loops to draw the text fields instead of brute-force of listing 81 text fields. You should do something like:
for (int k = 1; k <= 9; k++)
{
JPanel level2 = new JPanel();
….
for (int i = 1; i <= 9; i++)
{
JTextField text = new JTextField();
…
}
gridPanel.add(level2);
}
I need 2 classes in
an application class named TestSudoku and a work class named SudokuLayout.
Implement the following visual gadgets and write listeners for them. These gadgets have the following behaviors:
Button “Reset”---when the button is clicked, the program will clear the text area, then output the string “Reset button clicked!” to the text area.
Button “Hint”---when the button is clicked, the program will clear the text area, then output the string “Hint button clicked!” to the text area.
Combobox “Difficulty”---when an item is selected, the program will clear the text area, then output the selected item name to the text area.
implement the listeners using loosely coupled methods (private listener class or private adapter class).
This is what I currently have..
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
public class SudokuLayout extends JFrame {
public SudokuLayout() {
JPanel board = new JPanel(new GridLayout(9, 9));
add(board);
JPanel[][] squares = new JPanel[9][9];
Border border = BorderFactory.createLineBorder(Color.BLACK);
for (int row = 1; row < 9; row++) {
for (int col = 1; col < 9; col++) {
squares[row][col] = new JPanel();
board.add(squares[row][col]);
}
}
JPanel menu = new JPanel();
menu.add(new JButton("Reset"));
menu.add(new JButton("Hint"));
menu.add(new JButton("Solve"));
menu.add(new JButton("New Puzzle"));
add(menu);
}
public static void main(String[] args) {
/** Create a frame and set its properties*/
JFrame frame = new SudokuLayout();
frame.setTitle("Sudoku");
frame.setSize(600, 600);
frame.setLocationRelativeTo(null); //Center the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
The problem is that, with my current version, the right menu shows horizontally and I cannot see the grid. Moreover, I don't know how to add the output area.
When facing this kind of frames with Swing you need to divide the frame in sections and treat them separately. I mean, by looking at the image you can easily identify the Sudoku, the menu, and the output. Thus, your answer should try to first create each of them separately and then join them.
Considering this, you must notice:
There are 3 main components: sudoku, menu, and output
The 3 main components may use a BorderLayout since they are probably at WEST, EAST and SOUTH
The sudoku component is a 3x3 grid of 3x3 smaller grids (so you can change the black-border).
The menu component is a 5x1 grid with buttons in each position
The output component is a single text frame
So, you may need to change something to get the exact behaviour (button sizes and margins, options on the difficulty ComboBox) but your solution should look like this:
public class SudokuLayout extends JFrame {
public SudokuLayout() {
// Create panel for Sudoku
JPanel board = new JPanel();
board.setLayout(new GridLayout(3, 3));
board.setBorder(BorderFactory.createLineBorder(Color.BLACK));
for (int row = 0; row < 3; ++row) {
for (int col = 0; col < 3; ++col) {
JPanel box = new JPanel(new GridLayout(3, 3));
box.setBorder(BorderFactory.createLineBorder(Color.BLACK));
for (int cell = 0; cell < 9; ++cell) {
box.add(new JTextField(2));
}
board.add(box);
}
}
// Create difficulty combo box
JComboBox<String> difficultyChoices = new JComboBox<>(new String[] { "Hard", "Easy" });
difficultyChoices.setSelectedIndex(0);
// Create menu panel
JPanel menu = new JPanel();
menu.setLayout(new GridBagLayout());
GridBagConstraints menuConstraints = new GridBagConstraints();
menuConstraints.anchor = GridBagConstraints.WEST;
menuConstraints.weightx = 0.5;
menuConstraints.weighty = 0.5;
menuConstraints.gridwidth = 2;
menuConstraints.gridx = 2;
menuConstraints.gridy = 0;
menu.add(new JButton("Reset"), menuConstraints);
menuConstraints.gridx = 2;
menuConstraints.gridy = 1;
menu.add(new JButton("Hint"), menuConstraints);
menuConstraints.gridx = 2;
menuConstraints.gridy = 2;
menu.add(new JButton("Solve"), menuConstraints);
menuConstraints.gridx = 2;
menuConstraints.gridy = 3;
menu.add(new JButton("New Puzzle"), menuConstraints);
menuConstraints.weighty = 1.0;
menuConstraints.gridx = 2;
menuConstraints.gridy = 4;
menu.add(new JLabel("Difficulty:"), menuConstraints);
menuConstraints.fill = GridBagConstraints.HORIZONTAL;
menuConstraints.weightx = 0.5;
menuConstraints.weighty = 0.5;
menuConstraints.gridwidth = 2;
menuConstraints.gridx = 0;
menuConstraints.gridy = 5;
menu.add(difficultyChoices, menuConstraints);
// Create output panel
JTextArea output = new JTextArea(5, 20);
output.setEditable(false);
output.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.BLUE), "Output Area"));
// Join the 3 panels on the frame
Container cp = getContentPane();
cp.setLayout(new BorderLayout());
cp.add(board, BorderLayout.WEST);
cp.add(menu, BorderLayout.EAST);
cp.add(output, BorderLayout.SOUTH);
}
public static void main(String[] args) {
// Create a frame and set its properties
JFrame frame = new SudokuLayout();
frame.setTitle("TestSudoku");
frame.setSize(600, 600);
frame.setLocationRelativeTo(null); // Center the frame
// Setup the window
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
UPDATE: I update my answer with a GridBagLayout for the menu and a TextArea with border for the output.

i have added the scroll bar but it does not scroll up and down the frame

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);
}

Creating Grid on my Image

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.

JPanel of JPanels in Java

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();

Categories