How to see the components inside a layered pane - java

I placed two components inside a JLayeredPane but I can't make them visible. Here is a fairly MCV code. How do I see my JTextField and JLabel inside the layeredPane?
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EtchedBorder;
public class GUI extends JFrame {
JFrame mainframe = new JFrame();
JPanel centrejPanel = new JPanel();
JTextField keyText;
JLabel jLabel;
public GUI() {
mainframe.setLayout(new BorderLayout());
mainframe.setSize(1200, 700);
mainframe.getContentPane().add(centrejPanel, BorderLayout.CENTER);
keyText = new JTextField("hello");
keyText.setOpaque(false);
keyText.setCaretColor(Color.BLACK);
keyText.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
jLabel = new JLabel("hello");
jLabel.setFont(new Font("Palatino", Font.BOLD, 18));
jLabel.setVerticalAlignment(JLabel.TOP);
jLabel.setForeground(Color.GRAY);
JLayeredPane layeredPane = new JLayeredPane();
layeredPane.add(keyText, 1);
layeredPane.add(jLabel, 0);
centrejPanel.getRootPane().add(layeredPane);
mainframe.setVisible(true);
}
}
public class Main {
public static void main(String[] args) {
GUI gui = new GUI();
}
}

//mainframe.setLayout(new BorderLayout());
Not needed. The default layout manager of the content pane of the frame is a BorderLayout.
//mainframe.getContentPane().add(centrejPanel, BorderLayout.CENTER);
Don't add an empty panel to the content pane of the frame. Just add the LayeredPane directly to the content pane.
keyText.setBounds(0, 50, 100, 20);
...
jLabel.setBounds(0, 150, 100, 20);
A JLayeredPane uses a null layout so it is your responsibility to set the size and location of each component added to the layered pane.
//centrejPanel.getRootPane().add(layeredPane);
Don't add the layered pane to the root pane. Don't even know if this will work but in any case the content pane will just cover the layered pane.
Read the section from the Swing tutorial on Using Top Level Containers to see how all the frame layers are structured.
mainframe.add(layeredPane);
Just add the layered pane directly to the content pane of the frame. Read the Swing tutorial on How to Use LayeredPane for more information and working examples.
Always start with examples from the tutorial when learning a new concept or component.

Related

How to add a GUI panel and a DrawPanel within one frame?

I want two Panels to be added here within one frame, I want some buttons and text fields on the right side there to edit the drawing panel class but I can't figure out how to add GUI components to panel2. Whenever I try to add GUI components to panel2, it just doesn't work how it would work normally and I don't understand why.
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setTitle("WaferMap");
frame.setSize(1286, 829);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setVisible(true);
Container contentPane = frame.getContentPane();
contentPane.setLayout(new BorderLayout());
DrawingPanel panel1 = new DrawingPanel();
JPanel panel2 = new JPanel();
frame.add(panel1, BorderLayout.WEST);
panel1.setPreferredSize(new Dimension(1080, 800));
final JButton button = new JButton();
panel2.add(button, BorderLayout.EAST);
button.setBounds(50,50,100,50);
frame.add(panel2, BorderLayout.EAST);
panel2.setPreferredSize(new Dimension(200, 800));
}
}
This is the main, I have another class called drawingPanel with a paintComponent in it.
Don't use setPreferredSize(). It is the job of the layout manager to calculate the preferred size of the panel based on the rules of the layout manager and the preferred size of the components added to the panel.
Don't use setSize(). Instead use pack(). The frame will be sized based on the preferred size of all components added to the frame.
The real problem is that by default a component has a size of (0. 0) so there is nothing to paint. It is the job of the layout manager to give the component a size/location to the component. The problem is that you made the frame visible before you added the components to the panel, so the layout manager was never invoked. So the solution is that the pack() and setVisible(true) statements should be invoked AFTER all components have been added to the frame.
Note, the tutorial also has a section on Custom Painting that you should be reading so you implement the painting properly to make sure your DrawingPanel has a preferred size.

How can I display 2 different panels at the same time?

My first panel's layout is BorderLayout and my second panel's layout is GridBagLayout. I don't know how to show them both at the same time.
I already tried adding two panels to on another panel.
Adding both to another panel is the way to go! But you have to make the right choice of LayoutManager for this "parent" panel. Let me give you an example:
The JFrame's content pane (where you add all your Components to) can be setup with a LayoutManager of your choice. See this runnable example, which creates two JPanels of 100x100 pixels in different colors. The panels are using the LayoutManagers you mentioned, but the main content pane of the JFrame is set to a BoxLayout (horizontal, but you can also set it to vertical!).
You can do this to any other panel, too. A panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS)); is enough. The below example just uses the content pane, but you can adapt it to your needs:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TwoPanels extends JFrame {
private static final long serialVersionUID = 1L;
private static final Dimension DEFAULT_DIMENSION = new Dimension(100, 100);
public static void main(String[] args) {
new TwoPanels();
}
public TwoPanels() {
//create panel 1
JPanel panel1 = new JPanel(new BorderLayout());
panel1.setPreferredSize(DEFAULT_DIMENSION);
panel1.setBackground(Color.RED);
//create panel 2
JPanel panel2 = new JPanel(new GridBagLayout());
panel2.setPreferredSize(DEFAULT_DIMENSION);
panel2.setBackground(Color.GREEN);
//set content pane layout
setLayout(new BoxLayout(this.getContentPane(), BoxLayout.X_AXIS));
//add to content pane
add(panel1);
add(panel2);
//setup and display window
pack();
setVisible(true);
}
}
It looks like this:
EDIT: It's a little unclear from your question that you actually want to stack overlaying panels. You might find what you need here: https://docs.oracle.com/javase/tutorial/uiswing/components/layeredpane.html

How do I resize a button?

How do you resize the button? I've tried various methods like setSize and setPreferredSize but they aren't workng.
package tests;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JButton;
public class RPS extends JPanel{
public static void main(String[] args) {
JFrame frame = new JFrame("Rock Paper Scissors");
JPanel panel = new JPanel();
JLabel label = new JLabel("<html>Will you choose <i>rock,</i> <i>paper,</i> or <i>scissors?</i></html>");
JButton button = new JButton("I am a button.");
label.setHorizontalAlignment(0);
label.setVerticalAlignment(1);
frame.pack();
frame.getContentPane();
frame.setTitle("Rock Paper Scissors");
frame.setSize(640, 480);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
frame.add(label);
frame.add(button);
button.setToolTipText("Y U no click me?");;
}
}
There is generally no need to resize a button. The button will determine its own size based on the text and Font used by the button. The layout manager will then use this information to give the components a size and location based on the rules of the layout manager.
If you want extra space around the text then you can use:
button.setMargin(...);
You have many other problems with your code:
frame.add(panel);
frame.add(label);
frame.add(button);
This won't do anything. By default the content pane of the frame uses a BorderLayout. If you don't specify a constraint, then then component is add to the CENTER, but only one component can be displayed in the CENTER so you only see the last one.
frame.setSize(640, 480);
There is no need for that statement. The pack() will set all the components at their preferred sizes.
frame.setVisible(true);
This should be the last statement executed, "after" all components have been added to the frame.
frame.getContentPane();
This does nothing, you don't assign the returned value to any variable.
label.setHorizontalAlignment(0);
label.setVerticalAlignment(1);
Don't use magic numbers. Nobody knows what 0 and 1 are used for. Read the API for those methods for variables that you can use.
Read the section from the Swing tutorial on Layout Managers for more information and working examples to get you started. Start with the section on How to Use BorderLayout to understand why your current code isn't working and to understand how to specify the "constraints" when you add your components to the frame.
package tests;
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class RPS extends JPanel{
public static void main(String[] args) {
JFrame frame = new JFrame("Rock Paper Scissors");
JPanel panel = new JPanel();
JLabel label = new JLabel("<html>Will you choose <i>rock,</i> <i>paper,</i> or <i>scissors?</i></html>");
JButton button = new JButton("I am a button.");
label.setHorizontalAlignment(0);
label.setVerticalAlignment(1);
panel.add(button); <-add button to panel
frame.add(panel, BorderLayout.SOUTH); <--- you need to say where you are adding the panel onto the frame.
frame.add(label,BorderLayout.NORTH); <--- same with the label
frame.pack();
frame.setTitle("Rock Paper Scissors");
frame.setSize(640, 480);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
button.setToolTipText("Y U no click me?");;
}
}
If you want a bigger button in the center, you can modify with this:
button.setPreferredSize(new Dimension(300, 300));
panel.add(button);
frame.add(panel, BorderLayout.CENTER); <--adds to center rather than south

JLabel alignment in JPanel

I'm trying to align a JLabel to the right in a JPanel. I'm adding a JTabbedPane, a JPanel which contains my JLabel and JTextArea to a main JPanel.
I have searched SO and tried some methods like setAlignmentX, setHorizontalAlignment(SwingConstants.LEFT) and nested containers to no avail.
Here's my code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class LabelProblem
{
public static void main(String[] args)
{
JFrame frame = new JFrame("Label Problem");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel Main = new JPanel();
Main.setLayout(new BoxLayout(Main, BoxLayout.Y_AXIS));
JPanel ComponentPanel = new JPanel();
JLabel label = new JLabel("Sample Text");
label.setHorizontalAlignment(SwingConstants.LEFT);
label.setBorder(BorderFactory.createLineBorder(Color.BLACK));
label.setAlignmentX(Component.RIGHT_ALIGNMENT);
ComponentPanel.add(label);
JTabbedPane Tab = new JTabbedPane();
Tab.add("Document 1", new JPanel());
Main.add(Tab);
Main.add(ComponentPanel);
JTextArea Area = new JTextArea(10,10);
JScrollPane Scroll = new JScrollPane(Area);
frame.add(Main);
frame.add(Scroll, BorderLayout.SOUTH);
frame.setSize(450,450);
frame.setVisible(true);
}
}
How can I align my JLabel to the right?
Thanks!
So, the place of that label is determined by the layout of ComponentPanel. Since you didn't specify any layout it is using the default FlowLayout with a CENTER alignment. Assuming that you are ok with a FlowLayout it is a mere question of setting the alignment of the LEFT since this is possible with this layout.
Here's the code with the fix, however I suspect that as you put more elements to the ComponentPanel you will want to use another layout since FlowLayout is more adequate for menus and the like and not for displaying the main content.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
class LabelProblem
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
initGUI();
}
});
}
public static void initGUI()
{
JFrame frame = new JFrame("Label Problem");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel main = new JPanel();
main.setLayout(new BoxLayout(main, BoxLayout.Y_AXIS));
JPanel componentPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
JLabel label = new JLabel("Sample Text");
label.setBorder(BorderFactory.createLineBorder(Color.BLACK));
componentPanel.add(label);
JTabbedPane Tab = new JTabbedPane();
Tab.add("Document 1", new JPanel());
main.add(Tab);
main.add(componentPanel);
JTextArea area = new JTextArea(10, 10);
JScrollPane scroll = new JScrollPane(area);
frame.add(main);
frame.add(scroll, BorderLayout.SOUTH);
frame.setSize(450, 450);
frame.setVisible(true);
}
}
Result:
Note: I also changed the variable names to follow the java style convention: variable names should start with lower case to differenciate them from clases names, starting in upper case.
One simple approach is to set the label's horizontalAlignment to JLabel.RIGHT in the constructor.
import java.awt.*;
import javax.swing.*;
class LabelProblem {
public static void main(String[] args) {
JFrame frame = new JFrame("Label Problem");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridLayout(0, 1));
JTabbedPane tab = new JTabbedPane();
tab.add("Document 1", new JPanel());
frame.add(tab);
JLabel label = new JLabel("Sample Text", JLabel.RIGHT);
frame.add(label);
JTextArea area = new JTextArea(10, 10);
JScrollPane scroll = new JScrollPane(area);
frame.add(scroll);
frame.pack();
frame.setSize(450, 450);
frame.setVisible(true);
}
}
I think it may be a matter of you not actually setting layouts where you imagine you're setting layouts.
You have a JPanel with a vertically oriented BoxLayout (Main) enclosing another JPanel with default layout (ComponentPanel), finally enclosing your label. The reason why your label can't be pushed to the right is because is already is pushed to the right within it's enclosing container. If you set a colored border around ComponentPanel, you'll see what I mean -- it only occupies the same amount of space as the JLabel, giving the JLabel nowhere to move.
You need to set a layout and constraints for your intermediate ComponentPanel, allowing it to horizontally fill its parent container so that the label has someplace to go.
You haven't really specified how your layout is supposed to look, but if you change the layout on Main to X_AXIS, your label will pop over to the left (as will its parent container). Without knowing what you're really trying to do, I can't say much more.
I would however, suggest you throw your BoxLayout away entirely and look into using GridBagLayout, which gives you a high level control over your UI. GridBagLayout isn't the most concise construct, but that's the price of control.

JTabbedPane image alignment

I made a quick research to solve this problem but until now I found nothing regarding to this. I have one image into one TabbedPane object but when I try to align this image on the center of the label inside the TabbedPane it "Doesn't" work. The center alignment in this case works only for horizontal view but I want to be in the center of both vertical and horizontal. Check out the sample below:
import java.awt.BorderLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import com.sh.st.gui.MainScreen;
public class test {
JTabbedPane tabbedPane = new JTabbedPane();
JFrame mainFrame = new JFrame();
public static void main (String[] args){
test t = new test();
}
public test(){
JPanel entrance = new JPanel();
JLabel lbImage1;
JMenuBar bar;
JMenu file, registerQuery;
ImageIcon Logo= new ImageIcon("rsc/img/imagem.jpg");
lbImage1= new JLabel(Logo, JLabel.CENTER);
entrance.add(lbImage1);
tabbedPane.addTab("Entrance", null, entrance);
mainFrame.getContentPane().add( tabbedPane, BorderLayout.CENTER);
bar= new JMenuBar();
file= new JMenu("File");
registerQuery= new JMenu("Request");
mainFrame.setVisible(true);
}
}
I guess its not so hard to do what I want but until now as I said, I found nothing, anyone could help please? thanks in advance
The JLabel alignment will only center horizontally due to the potitioning characteristics of its parent container. In this case it is the default layout for JPanel which is FlowLayout. This layout manager does not facilitate easy vertical alignment.
Provided that the JLabel lbImage1 will be the only component on the JPanel entrance, then GridLayout can be used to center the JLabel both horizontally and vertically:
entrance.setLayout(new GridLayout());

Categories

Resources