How to set position to JComponent in BoxLayout? - java

I want to use 2 JPanel as panel and panel_1.
I want to add image automatically to panel using JLabel
and also add JButton to panel_1.
How can I resize button according to the image which is above the button?
public class Testing extends JFrame {
public Testing() {
this.setSize(590, 327);
this.setTitle("JFrame");
getContentPane().setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(118, 136, 321, 89);
getContentPane().add(panel);
panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
JLabel lblImage = new JLabel("image for button1");
panel.add(lblImage);
JLabel lblImage_1 = new JLabel("image for button2");
panel.add(lblImage_1);
JLabel lblImage_2 = new JLabel("image for button3");
panel.add(lblImage_2);
JPanel panel_1 = new JPanel();
panel_1.setBounds(118, 30, 321, 77);
getContentPane().add(panel_1);
panel_1.setLayout(new BoxLayout(panel_1, BoxLayout.X_AXIS));
JButton btnNewButton = new JButton("New button 1");
panel_1.add(btnNewButton);
JButton btnNewButton_1 = new JButton("New button 2");
panel_1.add(btnNewButton_1);
JButton btnNewButton_2 = new JButton("New button 3");
panel_1.add(btnNewButton_2);
}
public static void main(String[] args) throws Exception {
Testing frame = new Testing();
frame.setVisible(true);
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
}

If your goal is to have the button above the image, and have the button's width expand with the image, then:
Get rid of your use of null layouts and .setBounds(...) (this is just good general advice)
Put the JLabel with the image into a JPanel that uses BorderLayout with the JLabel in the BorderLayout.CENTER position
Put the button above the JLabel in the same JPanel using the BorderLayout.PAGE_START position.
Then put that JPanel wherever it is needed in the GUI, nesting JPanels, each using their own layout manager.
The BorderLayout will allow the center component to fill that position, and will expand the PAGE_START and PAGE_END positions to fill the width necessary. If the top and bottom components are wider, then this will also expand the width of the container.

Related

FlowLayout takes up too much vertical space, change height

I have JFrame that uses FlowLayout for buttons and BoxLayout for the JFrame and looks like this:
I need it to look like this:
For some reason the JPanel of the buttons (green) takes up too much space, while the labels on the red panel are all on the same row, instead of each on a different row.
My code is as follows:
import javax.swing.*;
import java.awt.*;
public class ButtonsTest extends JFrame {
private JButton button1 = new JButton("Button1");
private JButton button2 = new JButton("Button2");
private JButton button3 = new JButton("Button3");
private JButton button4 = new JButton("Button4");
private JPanel panel = new JPanel(new FlowLayout());
private JPanel otherPanel = new JPanel();
public ButtonsTest() {
setPreferredSize(new Dimension(200, 200));
setMinimumSize(new Dimension(200, 200));
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
panel.add(button1);
panel.add(button2);
panel.add(button3);
panel.add(button4);
panel.setBackground(Color.GREEN);
add(panel);
setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
otherPanel.add(new Label("1"));
otherPanel.add(new Label("2"));
otherPanel.add(new Label("3"));
otherPanel.setBackground(Color.RED);
add(otherPanel);
pack();
}
public static void main(String[] args) {
ButtonsTest test = new ButtonsTest();
}
}
What is my mistake?
For some reason the JPanel of the buttons (green) takes up too much space
When using a BoxLayout, the components will grow up to the maximum size when extra space is available. So extra space is allocated to both the red and green panels.
Don't set the layout of the content pane to use a BoxLayout.
while the labels on the red panel are all on the same row, instead of each on a different row.
By default a JPanel uses a Flow layout.
The solution is to use the default BorderLayout of the JFrame.
Then you add the green panel to the frame using:
add(panel, BorderLayout.PAGE_START);
Then for the "otherPanel" you can use the BoxLayout:
otherPanel.setLayout( new BoxLayout(otherPanel, BoxLayout.Y_AXIS) );
Then you add the "otherPanel" to the frame using:
add(otherPanel, BorderLayout.CENTER);
Also, components should be added to the frame BEFORE the frame is visible. So the setVisible(...) statement should be the last statement in the constructor.

Trying to add ScrollPane in Jpanel with null layout inside BorderLayout

I am trying to add a scrollbar in jpanel with null layout.
I want to create a form. This should should display few buttons at the bottom at all times.Any content inside form should maintain it's size and ratio even if the parent container is resized.
Here is what I've come with. I have a panel with borderlayout and added buttons at the south of border. Then created another jpanel to contain form that is added at the center of parent jpanel. Since I want form to maintain it's ratio I went with null layout for inner panel. But I want it to display scrollbar when content is not fully visible. enter image description here
Now adding inner jpanel into scrollpane and adding scrollpanel into parent panel (.add(scrollpane, BorderLayout.CENTER)) doesn't give desired format.
Is there any thing that I can do to get desired format?
Here is code Sample:
public static void main(String[] args) {
JFrame jFrame = new JFrame();
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setSize(new Dimension(1000, 700));
Container c = jFrame.getContentPane();
c.setLayout(new BorderLayout());
bottomPanel(c);
centerPanel(c); //scrollbar should go in this panel
jFrame.setVisible(true);
}
private static void centerPanel(Container c) {
JPanel centerPanel = new JPanel();
centerPanel.setLayout(null);
JButton button = new JButton("This jObject should not resize when window resizes and also should maintain relative position.");
button.setBounds(new Rectangle(10, 10, 600, 50));
JButton button1 = new JButton("Just like it works in this code. Just Add ScrollPane to centerPanel That is in green backround");
button1.setBounds(new Rectangle(10, 70, 600, 50));
JButton button2 = new JButton("For clearity");
button2.setBounds(new Rectangle(10, 130, 600, 50));
centerPanel.add(button);
centerPanel.add(button1);
centerPanel.add(button2);
centerPanel.setBackground(Color.GREEN);
c.add(centerPanel, BorderLayout.CENTER);
}
private static void bottomPanel(Container c) {
JPanel bottomPanel = new JPanel(); //Buttons that goes at the bottom of screen will go in here
JPanel bottomInnerPanel = new JPanel();
bottomInnerPanel.setLayout(new BorderLayout());
bottomPanel.setLayout(new GridLayout());
bottomInnerPanel.add(new JButton("Add"), BorderLayout.WEST);
bottomInnerPanel.add(new JButton("Search"), BorderLayout.EAST);
bottomPanel.add(bottomInnerPanel);
bottomPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
c.add(bottomPanel, BorderLayout.SOUTH);
}

How to exactly make JComponents background transparent?

There are two things that I am trying to figure out. First thing, I want to figure out how to make a Jcomponent background transparent. When placing a JPanel into another JPanel that has a color set as the background, the JPanel that is set within the other JPanel has a white background that I can't seem to get rid of. I tried using the firstpanel.setOpache function and repaint but it doesn't do anything.
And second, I noticed that putting a JPanel within another JPanel thats within another JPanel compresses it size. The images below will show what I am trying to describe. I want to know what to do to avoid compressing the JPanel size and still able to put it within two levels of other JPanels . The code below is what I am practicing with.
import javax.swing.*;
import java.awt.*;
public class LearningFrame extends JFrame {
private JLabel userLabel;
private LearningFrame(){
JPanel backGroundPanel = new JPanel();
JPanel mainPanel = new JPanel();
JPanel firstPanel = new JPanel();
JPanel secondPanel = new JPanel();
userLabel = new JLabel("User");
userLabel.setFont(new Font("Arial",1 ,24));
backGroundPanel.setBackground(new Color(247,211,53));
// backGroundPanel.setLayout(new BoxLayout(backGroundPanel,BoxLayout.Y_AXIS));
setContentPane(backGroundPanel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(1200,800);
setLocationRelativeTo(null);
//backGroundPanel.setLayout(null);
mainPanel.setLayout(new GridLayout(1,2));
firstPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
secondPanel.setLayout(new BoxLayout(secondPanel,BoxLayout.Y_AXIS));
// firstPanel.setBackground(new Color(211,43,185));
secondPanel.setBackground(new Color(34,233,44));
JButton button = new JButton("Click");
JButton button2 = new JButton("Click");
JButton button3 = new JButton("Click");
JButton button4 = new JButton("Click");
firstPanel.add(button);
firstPanel.add(button2);
firstPanel.add(userLabel);
secondPanel.add(button3);
secondPanel.add(button4);
mainPanel.add(firstPanel);
mainPanel.add(secondPanel);
backGroundPanel.add(mainPanel);
setVisible(true);
}
public JPanel logPanel() {
JPanel logInPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
JTextField userTextField = new JTextField(14);
JTextField passTextField = new JTextField(14);
userLabel = new JLabel("Username: ");
JLabel passLabel = new JLabel("Password: ");
passLabel.setFont(new Font("Arial", Font.BOLD, 24));
userLabel.setFont(new Font("Arial", Font.BOLD, 24));
logInPanel.add(userLabel);
logInPanel.add(userTextField);
logInPanel.add(passLabel);
logInPanel.add(passTextField);
logInPanel.setOpaque(true);
logInPanel.repaint();
return logInPanel;
}
public static void main(String[] args){
LearningFrame x = new LearningFrame();
}
}
Just use
firstPanel.setOpaque(false);
This is will make the background of the component invisible, but you will still be able to see any components that are positioned inside it.

Trying to get two buttons drawn on top of each other

I;m trying to have a top button and a buttom button. But only the buttom button gets drawn.
Here is the code
add(panel1,BorderLayout.NORTH);
add(panel2,BorderLayout.NORTH);
the complete function
private void initUI() {
/////////////////////////////////////////////////////////////////////////////
// set upui
setTitle("Simple example");
// Set size to match screen
mWidth=(int)Toolkit.getDefaultToolkit().getScreenSize().getWidth()-50;
mHeight=(int)Toolkit.getDefaultToolkit().getScreenSize().getHeight()-50;
setSize( mWidth, mHeight);
setLocationRelativeTo(null);
// Set close operation to exit
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel panel1 =new JPanel();
JButton btn = new JButton("Top Button"); // Button is a Component
btn.addActionListener(this);
panel1.add(btn);
JPanel panel2 =new JPanel();
JButton btn2 = new JButton("Buttom Button"); // Button is a Component
panel2.add(btn2);
add(panel1,BorderLayout.NORTH);
add(panel2,BorderLayout.NORTH);
// Add the chart
// NOTE class DrawCompoment is defifin below where the drawing ooeration is overidden
DrawComponent test = new DrawComponent();
add(test,BorderLayout.CENTER);
///////////////////////////////////////////////////////////////////////////////
// sert up vscreen
vStartX=(double)10;
vStartY=10;
vWidth=mWidth-40;
vHeight=mHeight-80;
dx=vWidth/t.daySize;
dy=vHeight/t.dayBiggest;
// save fdata in spreads sheet
createSpreadSheet();
}
You're adding panel1 and panel2 both to the same BorderLayout position, and only one can be added there. You need perhaps another JPanel to hold them both and then add that one to the BorderLayout.NORTH spot.
e.g.,
JPanel panel1 = new JPanel();
JButton btn = new JButton("Top Button"); // Button is a Component
btn.addActionListener(this);
panel1.add(btn);
JPanel panel2 = new JPanel();
JButton btn2 = new JButton("Buttom Button"); // Button is a Component
panel2.add(btn2);
// A JPanel to hold both panel1 and panel2
JPanel containerPanel = new JPanel(new GridLayout(2, 1));
containerPanel.add(panel1);
containerPanel.add(panel2);
// add only one component to the BorderLayout.NORTH position of the JFrame
add(containerPanel, BorderLayout.NORTH);

(Java) There are white borders around all my buttons

In java, My buttons are surrounded by white, thick borders that only appear when I have a image in a jlabel for the background.
Example:
The problem here is that white borders go around all of my buttons, making it look terrible. The intended use was for the buttons to just go over the image, like this image:
Here is my code:
public class Gui extends JFrame {
private JTextField TextField;
private JButton Hi, Bye, Exit;
private JPanel Panel, Panel1, Panel2;
private JLabel label;
public Gui() {
super("My Program");
TextField = new JTextField("");
TextField.setEditable(false);
Hi = new JButton("Hi");
Bye = new JButton("Bye");
Exit = new JButton("Exit");
Actions a = new Actions();
Hi.addActionListener(a);
Bye.addActionListener(a);
Exit.addActionListener(a);
Dimension tfd = new Dimension(780, 25);
Dimension bd = new Dimension (75, 25);
Dimension lpd = new Dimension (800, 600);
TextField.setPreferredSize(tfd);
Hi.setPreferredSize(bd);
Bye.setPreferredSize(bd);
Exit.setPreferredSize(tfd);
ImageIcon image = new ImageIcon("C:/Users/Dakota/Desktop/Coding/Coding/img/Background.png");
label = new JLabel(image);
Panel = new JPanel();
this.setContentPane(label);
this.add(Panel);
this.setLayout(new FlowLayout());
Panel.add(TextField);
Panel1 = new JPanel();
this.add(Panel1);
Panel1.add(Hi);
Panel1.add(Bye);
Panel2 = new JPanel();
this.add(Panel2);
Panel2.add(Exit);
}
Your problem appears that you're adding JPanels to your GUI and not changing the opaque property. This property is by default true, meaning that the JPanel will paint a background that visually covers up anything below it, but if you set it to false by calling myPanel.setOpaque(false);, then any components below the JPanel will show through. This is true for most any Swing component, although is not quite straight-forward for component components such as JScrollPanes, and JTextComponents.

Categories