Java swing setSize - java

I have a problem, I set the size for frame, but doesn't work. I found a topic about this, but it's not helpful for me...
I tried with getContentPane().getSize(500,500), but nothing.
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class Mere extends JFrame{
private JPanel b;
public Mere(String titlu){
setSize(500,500);
setLayout(null);
this.getSmth();
pack();
show();
}
public void getSmth(){
JLabel user=new JLabel("User");
user.setBounds(10,10,80,25);
getContentPane().add(user);
JTextField userText=new JTextField(20);
userText.setBounds(100,10,160,25);
getContentPane().add(userText);
JPasswordField pas=new JPasswordField(20);
pas.setBounds(100,40,160,25);
getContentPane().add(pas);
JButton n=new JButton("Register");
n.setBounds(180,80,100,30);
getContentPane().add(n);
JLabel pass=new JLabel("Pass");
pass.setBounds(10,40,100,25);
getContentPane().add(pass);
getContentPane().setVisible(true);
}
public static void main(String[]args){
new Mere("yas");
}
}

You're calling pack(); after calling setSize().
From the Oracle docs
Causes this Window to be sized to fit the preferred size and layouts of its subcomponents. The resulting width and height of the window are automatically enlarged if either of dimensions is less than the minimum size as specified by the previous call to the setMinimumSize method.
So you rather override the getPreferredSize() method or remove the pack() call. See Should I avoid the use of setPreferred/Maximum/MinimumSize? (YES)
However the best practice would be to use a layout manager (as explanined below) and then call pack() and let the manager do it's job while calculating the preferredSize for you
Also you're using a null layout. Swing was designed to work with different PLAFs and screen sizes and resolutions, while pixel perfect GUIs might seem like the best and faster approach to make a complex GUI the more you advance in this the more problems you'll have in maintain it due to this, so go ahead and use a proper Layout Manager or combinations of them. See Null Layout is Evil and Why is it frowned upon to use a null layout in Java Swing?
Another thing is that you're not placing your program on the Event Dispatch Thread (EDT), see SwingUtilities.invokeLater() why is it needed? and this answer for an example on how to use it
Another thing I see is that you're extening JFrame which translated to english it says that your class is a JFrame, JFrame isn't flexible when you need to add it to another Component, instead you should create a JFrame instance and if you really need to extend something, extend from a JPanel.

Related

Elements don't appear in a Panel with a GridLayout or FlowLayout, but with a setBounds they do

I'm doing a program that is composed by multiple panels in a JFrame.
I need to do every elements in differents classes (It's because in my school, we need to have every elements separeated in different classes for clean code), but every example that I see with my kind of problem, they do everything in one class.
And I think that my problem comes from having multile classes so I show you my classes.
I have a panel in wich I need to put 2 panel, here is the code :
public class Inscription extends JPanel{
private PanneauBoutons panneauBoutons = new PanneauBoutons();
private PanneauFormulaire panneauFormulaire = new PanneauFormulaire();
public Inscription(){
this.setLayout(new BorderLayout());
this.setBorder(BorderFactory.createLineBorder(Color.RED, 2));
this.add(panneauFormulaire,BorderLayout.CENTER);
this.add(panneauBoutons,BorderLayout.SOUTH);
this.setVisible(true);
}
}
And here is the Panel panneauFormulaire :
public class PanneauFormulaire extends JPanel{
private JLabel labelMatricule;
private JTextField zoneTexteMatricule;
public PanneauFormulaire(){
this.setLayout(new GridLayout(8,2,10,10));
this.setBorder(BorderFactory.createLineBorder(Color.black));
labelMatricule = new JLabel("Matricule : ");
this.add(labelMatricule);
zoneTexteMatricule = new JTextField(30);
this.add(zoneTexteMatricule);
this.setVisible(true);
}
So the problem Inscription don't appear on the main Frame if I don't do setBounds, but I want a BorderLayout...
(I tested and with a set bounds I can see the borders, so I think that it means the panel are really added to the Frame so why without setBounds I see anything?).
And the other problem is that the panel PanneauFormulaire don't appear on the Inscription panel...
So if I miss something, can you help me? thank you
And here it is the class that extends JFrame :
public class FenetrePrincipale extends JFrame {
private Container cont;
private Inscription inscriptionForm;
public FenetrePrincipale(){
super("IESN");
setBounds(100,100,1200,960);
getContentPane().setLayout(null);
setLocationRelativeTo(null);
setResizable(false);
...
inscription.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e){
cont.removeAll();
inscriptionForm = new Inscription();
inscriptionForm.setOpaque(true);
cont.add(inscriptionForm);
invalidate();
repaint();
}
});
You should NOT be using a null layout and setBounds(). Swing was designed to be used with layout managers.
but when I click on an option in the menu, the current panel need to be change by another one,
Then you should be using a CardLayout.
Read the section from the Swing tutorial on How to Use CardLayout for working examples. So download the example and use it as the starting point of your project. The code will be better structured then what you currently have and it is easier to change working code than it is to fix broken code.
so why without setBounds I see anything?
That is because you set your layout to null in getContentPane().setLayout(null);.
Java containers comes with a default layout which you are allowed to set to a different one. How the components are arranged in the container are dependent on the layout you use. The layout will directly affects the location, alignment, spacing, dimension, preferredSize of the components.
However, if you choose not to use any layout (.setLayout(null)). Swing will not know how you want the components to be arranged, hence you see nothing in your content pane.
Since you wanted "absolute control" over the components, you will be expected to set the bounds (location and dimension) of each added component manually by yourself. This is why you are not seeing any components (even if you already added it) until you set the bounds for them.
Java, elements don't appear in a Panel with a GridLayout or FlowLayout, but with a setBounds they do
Every layout has their own characteristics and for some of them the order of your codes does makes a difference. Hence, I will advise you to go through what each layout can do for you. Then, depending on your needs, choose one (or a combination of a few) and study how to use it.
And here it is the class that extends JFrame :
You probably won't want to extends to a JFrame. You can always make a customized Container like JPanel and add it to the frame.
(Why would you want to paint your paintings on a frame instead of a piece of paper?)

Is there a way I can swap JPanel classes into and out of a JFrame? [duplicate]

To put it simple, there's a simple java swing app that consists of JFrame with some components in it. One of the components is a JPanel that is meant to be replaced by another JPanel on user action.
So, what's the correct way of doing such a thing? I've tried
panel = new CustomJPanelWithComponentsOnIt();
parentFrameJPanelBelongsTo.pack();
but this won't work. What would you suggest?
Your use case, seems perfect for CardLayout.
In card layout you can add multiple panels in the same place, but then show or hide, one panel at a time.
1) Setting the first Panel:
JFrame frame=new JFrame();
frame.getContentPane().add(new JPanel());
2)Replacing the panel:
frame.getContentPane().removeAll();
frame.getContentPane().add(new JPanel());
Also notice that you must do this in the Event's Thread, to ensure this use the SwingUtilities.invokeLater or the SwingWorker
frame.setContentPane(newContents());
frame.revalidate(); // frame.pack() if you want to resize.
Remember, Java use 'copy reference by value' argument passing. So changing a variable wont change copies of the reference passed to other methods.
Also note JFrame is very confusing in the name of usability. Adding a component or setting a layout (usually) performs the operation on the content pane. Oddly enough, getting the layout really does give you the frame's layout manager.
Hope this piece of code give you an idea of changing jPanels inside a JFrame.
public class PanelTest extends JFrame {
Container contentPane;
public PanelTest() {
super("Changing JPanel inside a JFrame");
contentPane=getContentPane();
}
public void createChangePanel() {
contentPane.removeAll();
JPanel newPanel=new JPanel();
contentPane.add(newPanel);
System.out.println("new panel created");//for debugging purposes
validate();
setVisible(true);
}
}
On the user action:
// you have to do something along the lines of
myJFrame.getContentPane().removeAll()
myJFrame.getContentPane().invalidate()
myJFrame.getContentPane().add(newContentPanel)
myJFrame.getContentPane().revalidate()
Then you can resize your wndow as needed.
Game game = new Game();
getContentPane().removeAll();
setContentPane(game);
getContentPane().revalidate(); //IMPORTANT
getContentPane().repaint(); //IMPORTANT
It all depends on how its going to be used. If you will want to switch back and forth between these two panels then use a CardLayout. If you are only switching from the first to the second once and (and not going back) then I would use telcontars suggestion and just replace it. Though if the JPanel isn't the only thing in your frame I would use
remove(java.awt.Component) instead of removeAll.
If you are somewhere in between these two cases its basically a time-space tradeoff. The CardLayout will save you time but take up more memory by having to keep this whole other panel in memory at all times. But if you just replace the panel when needed and construct it on demand, you don't have to keep that meory around but it takes more time to switch.
Also you can try a JTabbedPane to use tabs instead (its even easier than CardLayout because it handles the showing/hiding automitically)
The other individuals answered the question. I want to suggest you use a JTabbedPane instead of replacing content. As a general rule, it is bad to have visual elements of your application disappear or be replaced by other content. Certainly there are exceptions to every rule, and only you and your user community can decide the best approach.
Problem: My component does not appear after I have added it to the container.
You need to invoke revalidate and repaint after adding a component before it will show up in your container.
Source: http://docs.oracle.com/javase/tutorial/uiswing/layout/problems.html
I was having exactly the same problem!! Increadible!! The solution I found was:
Adding all the components (JPanels) to the container;
Using the setVisible(false) method to all of them;
On user action, setting setVisible(true) to the panel I wanted to
show.
// Hiding all components (JPanels) added to a container (ex: another JPanel)
for (Component component : this.container.getComponents()) {
component.setVisible(false);
}
// Showing only the selected JPanel, the one user wants to see
panel.setVisible(true);
No revalidate(), no validate(), no CardLayout needed.
The layout.replace() answer only exists/works on the GroupLayout Manager.
Other LayoutManagers (CardLayout, BoxLayout etc) do NOT support this feature, but require you to first RemoveLayoutComponent( and then AddLayoutComponent( back again. :-) [Just setting the record straight]
I suggest you to add both panel at frame creation, then change the visible panel by calling setVisible(true/false) on both.
When calling setVisible, the parent will be notified and asked to repaint itself.
class Frame1 extends javax.swing.JFrame {
remove(previouspanel); //or getContentPane().removeAll();
add(newpanel); //or setContentPane(newpanel);
invalidate(); validate(); // or ((JComponent) getContentPane()).revalidate();
repaint(); //DO NOT FORGET REPAINT
}
Sometimes you can do the work without using the revalidation and sometimes without using the repaint.My advise use both.
Just call the method pack() after setting the ContentPane, (java 1.7, maybe older) like this:
JFrame frame = new JFrame();
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
....
frame.setContentPane(panel1);
frame.pack();
...
frame.setContentPane(panel2);
frame.pack();
...

Display a grid of JPanels in a JFrame?

I'm very new to Java but have some experience with C++. This is a homework assignment so I'm really just looking for someone to point me in the right direction.
The assignment requires a JFrame with JPanel objects displaying every card in a deck in a 13x4 grid. The Professor has supplied us with some code to get us started:
import javax.swing.*;
import java.awt.*;
public class Main
public static void main(String[] args)
{
//load the card image from the gif file.
final ImageIcon cardIcon = new ImageIcon("cardImages/tenClubs.gif");
//create a panel displaying the card image
JPanel panel = new JPanel()
{
//paintComponent is called automatically by the JRE whenever
//the panel needs to be drawn or redrawn
public void paintComponent(Graphics g) {
super.paintComponent(g);
cardIcon.paintIcon(this, g, 20, 20);
}
};
//create & make visible a JFrame to contain the panel
JFrame window = new JFrame("Title goes here");
window.add(panel);
window.setPreferredSize(new Dimension(200,200));
window.pack();
window.setVisible(true);
}
}
I have tried out a few things, but I can't seem to get multiple panels to display. Should I use a gridLayout() feature? or just create multiple panels and specify each one's location in the frame?
Again if someone can just point me in the right direction that would be awesome.
For displaying elements at the same size, evenly distributed within the container, then yes, GridLayout would be a good choice.
If you need to display the components in the grid at there preferred size (which may be different for each component) then GridBagLayout would be a better choice
If the code was supplied by a your professor, then you need to go back and make them fix it.
Firstly, a JLabel would be easier and provide better support for what you are trying to achieve...
Secondly, because the JPanel doesn't override getPreferredSize, most of the layout managers will set the size of the component to 0x0
There is a way to display multiple JPanels in one JFrame. Unlucky you the way is not so easy. Java has many diffrent LayoutManagers.
For your purpose I would recommend GridBagLayout, it is more complex, but definately the thing you need.
Here is a good tutorial, which helped me to understand it:
GridBagLayout
Hope it is a help.

Invisible JButton in JPanel

Here's my JPanel. The first button is always visible, but the secound is visible only when you place a cursour on it. Where the problem can be?
P.S. Please use simple english, if u can, because I don't speak english well
public class GamePanel extends JPanel implements KeyListener{
GamePanel(){
setLayout(null);
}
public void paint(Graphics g){
JButton buttonShip1 = new JButton();
buttonShip1.setLocation(10, 45);
buttonShip1.setSize(40, 40);
buttonShip1.setVisible(true);
add(buttonShip1);
JButton buttonShip2 = new JButton();
buttonShip2.setLocation(110, 145);
buttonShip2.setSize(440, 440);
buttonShip2.setVisible(true);
add(buttonShip2);
}
}
If you want to avoid problems and learn Java Swing correctly, go check out their tutorials here.
There are too many problems to discuss here, so I'll try to keep it simple.
You're using a null layout. null layouts are avoided for the most part because there is usually a layout that does exactly what you want. It takes some time to get it working, but there are some defaults in this tutorial that are fairly simple to use. There are some nice pictures there that show you what you can do with each layout, too. If you use a layout manager, you generally don't need to use setLocation, setSize or setVisible on most components like JButtons.
You're calling the paint method in a Swing application. You want to call paintComponent because you're using Swing and not Awt. You also need to call the super.paintComponent(g) method on the first line of the paintComponent method in order to correctly override the other paintComponent method.
The paint/paintComponent related methods are called very often. You don't want to create/initialize objects in them. The paint/paintComponent methods aren't a one time method like they may sound. They're continuously called and you should design your GUI around this. Design your paint-related methods to be event-driven rather than sequential. In other words, program the paintComponent method with the mindset that your GUI is reacting to things continuously rather than running in sequential order like a normal program. That's a very basic approach to it and hopefully doesn't confuse you, but if you go check out that tutorial you'll see what I mean eventually.
There are two basic types of GUIs in Java. One is Swing and the
other is Awt. Check out this answer on stackoverflow for a
nice description of the two.
Here is an example of what two buttons on a JPanel look like.
public class Test
{
public static void main(String[] args)
{
JFrame jframe = new JFrame();
GamePanel gp = new GamePanel();
jframe.setContentPane(gp);
jframe.setVisible(true);
jframe.setSize(500,500);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static class GamePanel extends JPanel{
GamePanel() {
JButton buttonShip1 = new JButton("Button number 1");
JButton buttonShip2 = new JButton("Button number 2");
add(buttonShip1);
add(buttonShip2);
//setLayout(null);
//if you don't use a layout manager and don't set it to null
//it will automatically set it to a FlowLayout.
}
public void paintComponent(Graphics g){
super.paintComponent(g);
// ... add stuff here later
// when you've read more about Swing and
// painting in Swing applications
}
}
}
Don't use a null layout
Don't create components in a painting method. The paint() method is for custom painting only. There is no need for you to override the paint() method.
Read the section from the Swing tutorial How to Use FlowLayout for a simple example that uses buttons and a layout manager.

When to use JFrame, JLabel, and JPanel

I have been, for a long time, wondering when to use JFrame and JPanel. I was told by textbook, in-person, and via internet search some reasons, but of course, it took me looking at the Java documentation to figure out that a JPanel has the paintComponent(Graphics); method, which allows you to specify what to do to the object itself, unlike JFrame. Also, JPanels are nestable, allowing for more complex graphics than you could achieve with just one container. I also stumbled upon the fact that a JLabel is also a container. (It even has a freaking layout!) Now my question is: when should I be using a JLabel and when should I be using a JPanel?
//I know that you can put JLabels inside a JPanel, and by accident, that a JPanel is more expensive than a JLabel. I, in the long run, plan on making a cashier game that involves some lightweight container for the "money" that moves to the customer's hand upon clicking. I was thinking about using JLabels for the monetary amounts (dollar bills and coins), and the JPanel to serve as the overhead view of the transaction(s). I was also thinking about invoking repaint() (which, to my understanding, can simulate animation) on the "money labels" themselves. This is a double-question, but would you see this as the least expensive way to go about it?
For the most part, JPanel is a container that allows for better organization. When you need to organize your components of your gui, you should use a JPanel and define a layout for the JPanel. You can also nest JPanels within each other and etc. JLabels don't have the capability of defining layouts for further components to be displayed inside the JLabel.
Here's an example of a app that says "Hello World!!!" without the quotes:
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
public class JFrame {
public static void main (strings args[]) {
JFrame frame = new JFrame();
String title = "Hello App";
frame.setTitle(title);
frame.setSize(300, 200);
frame.setDefaultCloseOperation
(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
JPanel panle = new JPanel();
frame.add(panle);
JLabel lable = new JLabel("Hello World!!!");
panle.add(lable);
}
}
The file name is "JFrame.java" without the quotes.

Categories