i am trying to design a JFrame with 2 different JPanels in it, one on the left with AbsoluteLayout and one on the right with a GridLayout with variable dimensions.
After adding some components to the JPanels, i add them to the JFrame contentPane and use the method JFrame.pack() hoping to get a JFrame with the minimum size possible that can show all of its components, but what i am getting is the minimum size to show only the JPanel on the right with the GridLayout, the JPanel on the left gets overlapped by the one on the right.
Is there any good way to use the JFrame.pack() method and it still shows both JPanel completly?
Here is the code:
public class GameGUI extends JFrame{
private int labSize;
private JFrame mainFrame;
private JPanel labPanel;
private JPanel choicesPanel;
private JButton exitButton;
private JButton replayButton;
public GameGUI(int n) {
labSize=n;
mainFrame = new JFrame("Maze Game");
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.getContentPane().setLayout(new GridLayout(1, 0));
mainFrame.setVisible(false);
labPanel=new JPanel(new GridLayout(labSize,labSize));
choicesPanel=new JPanel(new GridLayout(0, 1));
choicesPanel.setLayout(null);
replayButton=new JButton("Replay");
replayButton.setBounds(10, 11, 80, 30);
exitButton=new JButton("Exit");
exitButton.setBounds(10, 51, 80, 30);
choicesPanel.add(replayButton);
choicesPanel.add(exitButton);
mainFrame.getContentPane().add(choicesPanel);
mainFrame.getContentPane().add(labPanel);
}
public void refreshLabShowing(char[][] lab){
labPanel.removeAll();
for(int i=0;i<labSize;i++){
for(int u=0;u<labSize;u++){
labPanel.add(new JLabel(String.valueOf(lab[i][u])));
}
}
mainFrame.pack();
mainFrame.setVisible(true);
}
}
pack() will only work if with layout managers, as pack() itself queries layout manager for required dimension so here, you cannot use absolute layout and pack().
Related
I am trying to make a minesweeper that has a different space for a smiley icon that we can click and the buttons which we have to click to play.
public final class testFrame extends JFrame implements MouseListener, ActionListener {
private JFrame screen = null;
private JPanel composite = new JPanel();
public testFrame() {
screen = new JFrame();
screen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
screen.setVisible(true);
screen.setResizable(true);
composite.setLayout(new BorderLayout());
//this button is not showing also
JButton button = new JButton("Text goes here");
composite.add(button);
Container cp = screen.getContentPane(); // JFrame's content-pane
cp.setLayout(new GridLayout(5, 5, 2, 2)); // in 10x10 GridLayout
//codes to add buttons
}
So here I am trying to add the container cp to the screen. But it opened
two screen
Sorry if this seems like minor things but I am really new to this java GUI so please help me.
EDIT:
I removed the extends JFrame and used the screen instead. It kinda works but I can't seem to put the container cp to a panel. The requirement is that I have to use container cp. So I cannot change. Thank you
public final class TestFrame implements MouseListener, ActionListener {
private JFrame screen = null;
private JPanel composite = new JPanel();
private JPanel topPanel = new JPanel();
public TestFrame() {
screen = new JFrame("TestFrame");
screen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
screen.setVisible(true);
topPanel.setLayout(new BorderLayout());
//composite.setLayout(new BorderLayout());
//button in topPanel
JButton button = new JButton("Text goes here");
topPanel.add(button, BorderLayout.PAGE_START);
//Content Pane
Container cp = screen.getContentPane();// JFrame's content-pane
cp.setLayout(new GridLayout(5, 5, 2, 2)); // in 10x10 GridLayout
//composite.add(cp, BorderLayout.CENTER);
screen.add(topPanel);
// screen.add(composite);
}
Now it looks like
this
There are a few things.
You are extending JFrame as well as using it as an attribute of the same class so you can ether use this instead of screen or you remove the extends JFrame as it is redundant and not needed
To answer your question. A JFrame contains a Panel called ContentPane this pane is were you add you panels to (you already did the layout thing right). So the solution is:
cp.add(composite);
You've got too many JFrames, the testFrame class which extends JFrame (and which should be re-named TestFrame to comply with Java naming conventions) and the screen variable. Use only one.
You can and should nest JPanels to achieve your desired result. For instance if you want a grid as well as some control buttons, create a JPanel, give it a BorderLayout, put your grid JPanel in the BorderLayout.CENTER postion and a JPanel with control JButtons in a different position, say BorderLayout.PAGE_START
I am adding components in JPanel which is set as FlowLayout, they are not moving on next line even there is no space in left in that line.
Here is the screenshot of the problem
import javax.swing.*;
import java.awt.*;
import javax.swing.border.BevelBorder;
import javax.swing.border.TitledBorder;
public class GUI extends JFrame
{
private JLabel jlfname;
private JPanel p1;
private JTextField t1;
private JLabel jllname;
private JTextField t2;
private JLabel jltitle;
private JTextField t3;
GUI()
{
jlfname=new JLabel("First Name : ");
p1=new JPanel();
TitledBorder titled = new TitledBorder("Name");
p1.setBorder(titled);
t1=new JTextField(10);
jllname=new JLabel("Last Name : ");
t2=new JTextField(10);
jltitle=new JLabel("Title : ");
t3=new JTextField(10);
//Add in Pannel
p1.setLayout(new FlowLayout());
p1.add(jlfname);
p1.add(t1);
p1.add(jllname);
p1.add(t2);
p1.add(jltitle);
p1.add(t3);
//Add in Frame
add(p1);
setSize(550,500);
setTitle("JFrame Tutorial");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout(FlowLayout.LEFT));
setResizable(false);
setVisible(true);
}
public static void main(String [] args)
{
new GUI();
}
}
I have also tried to set width of the panel but it doesn't work!
FlowLayout is designed to calculate its preferred size based on all components being displayed on a single line. The FlowLayout also respects the preferred size of components.
setLayout(new FlowLayout(FlowLayout.LEFT));
You are overriding the default layout manager of the frame, so now the frame will respect the preferred size of the panel added to the frame, which means all the components will be displayed on a single line.
Get rid of that statement.
Now the components will be able to wrap in the space available because by default the panel will be added to the BorderLayout.CENTER which takes up all the space available in the frame.
However the above solution will only work when components are added to the CENTER of the BorderLayout. Normally you should not be using setSize() but instead use pack() to all a frame to display at its preferred size.
For a more flexible layout that will calculate a proper preferred size of a panel check out the Wrap Layout. This class extends FlowLayout to calculate the preferred size.
I have created a password generator in Java which works perfectly. My functionality is working (after a lot of tears, sweat and blood :) ), but the only problem that remains is the layout of my GUI.
My approach was the following:
1 pane (Top/North) for the title
1 pane in the center for the form
1 pane (bottom/south) for the buttons and textfield
This is the result
as you can see this is not how I wanted it. But if I look at my code, it should be placed nicely from top to bottom.
Where did it go wrong?
My code:
The constructor (extends from JFrame)
public PasswordGenerator(){
this.setContentPane(ContentPane());
this.setSize(500,270);
this.setResizable(true);
this.setVisible(true);
}
The panes:
private JPanel ContentPane()
{
JPanel ContentPane = new JPanel();
ContentPane.add(getTopPane(), BorderLayout.NORTH);
ContentPane.add(getCenterPane(),BorderLayout.CENTER);
ContentPane.add(getSouthPane(),BorderLayout.EAST);
return ContentPane;
}
private JPanel getTopPane(){
JPanel TopPane = new JPanel();
JLabel intro = new JLabel("Password generator V1.0");
intro.setFont(new Font("TimesRoman",Font.BOLD,20));
TopPane.setLayout(new GridLayout(1,1));
TopPane.add(intro);
return TopPane;
}
private JPanel getCenterPane(){
JPanel CenterPane = new JPanel();
CenterPane.add(aantalChars);
CenterPane.setLayout(new GridLayout(6,3));
//8,2
hidden.setVisible(false);
hiddenL.setVisible(false);
CenterPane.add(aantalCharsLabel);
CenterPane.add(hidden);
CenterPane.add(hidden);
CenterPane.add(hiddenL);
CenterPane.add(lowerCase);
CenterPane.add(lowerCaseLabel);
CenterPane.add(upperCase);
CenterPane.add(upperCaseLabel);
CenterPane.add(numberCase);
CenterPane.add(numberCaseLabel);
CenterPane.add(symbolCase);
CenterPane.add(symbolCaseLabel);
return CenterPane;
}
You forgot to set the BorderLayout as LayoutManager on your ContentPane. Just using the right constraints is not enough. You can use ContentPane.setLayoutManager() for this, or you can give the LayoutManager directly in the constructor of JPanel.
The default layout manager for JPanel is FlowLayout manager.
To use BorederLayout you need to set it specifically :
private JPanel ContentPane()
{
JPanel ContentPane = new JPanel();
//////////////////////////////////////////////
ContentPane.setLayout(new BorderLayout());
////////////////////////////////////////////
ContentPane.add(getTopPane(), BorderLayout.NORTH);
ContentPane.add(getCenterPane(),BorderLayout.CENTER);
ContentPane.add(getSouthPane(),BorderLayout.SOUTH);
return ContentPane;
}
I have a JFrame and three JPanels. On the frame I used BorderLayout. At the CENTER of the frame I have put outerPanel. On my outerPanel I have used MigLayout. The two other panels are added on to the outerPanel. These two panels are of equal size and their widths add up to the width of the outerPanel - I wanted the outerPanel to be divided into two halves. Below is the code for this:
public class ControlPanel extends JFrame {
// components
public JPanel outerPanel;
public JPanel innerPanel1;
public JPanel innerPanel2;
public ControlPanel() {
this.createUI();
}
public void createUI() {
// form properties
this.setSize(new java.awt.Dimension(300, 300));
this.setVisible(true);
this.setLayout(new java.awt.BorderLayout());
this.outerPanel = new JPanel();
this.outerPanel.setPreferredSize(new java.awt.Dimension(260, 250));
this.outerPanel.setLayout(new net.miginfocom.swing.MigLayout());
this.outerPanel.setBorder(BorderFactory.createEtchedBorder());
this.add(new javax.swing.JLabel("North"), BorderLayout.NORTH);
this.add(this.outerPanel, BorderLayout.CENTER);
this.innerPanel1 = new JPanel();
this.innerPanel1.setPreferredSize(new java.awt.Dimension(130, 150));
this.innerPanel1.setLayout(new net.miginfocom.swing.MigLayout());
this.innerPanel1.setBorder(BorderFactory.createTitledBorder("Panel1"));
this.innerPanel2 = new JPanel();
this.innerPanel2.setPreferredSize(new java.awt.Dimension(130, 150));
this.innerPanel2.setLayout(new net.miginfocom.swing.MigLayout());
this.innerPanel2.setBorder(BorderFactory.createTitledBorder("Panel2"));
this.outerPanel.add(this.innerPanel1);
this.outerPanel.add(this.innerPanel2);
this.pack();
}
public static void main(String[] args) {
ControlPanel cp = new ControlPanel();
}
}
Problem: When I run my program, the GUI that appears before I resize the window is fine; but when I resize the window -enlarging it, innerPane1 and innerPanel2 remains of the same size without resizing to occupy the space available.
Question: How do we make the two panels , innerPannel1 and innerPanel2, resize at the same time with the window so that they can share equally the available space? Any particular Layout Manager that can be used to divide a panel into two equal halves that can resize at the same time with the window?
Images Showing the output.
Before resizing - the GUI looks well and the panels have correct size.
After resizing -the GUI is distorted and the panels doesn't change size.
I suggest you use new GridLayout(1, 2). This will split the panel in 1 row and 2 (equally sized) columns.
So, simply changing
this.outerPanel = new JPanel();
to
this.outerPanel = new JPanel(new GridLayout(1, 2));
should do.
Trying to change the look of a JOptionPane while its open, depending on which radiobutton the user clicks. What am I doing wrong? It works perfect if I for example add a button and move a JLabel from side to side of the window.
import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.*;
import static javax.swing.JOptionPane.*;
public class ChangePanel extends JFrame{
private JButton click = new JButton("CLICK ME!");
ChangePanel(){
add(click, BorderLayout.SOUTH);
click.addActionListener(new ButtonListen());
setVisible(true);
setSize(300,100);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
public class ButtonListen implements ActionListener{
public void actionPerformed(ActionEvent e){
PopUpPanel pop = new PopUpPanel();
showConfirmDialog(ChangePanel.this, pop, "Changeable", OK_CANCEL_OPTION);
}
}
//Send this as Parameter to the ConfirmDialog
public class PopUpPanel extends JPanel implements ActionListener{
JRadioButton jewelry = new JRadioButton("Jewelry");
JRadioButton shares = new JRadioButton("Shares");
JRadioButton machine = new JRadioButton("Machine");
PopUpPanel(){
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
ButtonGroup bg = new ButtonGroup();
JPanel north = new JPanel();
bg.add(jewelry);
jewelry.addActionListener(this);
bg.add(shares);
shares.addActionListener(this);
bg.add(machine);
machine.addActionListener(this);
north.add(jewelry);
north.add(shares);
north.add(machine);
add(north);
}
//Listener for RadioButtons
public void actionPerformed(ActionEvent e){
JTextField info1Txt = new JTextField(12);
JTextField info2Txt = new JTextField(12);
JTextField info3Txt = new JTextField(3);;
JRadioButton b = (JRadioButton)e.getSource();
if(b.getText().equals("Jewelry")){
//Dummy test text
System.out.println("Jewelry");
JPanel info1 = new JPanel();
info1.add(new JLabel("info1:"));
info1.add(info1Txt);
add(info1);
JPanel info2 = new JPanel();
info2.add(new JLabel("info2:"));
info2.add(info2Txt);
add(info2);
JPanel info3 = new JPanel();
info3.add(new JLabel("info3:"));
info3.add(info3Txt);
add(info3);
validate();
repaint();
}else if(b.getText().equals("Shares")){
//Dummy test text
System.out.println("Shares");
}else
//Dummy test text
System.out.println("Machine");
}
}
public static void main(String[] args){
new ChangePanel();
}
}
As you are working with BoxLayout, you should provide size hints to the PopUpPanel panel, which you haven't given.
When a BoxLayout lays out components from top to bottom, it tries to size each component at the component's preferred height. If the vertical space of the layout does not match the sum of the preferred heights, then BoxLayout tries to resize the components to fill the space. The components either grow or shrink to fill the space, with BoxLayout honoring the minimum and maximum sizes of each of the components.
check out the official tutorial page discussion: BoxLayout Feature
Call revalidate() and repaint() on the container after removing or adding components to it. So if you change the following lines:
validate();
repaint();
to:
revalidate();
repaint();
The content should appear. Though, it will not fit the original size of the JOptionPane. You can override PopUpPanel.getPreferredSize() to return desired size so that JOptionPane is packed properly, ie:
#Override
public Dimension getPreferredSize() {
return new Dimension(300, 300);
}
You can also use JDialog instead of JOptionPane.
Also, consider using CardLayout instead of swapping components manually. Check How to Use CardLayout for examples.
Why not just use setPreferredSize(new Dimension(300, 300)) in PopUpPanel constructor? Works fine for me. Good eye on revalidate and repaint.