JPanel inside JPanel in JAVA - java

public static void main(String[] args) {
JTextField text = new JTextField();
JFrame frame = new JFrame();
frame.setLayout(new BorderLayout());
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(5, 4));
JPanel panel2 = new JPanel();
panel2.setLayout(new FlowLayout());
JLabel imgLabel1 = new JLabel(new ImageIcon("C:\\Users\\Arthur\\Downloads\\abs.jpg"));
JLabel imgLabel2 = new JLabel(new ImageIcon("C:\\Users\\Arthur\\Downloads\\abss.jpg"));
imgLabel1.setPreferredSize(new Dimension(100,100));
imgLabel2.setPreferredSize(new Dimension(100,100));
panel2.add(imgLabel1);
panel2.add(imgLabel2);
for(int i=0; i<20; i++){
panel.add(panel2);
}
frame.add(text, BorderLayout.NORTH);
frame.add(panel, BorderLayout.CENTER);
frame.setPreferredSize(new Dimension(1280,700));
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
I want to make a memory game, I need to put two images in each cell of the JPanel of 4x5. For this I created a JPanel 1x2 with two images inside and put it in the JPanel of 4x5. But the result is:
Result:

So, if understand correctly, you're problem is, you're not seeing 20 new panels, only one.
The problem is, a component can only reside in a single container, once, so doing something like...
for (int i = 0; i < 20; i++) {
panel.add(panel2);
}
is the equivalent of doing something like...
panel.add(panel2);
You actually need to create a new instance of the component on each iteration of the loop
What I would suggest you do is create a "wrapper" or "card" panel which can contain the two images. In my testing I just used coloured panels, but you get the idea...
public class WrapperPane extends JPanel {
public WrapperPane() {
setLayout(new FlowLayout());
add(makePanel(Color.RED));
add(makePanel(Color.GREEN));
// This is just for demonstration purposes
setBorder(new LineBorder(Color.DARK_GRAY));
}
protected JPanel makePanel(Color background) {
JPanel panel = new JPanel();
panel.setBackground(background);
panel.setPreferredSize(new Dimension(100, 100));
return panel;
}
}
The you'd just have to do something like...
JTextField text = new JTextField();
JFrame frame = new JFrame();
frame.setLayout(new BorderLayout());
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(5, 4));
for (int i = 0; i < 20; i++) {
panel.add(new WrapperPane());
}
frame.add(text, BorderLayout.NORTH);
frame.add(panel, BorderLayout.CENTER);
// Don't do this, just let the content make it's own
// calculations
//frame.setPreferredSize(new Dimension(1280, 700));
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
And you'd end up with something like...

Related

How to for loop jPanel in jFrame?

May I know why my jPanel does not appear in the jFrame? I want to make 5 blue jPanel appear in the jFrame but why only 1 blue jPanel appear in my jFrame? Thanks for helping!
public class NewJFrame2 extends javax.swing.JFrame {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
/**
* Creates new form NewJFrame2
*/
public NewJFrame2() {
initComponents();
JPanel[] panelArray = new JPanel[5];
JButton btnArray[] = new JButton[5];
for(int i = 0; i<5;i++)
{
panelArray[i] = new JPanel();
//panelArray[i].setVisible(true);
System.out.println(panelArray[i]);
javax.swing.border.Border border = BorderFactory.createLineBorder(Color.BLUE, 5);
panelArray[i].setBackground(Color.YELLOW);
panelArray[i].setBorder(border);
frame.getContentPane().add(panelArray[i]);
}
frame.setSize(new Dimension(500, 400));
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("A Simple JFrame");
frame.setVisible(true);
}
As mentioned in the comments you want a LayoutManager.
The current issue is that you are adding all five panels to the exact same space on your frame. To solve this issue you need to provide a structure for the frame to associate different coordinates with different areas.
This answer contains a good jumping off point for you to start to play with layouts in Java.
Using a container JPanel with a BoxLayout -- see comments below for further info :
initComponents();
JPanel[] panelArray = new JPanel[5];
JButton btnArray[] = new JButton[5];
JPanel container = new JPanel(); // Container JPanel
container.setLayout(new BoxLayout(container, BoxLayout.X_AXIS)); // With a BoxLayout
for (int i = 0; i < 5; i++) {
panelArray[i] = new JPanel();
//panelArray[i].setVisible(true);
System.out.println(panelArray[i]);
javax.swing.border.Border border = BorderFactory.createLineBorder(Color.BLUE, 5);
panelArray[i].setBackground(Color.YELLOW);
panelArray[i].setBorder(border);
container.add(panelArray[i]); // Adding 5 JPanels to container JPanel
}
frame.getContentPane().add(container); // Adding container JPanel to JFrame
frame.setSize(new Dimension(500, 400));
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("A Simple JFrame");
frame.setVisible(true);

How can I fix blank screen on jframe and set values of vgap and hgap from textfield

How can I fix blank screen on jframe and set values of vgap and hgap from textfield. i am using borderlayout for this.
import java.awt.*;
import javax.swing.*;
public class d1{
public static void main(String[] args) {
JFrame f1 = new JFrame ("Border Layout") ;
f1.setLayout(new BorderLayout());
f1.setVisible(true);
f1.setSize(400,400);
f1.setLocationRelativeTo(null);
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextField t1 = new JTextField();
t1.setBorder(BorderFactory.createLineBorder(Color.BLACK));
JTextField t2 = new JTextField();
t2.setBorder(BorderFactory.createLineBorder(Color.BLACK));
JPanel p1 = new JPanel ();
p1.setLayout(new BorderLayout());
p1.add(new JButton("East"), BorderLayout.EAST);
p1.add(new JButton("South"), BorderLayout.SOUTH);
p1.add(new JButton("West"), BorderLayout.WEST);
p1.add(new JButton("North"), BorderLayout.NORTH);
p1.add(new JButton("Center"), BorderLayout.CENTER);
JPanel p2 = new JPanel ();
p2.setLayout(new BorderLayout());
JPanel p3 = new JPanel ();
p3.setLayout(new BorderLayout());
p3.add(new JLabel("Vgap"), BorderLayout.WEST);
p3.add(t1, BorderLayout.CENTER);
JPanel p4 = new JPanel ();
p4.setLayout(new BorderLayout());
p4.add(new JLabel("Hgap"), BorderLayout.WEST);
p4.add(t2, BorderLayout.CENTER);
JPanel p5 = new JPanel();
p5.setLayout(new BorderLayout());
p5.add(new JLabel("Container of BorderLayout"));
JPanel p6 = new JPanel();
p6.setLayout(new BorderLayout());
p6.add(new JLabel("BorderLayout Properties"));
JPanel p7 = new JPanel ();
p7.setLayout(new BorderLayout());
p7.add(p6, BorderLayout.NORTH);
p7.add(p2, BorderLayout.SOUTH);
p2.add(p3, BorderLayout.NORTH);
p2.add(p4, BorderLayout.SOUTH);
f1.add(p1, BorderLayout.CENTER);
f1.add(p7, BorderLayout.SOUTH);
f1.add(p5, BorderLayout.NORTH);
}
}
after this code.
There should be space between north-south and center, north, south and center.
I can not fix blank screen problem when frame is opened.
Add your panels before f1.setVisible(True) on frame and f1.pack() the frame after it.
You don't need to set a fixed size for your frame. Your added components should take care of it.
To set your components, look at MigLayout. It's easy to use and set components the way you need it.
please check this link you can find it here but first you should add ActionListener into your textField objects so that they can take numbers from inside of themselves. Providing white space in a Swing GUI
something like that
t1 = new JTextField();
t1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String v = t1.getText();
int numberhGap = Integer.parseInt(v);
}
});
t1.setBorder(BorderFactory.createLineBorder(Color.BLACK));
t2 = new JTextField();
t2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String h = t2.getText();
int numberhGap = Integer.parseInt(h);
}
});
and declare t1 and t2 outside main method.
like this
public class d1{
static JTextField t1;
static JTextField t2;

too much empty space inside jpanel with gridlayout

I have a JPanel and inside I use a GridLayout like this:
JPanel panel = new JPanel(new GridLayout(0, 1, 0, 0));
JPanel p1 = new JPanel(new FlowLayout());
JLabel label = new JLabel("SOMETHING");
JTextField tf = new JTextField(30);
JPanel p2 = new JPanel();
JTextArea txt = new JTextArea(6, 30);
JScrollPane sp = new JScrollPane(txt);
p1.add(label);
p1.add(tf);
p2.add(sp);
panel.add(p1);
panel.add(p2);
Unfortunately, the space between the JTextArea and the upper elements if very big.
What can I do to bring the JTextArea up?
http://img20.imageshack.us/img20/1086/screenshot1412201213550.png
Use BorderLayout and add the top panel to NORTH and the scroll pane to the CENTER.
Screenshot of the code below:
public static void main(String[] args) {
JFrame frame = new JFrame("Test");
frame.add(new JPanel(new FlowLayout()) {{
add(new JLabel("something"));
add(new JTextField(30));
}}, BorderLayout.NORTH);
frame.add(new JScrollPane(new JTextArea(6, 30)), BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}

JScrollPane for multiple labels

I am working on a program that has a flow layout, inside are a set of labels and because there are so many they do not all display. Is there anyway to add a scroll pane to scroll through all of these labels horizontally?
JPanel mainpanel = new JPanel();
mainpanel.setLayout(new BoxLayout(mainpanel, BoxLayout.X_AXIS));
pane.add(mainpanel, BorderLayout.NORTH);
JPanel rightpanel = new JPanel();
rightpanel.setLayout(new FlowLayout());
for (int i = 0; i < 100; i++)
{
rightpanel.add(new JLabel("Label " + i));
}
mainpanel.add(new JLabel("Left label"));
mainpanel.add(new JScrollPane(rightpanel));
I'd suggest ot use JList or JTable with one Column or Row (depends or direction), Object in the JList or JTable is JLabel/JComponent by default
Not sure what your question really is, since you already know you need to use a JScrollPane. How about:
public class ScrollLabels
{
public static void main(String[] args)
{
JFrame frame = new JFrame("Labels");
JPanel mainpanel = new JPanel();
mainpanel.setLayout(new BoxLayout(mainpanel, BoxLayout.X_AXIS));
frame.add(mainpanel);
JPanel rightpanel = new JPanel();
rightpanel.setLayout(new FlowLayout());
for (int i = 0; i < 100; i++)
{
rightpanel.add(new JLabel("Label " + i));
}
mainpanel.add(new JLabel("Left label"));
mainpanel.add(new JScrollPane(rightpanel));
frame.setSize(500, 100);
frame.setVisible(true);
}
}

How can I set size of a button?

I put my buttons in a JPane with GridLayout. Then I put JPanel into another JPanel with BoxLayout.Y_AXIS. I want buttons in the GridLayout to be square. I use tmp.setSize(30,30) and it does not work. I also try to use new GridLayout(X, Y, 4, 4) but I cannot figure out what X and Y are. So, what is the correct way to do this stuff?
ADDED:
I still cannot solve the problem. Here is the code of what I am trying to do:
import javax.swing.*;
import java.awt.*;
public class PanelModel {
public static void main(String[] args) {
JFrame frame = new JFrame("Colored Trails");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
JPanel firstPanel = new JPanel();
firstPanel.setLayout(new GridLayout(4, 4));
JButton btn;
for (int i=1; i<=4; i++) {
for (int j=1; j<=4; j++) {
btn = new JButton();
btn.setPreferredSize(new Dimension(100, 100));
firstPanel.add(btn);
}
}
JPanel secondPanel = new JPanel();
secondPanel.setLayout(new GridLayout(5, 13));
for (int i=1; i<=5; i++) {
for (int j=1; j<=13; j++) {
btn = new JButton();
btn.setPreferredSize(new Dimension(40, 40));
secondPanel.add(btn);
}
}
mainPanel.add(firstPanel);
mainPanel.add(secondPanel);
frame.add(mainPanel);
frame.setSize(400,600);
frame.setVisible(true);
}
}
The problem is that Java tries to make width of the firstPanel and secondPanel equal! Moreover, Java tries to to fill all height of the window. How can I remove this behavior?
The following bit of code does what you ask for. Just make sure that you assign enough space so that the text on the button becomes visible
JFrame frame = new JFrame("test");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new GridLayout(4,4,4,4));
for(int i=0 ; i<16 ; i++){
JButton btn = new JButton(String.valueOf(i));
btn.setPreferredSize(new Dimension(40, 40));
panel.add(btn);
}
frame.setContentPane(panel);
frame.pack();
frame.setVisible(true);
The X and Y (two first parameters of the GridLayout constructor) specify the number of rows and columns in the grid (respectively). You may leave one of them as 0 if you want that value to be unbounded.
Edit
I've modified the provided code and I believe it now conforms to what is desired:
JFrame frame = new JFrame("Colored Trails");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
JPanel firstPanel = new JPanel();
firstPanel.setLayout(new GridLayout(4, 4));
firstPanel.setMaximumSize(new Dimension(400, 400));
JButton btn;
for (int i=1; i<=4; i++) {
for (int j=1; j<=4; j++) {
btn = new JButton();
btn.setPreferredSize(new Dimension(100, 100));
firstPanel.add(btn);
}
}
JPanel secondPanel = new JPanel();
secondPanel.setLayout(new GridLayout(5, 13));
secondPanel.setMaximumSize(new Dimension(520, 200));
for (int i=1; i<=5; i++) {
for (int j=1; j<=13; j++) {
btn = new JButton();
btn.setPreferredSize(new Dimension(40, 40));
secondPanel.add(btn);
}
}
mainPanel.add(firstPanel);
mainPanel.add(secondPanel);
frame.setContentPane(mainPanel);
frame.setSize(520,600);
frame.setMinimumSize(new Dimension(520,600));
frame.setVisible(true);
Basically I now set the preferred size of the panels and a minimum size for the frame.
Try with setPreferredSize instead of setSize.
UPDATE: GridLayout take up all space in its container, and BoxLayout seams to take up all the width in its container, so I added some glue-panels that are invisible and just take up space when the user stretches the window. I have just done this horizontally, and not vertically, but you could implement that in the same way if you want it.
Since GridLayout make all cells in the same size, it doesn't matter if they have a specified size. You have to specify a size for its container instead, as I have done.
import javax.swing.*;
import java.awt.*;
public class PanelModel {
public static void main(String[] args) {
JFrame frame = new JFrame("Colored Trails");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
JPanel firstPanel = new JPanel(new GridLayout(4, 4));
firstPanel.setPreferredSize(new Dimension(4*100, 4*100));
for (int i=1; i<=4; i++) {
for (int j=1; j<=4; j++) {
firstPanel.add(new JButton());
}
}
JPanel firstGluePanel = new JPanel(new BorderLayout());
firstGluePanel.add(firstPanel, BorderLayout.WEST);
firstGluePanel.add(Box.createHorizontalGlue(), BorderLayout.CENTER);
firstGluePanel.add(Box.createVerticalGlue(), BorderLayout.SOUTH);
JPanel secondPanel = new JPanel(new GridLayout(13, 5));
secondPanel.setPreferredSize(new Dimension(5*40, 13*40));
for (int i=1; i<=5; i++) {
for (int j=1; j<=13; j++) {
secondPanel.add(new JButton());
}
}
JPanel secondGluePanel = new JPanel(new BorderLayout());
secondGluePanel.add(secondPanel, BorderLayout.WEST);
secondGluePanel.add(Box.createHorizontalGlue(), BorderLayout.CENTER);
secondGluePanel.add(Box.createVerticalGlue(), BorderLayout.SOUTH);
mainPanel.add(firstGluePanel);
mainPanel.add(secondGluePanel);
frame.getContentPane().add(mainPanel);
//frame.setSize(400,600);
frame.pack();
frame.setVisible(true);
}
}
GridLayout is often not the best choice for buttons, although it might be for your application. A good reference is the tutorial on using Layout Managers. If you look at the GridLayout example, you'll see the buttons look a little silly -- way too big.
A better idea might be to use a FlowLayout for your buttons, or if you know exactly what you want, perhaps a GroupLayout. (Sun/Oracle recommend that GroupLayout or GridBag layout are better than GridLayout when hand-coding.)
This is how I did it.
JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("SAP Multiple Entries");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new GridLayout(10,10,10,10));
frame.setLayout(new FlowLayout());
frame.setSize(512, 512);
JButton button = new JButton("Select File");
button.setPreferredSize(new Dimension(256, 256));
panel.add(button);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
JFileChooser fileChooser = new JFileChooser();
int returnValue = fileChooser.showOpenDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
keep = selectedFile.getAbsolutePath();
// System.out.println(keep);
//out.println(file.flag);
if(file.flag==true) {
JOptionPane.showMessageDialog(null, "It is done! \nLocation: " + file.path , "Success Message", JOptionPane.INFORMATION_MESSAGE);
}
else{
JOptionPane.showMessageDialog(null, "failure", "not okay", JOptionPane.INFORMATION_MESSAGE);
}
}
}
});
frame.add(button);
frame.pack();
frame.setVisible(true);

Categories