What kind of box is this in java? - java

On the bottom of the container where it says Statistic, what kind of box is that. I am using JFrame and would like to duplicate that.
Thanks

The container you're seeing is a JPanel. Followed by adding a border to it. You can add a border with text, by doing.
// panel is a JPanel
panel.setBorder(BorderFactory.createTitledBorder("Title"));
You can read more about it on the Java Docs there is even a lot of examples, showing the various borders, etc.
There is even a "BorderDemo.java" you can get from the Java Docs.

It's a JPanel. It provides means for visually structuring elements into box-like containers. You can find more information on how to effectively use and style them here.
EDIT: Since you seem to be more interested in the style of the border than the name of the component being used in the example above here's a snippet using the border style on a panel.
import javax.swing.*;
public class MyPanelTestDrive{
public static void main(String [] args){
JFrame myFrame = new JFrame("Panel Test Drive");
myFrame.setSize(800,600);
JPanel myPanel = new JPanel();
myPanel.setBorder(BorderFactory.createTitledBorder("Panel Title"));
myFrame.add(myPanel);
myFrame.setVisible(true);
}
}

Related

Size of JPanel in JFrame

I am trying to add a JPanel into my JFrame with a specific size. But whatever size I add to the JPanel, it always fills the whole entire JFrame. And I also tried to reposition the JButton according to my set position but it also doesn't work. Any recommendations or explanations anyone? Tq :P
import javax.swing.*;
import java.awt.event.*;
import java.awt.;
import java.awt.image.;
public class Login {
//Creating a method just for the login page0
static void Login(){
JFrame LoginFrame = new JFrame();
JPanel Panel = new JPanel();
Panel.setBounds(40,80,100,50);
Panel.setBackground(Color.black);
JButton Enter = new JButton();
Enter.setBackground(Color.cyan);
Enter.setBounds(50,100,80,30);
Enter.setText("Enter");
JButton Enter2 = new JButton();
Enter.setBackground(Color.cyan);
Enter.setBounds(50,100,80,30);
Enter.setText("Enter");
LoginFrame.setSize(420,720);
LoginFrame.setBackground(Color.white);
LoginFrame.setTitle("LoginFrame");
LoginFrame.setVisible(true);
LoginFrame.setLayout(null);
}
public static void main(String[]args) {
Login();
}
}
First of all, variable names should NOT start with an upper case character. These are Java conventions you will find in any text book or tutorial. Follow the examples.
I am trying to add a JPanel into my JFrame with a specific size
Why use a random size? Swing is designed so that each component will determine its own size so it will work on any platform with any Look and Feel.
Any recommendations
Don't use a null layout. Don't use setBounds(). Swing was designed to be used with layout managers. Layout managers make coding easier and more maintainable. Read the Swing tutorial on Layout Managers
Also, you actually need to "add" the buttons to the panel and "add" the panel to the frame. The above tutorial demos show how to do that.
If you are simply trying to change the size of the buttons then use:
button.setMargin( new Insets(20, 20, 20, 20) );
The button will be sized properly based on the text AND the extra space specified.
If you want your panel to have extra space then you can use:
panel.setBorder( new EmptyBorder(20, 20, 20, 20) );
Read the Swing tutorial on How to Use Borders for more information and examples.
or explanations
The JPanel uses a FlowLayout by default. So the layout manager is resetting the size/location based on the rules of the layout manager. If you want a different layout, then use a different layout manager.

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?)

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.

Why is nothing showing up in my Java GUI?

I am currently working on a calculator. Right now, I am trying to gain experience with coding Java GUI, by making a simple program that makes a window with a text field. The code can compile without errors, but when I execute the program, the window appears, but without the text field. How do I make the text field visible? The code is shown as follows:
import javax.swing.*;
import java.awt.*;
public class Window {
public static void main(String[] args) {
JFrame Window = new JFrame("Window");
Window.setSize(400,550);
Window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel Panel = new JPanel (new FlowLayout());
JTextField TextField = new JTextField("Type something here");
Window.setVisible(true);
}
}
You haven't added any of the components to your JFrame. You can add them like so:
Panel.add(TextField);
Window.add(Panel);
Window.setVisible(true);
Side note: You should stick to the Java naming conventions. Use camel case for variable names.
You need to add them before making the frame visible.
Panel.add(TextField);
Window.add(Panel);
You need to take the components that you have instantiated (e.g. the Panel and TextField) and add them to appropriate containers.
For example:
Panel.add(TextField);
Window.add(Panel);
If you have not done so already, I would highly recommend visiting http://docs.oracle.com/javase/tutorial/uiswing/index.html for more information on how to use the Swing library.

Java: JScrollPane didn't show scroll bars

I am developing a small desktop application in Java using Netbeans. On my jframe i have various pannels and one scroll panes. The purpose of this JScrollPane is to show some visual elements to its users. I achieve this by following the below steps in sequence:
Drag and drop JScrollPane at desired location of my JFrame
Adjust the size of JScrollPane according to my needs.
Write a new java class and extend that class with JPanel
Override the public void paintComponent(Graphics g) method
Then i add that panel to above JScrollPane,
using following code:
JPanel jpnl = new myClass();
jScrollPane2.setViewportView(jpnl);
jScrollPane2.repaint();
Now every thing is working fine as per my requirements, the only thing which is lacking is that when my drwaing is big then no sroll bars are shown at JScrollPane. This is my first application and i don't know much about Java, so any guidence regarding what is missing would be highly appreciated
Remember to add the required component to the JScrollPane object, and the scroll pane object to the panel. Also, it could be that you need to change the scroll bar policy: use scroll pane's setHorizontalScrollBarPolicy() and setVerticalScrollBarPolicy().
Consult the JScrollPane documentation for these methods.

Categories