Java swing jpanel dimension - java

JLabel titleLabel = new JLabel(title, SwingConstants.CENTER);
titleLabel.setForeground(Color.DARK_GRAY);
titleLabel.setFont(new Font("Comic Sans MS", Font.PLAIN, 15));
add(titleLabel, BorderLayout.PAGE_START);
JLabel descLabel = new JLabel("<html>description");
descLabel.setForeground(Color.GRAY);
descLabel.setFont(new Font("Comic Sans MS", Font.PLAIN, 13));
add(descLabel, BorderLayout.PAGE_START);
JLabel picLabel = new JLabel(new ImageIcon(Home.class.getResource("/icon/img.jpg")));
picLabel.setIconTextGap(-310);
picLabel.setOpaque(true);
picLabel.setLayout(null);
add(picLabel);
JPanel buttonPanel = new JPanel();
buttonPanel.setBorder(new LineBorder(Color.BLACK, 1, true));
JButton btnDetails = new JButton("Dettagli");
JButton btnDetails2 = new JButton("Modifica");
buttonPanel.add(btnDetails);
buttonPanel.add(btnDetails2);
add(buttonPanel);
I want to change the dimension of the panel (see the image) and align it with the image.
Any suggestion ?

Change the layout of your main panel to use BorderLayout.
Then add the picLabel to be add(picLabel, BorderLayout.CENTER) and your buttonPanel as add(buttonPanel, BorderLayout.SOUTH) and you should get better layout.
Packing the dialog / frame will also help.
You cannot add two labels to the same position in a BorderLayout. Your label descLabel will replace your titleLabel
You could do that like the buttons: Create another panel, add both labels vertically and add that to your main panel with BorderLayout.NORTH (or PAGE_START)

Related

JPanel not displaying JLabel

I am trying to make an application, which displays a JLabel and a button, which if clicked switches to another jPanel. For some reason my JLabel is not displaying at all in either case. I would appreciate an expert eye to look over my code and see what I am doing wrong. Thanks in advance.
HomeScreenUI(){
//frame
JFrame frame = new JFrame("Opisa");
//panels, one before button click and one after
JPanel panel = new JPanel();
JPanel panelAfterButtonClick = new JPanel();
panel.setLayout(null);
panelAfterButtonClick.setLayout(null);
//jlabel that isnt displaying + dimensions
JLabel label = new JLabel("Opisa");
Dimension size = label.getPreferredSize();
label.setBounds(100, 100, size.width, size.height);
label.setFont(new Font("Helvetica", Font.PLAIN, 70));
//second jlabel that isn't displaying
JLabel label2 = new JLabel("Opisa");
Dimension size4 = label2.getPreferredSize();
label2.setBounds(100, 100, size4.width, size4.height);
label2.setFont(new Font("Helvetica", Font.PLAIN, 70));
//adding the labels to the panels
panel.add(label);
panelAfterButtonClick.add(label2);
//button that is displaying both before and after
JButton button = new JButton("Click Me..");
JButton buttonAfterClick = new JButton("Clicked Me..");
//dimensions
Dimension size2 = button.getPreferredSize();
button.setBounds(100, 100, size2.width, size2.height);
Dimension size3 = button.getPreferredSize();
buttonAfterClick.setBounds(100, 100, size3.width, size3.height);
//adding the buttons to the jpanel
panel.add(button);
panelAfterButtonClick.add(buttonAfterClick);
//function that changes the panel after the button is clicked
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
frame.setContentPane(panelAfterButtonClick);
frame.invalidate();
frame.validate();
}
});
//adding the panel to the frame and setting the size
frame.add(panel);
frame.setSize(1000,800);
frame.setVisible(true);
}
Check the label bounds, more specifically its size... try setting some fixed values (great enough to show its content like setBounds(100, 100, 250, 80)).
The preferred size is being retrieved before changing the font size, so it is not big enough to show that big characters. Try changing the font first.

How to arrange components using BorderLayout?

I'm trying to get my GUI to appear as such:
Grocery Cart [Refill]
(TextArea)
I am currently using BorderLayout and I would like to stick with it. How can I get the text area underneath the JLabel and the JButton whilst being in the same JPanel? Here is my code for the specific area:
How do I add the text box underneath the two side by side? Whenever I add it, it just goes next to them.
JPanel newPanel = new JPanel();
JLabel label = new JLabel("Grocery Cart");
label.setFont(new Font("Arial", Font.BOLD, 20));
newPanel.add(label);
contentPane.add(newPanel, BorderLayout.WEST) ;
JButton btnNewButton = new JButton("Refill");
btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 20));
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
newPanel.add(btnNewButton);
If my understanding is correct, here's what you need to do:
JPanel mainPanel = new JPanel(new BorderLayout());
JPanel eastPanel = new JPanel(new BorderLayout());
JTextArea area = new JTextArea("Test content");
JLabel label = new JLabel("Grocery Cart");
label.setFont(new Font("Arial", Font.BOLD, 20));
mainPanel.add(label, BorderLayout.WEST);
JButton btnNewButton = new JButton("Refill");
btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 20));
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
eastPanel.add(btnNewButton, BorderLayout.WEST);
eastPanel.add(area, BorderLayout.CENTER);
mainPanel.add(eastPanel, BorderLayout.CENTER);
contentPane.add(mainPanel, BorderLayout.NORTH);
The main idea is that in order to construct complex layouts with simple layout types such as border and flow, you have to use container hierarchy and get creative with a combination of flow and border layouts.
In my example the label and button aren't resizable and always have their widths equal to their preferred widths. The text area, however is resizable and takes up its container's remaining width.
Note, that all components added to mainPanel are resizable vertically. In order to keep mainPanel to its preferred height you place it to the contentPane's BorderLayout.NORTH or SOUTH for that matter.

Not able to scroll my JPanel which contains several other panels using JScrollPane

I am having one main JPanel in which I had used several other panels. I want to add a scroll pane to my panel so that when child panel go out of frame they should be scrollable, but I am unable to achieve this.
How to achieve correct scrolling functionality for panels within a panel?
Code inside my child panel:
public class Page13111SubPanel extends JPanel {
/**
* Create the panel.
*/
public Page13111SubPanel() {
// setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
setSize(1000,130);
setLayout(new GridLayout(3, 3, 40, 35));
JLabel label= new JLabel();
label.setText("Vehicle Companies:asdddddddddddddddd1");
label.setFont(new Font("Monotype Corsiva", Font.ITALIC, 20));
add(label);
JLabel label1= new JLabel();
label1.setText("Vehicle Companies:asdddddddddddddddd2");
label1.setFont(new Font("Monotype Corsiva", Font.ITALIC, 20));
add(label1);
JLabel label2= new JLabel();
label2.setText("Vehicle Companies:asdddddddddddddddd3");
label2.setFont(new Font("Monotype Corsiva", Font.ITALIC, 20));
add(label2);
JLabel label3= new JLabel();
label3.setText("Vehicle Companies:asdddddddddddddddd4");
label3.setFont(new Font("Monotype Corsiva", Font.ITALIC, 20));
add(label3);
// JLabel label3= new JLabel();
// label3.setText("Vehicle Companies:asdddddddddddddddd4");
// label3.setFont(new Font("Monotype Corsiva", Font.ITALIC, 20));
// add(label3);
JLabel label4= new JLabel();
label4.setText("Vehicle Companies:asdddddddddddddddd5");
label4.setFont(new Font("Monotype Corsiva", Font.ITALIC, 20));
add(label4);
JLabel label5= new JLabel();
label5.setText("Vehicle Companies:asdddddddddddddddd6");
label5.setFont(new Font("Monotype Corsiva", Font.ITALIC, 20));
add(label5);
JLabel label6= new JLabel();
label6.setText("Vehicle Companies:asdddddddddddddddd7");
label6.setFont(new Font("Monotype Corsiva", Font.ITALIC, 20));
add(label6);
}
}
Code inside my main panel:
public class Page1311Test extends JPanel {
// private JTable table;
public JScrollPane pane=null;
public JPanel panel=null;
public JButton back=null;
/**
* Create the panel.
*/
public Page1311Test() {
JPanel panel=new JPanel();
panel.setLayout(new GridLayout(7,1,1,40));
Page13111SubPanel[] panel1=new Page13111SubPanel[7];
panel1[0]=new Page13111SubPanel();
panel.add(panel1[0]);
panel1[1]=new Page13111SubPanel();
panel.add(panel1[1]);
panel1[2]=new Page13111SubPanel();
panel.add(panel1[2]);
panel1[3]=new Page13111SubPanel();
panel.add(panel1[3]);
panel1[4]=new Page13111SubPanel();
panel.add(panel1[4]);
panel1[5]=new Page13111SubPanel();
panel.add(panel1[5]);
panel1[6]=new Page13111SubPanel();
panel.add(panel1[6]);
pane=new JScrollPane(panel,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
add(pane);
}
public static void main(String[] args) {
JFrame frame=new JFrame();
frame.setExtendedState(frame.MAXIMIZED_BOTH);
frame.setLocation(0, 0);
frame.add(new Page1311Test());
frame.setVisible(true);
}
}
Changing the main panel from FlowLayout (the default) to a layout that uses the child components of the panel to fill the available space (used GridLayout here) solves the problem. E.G.
import java.awt.*;
import javax.swing.*;
public class Page1311Test extends JPanel {
public JScrollPane pane = null;
public JPanel panel = null;
/**
* Create the panel.
*/
public Page1311Test() {
setLayout(new GridLayout());
//JPanel panel = new JPanel(); //shadowed class attribute
panel = new JPanel(new GridLayout(0, 1, 1, 40));
for (int ii = 0; ii < 17; ii++) {
panel.add(new Page13111SubPanel(ii));
}
pane = new JScrollPane(panel,
ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
add(pane);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setExtendedState(frame.MAXIMIZED_BOTH);
frame.setLocation(0, 0);
frame.add(new Page1311Test());
frame.pack();
frame.setVisible(true);
}
}
class Page13111SubPanel extends JPanel {
/**
* Create the panel.
*/
public Page13111SubPanel(int num) {
//setSize(1000, 130); // Don't set sizes using 'magic numbers'
setLayout(new GridLayout(3, 3, 40, 35));
add(new JLabel(num + " Vehicle Companies:asdddddddddddddddd1"));
add(new JLabel("Vehicle Make:"));
add(new JLabel("Vehicle Model"));
add(new JLabel("Vehicle Number"));
add(new JLabel("Vehicle Driver:"));
add(new JLabel("Vehicle Wheels"));
add(new JLabel("Vehicle Air Con"));
}
}
I am guessing the problem lies in your GridLayout and setSize calls. When you call, essentially your layout manager is going to overwrite you setSize call.
In the following example, I set the preferred and minimum sizes and the scroll pane works fine.
JFrame frame = new JFrame("Grids of Grids");
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3, 3));
for(int i = 0; i<9; i++){
JPanel localGrid = new JPanel();
localGrid.setMinimumSize(new Dimension(250, 250));
localGrid.setPreferredSize(new Dimension(250, 250));
localGrid.setLayout(new GridLayout(5, 5));
for(int j = 0; j<25; j++){
localGrid.add(new JLabel("" + i + ":: " + j));
}
panel.add(localGrid);
}
JScrollPane scroll = new JScrollPane(panel);
frame.add(scroll);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Otherwise, your scrollable area will actuall be pretty small because the layouts will try to fit it into the available space.
I think your problem is that the main Panel is not scrollable, therefore you cannot scroll at all.
Try using a JScrollPane there and take a look at this tutorial to get more information about JScrollPanes. There are also lots of information about other GUI-Objects like Frames and Panels, so I think you can find a solution for your problem!

How to center buttons in a panel

Although I'm using BorderLayout.CENTER, my group of buttons still appears to be aligning to the north of the panel. If I use BorderLayout.SOUTH their relative position is the same as BorderLayout.CENTER but to the south of the panel.
How can I get them to be in the middle of the panel?
Is there anything I'm doing that is glaringly wrong?
public void createExecuteArea() {
JButton connectButton = new JButton("Connect");
connectButton.setPreferredSize(new Dimension(100, 40));
JButton disconnectButton = new JButton("Disconnect");
disconnectButton.setPreferredSize(new Dimension(100, 40));
JButton abortButton = new JButton("Abort");
abortButton.setPreferredSize(new Dimension(100, 40));
executePanel = new JPanel();
executePanel.setLayout(new BorderLayout());
JPanel buttonPanel = new JPanel();
buttonPanel.add(connectButton);
buttonPanel.add(disconnectButton);
buttonPanel.add(abortButton);
executePanel.add(buttonPanel, BorderLayout.CENTER);
}
The following changes to my code resolved my issues.
public void createExecuteArea() {
JButton connectButton = new JButton("Connect");
connectButton.setPreferredSize(new Dimension(100, 40));
JButton disconnectButton = new JButton("Disconnect");
disconnectButton.setPreferredSize(new Dimension(100, 40));
JButton abortButton = new JButton("Abort");
abortButton.setPreferredSize(new Dimension(100, 40));
executePanel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JPanel buttonPanel = new JPanel();
buttonPanel.add(connectButton);
buttonPanel.add(disconnectButton);
buttonPanel.add(abortButton);
executePanel.add(buttonPanel, c);
}
The issue is with executePanel and what layout it is using. You don't give it an explicit layout, and so it uses a BorderLayout by default. If you want to center your buttons within this JPanel, then consider using a different layout, perhaps a GridBagLayout.
For more specific help, consider creating and posting a minimal example program.

Java cardlayout dosen't show textarea

i want the user to click on a image and the panel with cardlayout changes for every image clicked. so i have one panel with a textarea and one with just a blue backgound, when i start the program the panel with the textarea show without textarea, when i click the image it show the blue panel, why cant i see the textarea?
i have removed the location of the image in the code
frame = new JFrame("Sandwich deLuxe");
frame.setBounds(100, 100, 741, 522);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(10, 132, 705, 89);
frame.getContentPane().add(panel);
panel.setLayout(null);
JToolBar toolBar = new JToolBar();
toolBar.setBounds(10, 0, 705, 22);
frame.getContentPane().add(toolBar);
panelCont.setBounds(10, 221, 544, 251);
frame.getContentPane().add(panelCont);
panelCont.setLayout(cl);
JPanel panelTest1 = new JPanel();
JTextArea txtrGhg = new JTextArea();
txtrGhg.setForeground(Color.BLACK);
txtrGhg.setBackground(UIManager.getColor("Button.background"));
txtrGhg.setEditable(false);
txtrGhg.setText("Velkommen til Sandwich deLuxe\r\n\r\nK\u00F8b din mad her!\r\n\r\n1. V\u00E6lg en af kategoriene fra oven.\r\n2. V\u00E6lg dinne retter/sandwichs.\r\n3. Bekr\u00E6ft k\u00F8bet i indk\u00F8bskurven.\r\n4. Din bestilling er modtaget og vi g\u00E5r straks \r\n i gang med at tilberede din mad. ");
txtrGhg.setFont(new Font("Monospaced", Font.PLAIN, 18));
panelTest1.add(txtrGhg);
panelCont.add(panelTest1, "1");
JPanel panelTest2 = new JPanel();
panelTest2.setBackground(Color.BLUE);
panelCont.add(panelTest2,"2");
cl.show(panelCont, "1");
JLabel lblNewLabel = new JLabel("New label");
lblNewLabel.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent arg0) {
cl.show(panelCont, "2");
}
});
lblNewLabel.setIcon(new ImageIcon(""));
lblNewLabel.setBounds(28, 11, 97, 67);
panel.add(lblNewLabel);
JScrollPane scrollBar = new JScrollPane(txtrGhg, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollBar.setViewportBorder(null);
scrollBar.setPreferredSize(new Dimension(300,200));
scrollBar.setBorder(BorderFactory.createEmptyBorder());
panelCont.add(scrollBar, "name_171726698118247");
panelCont.setOpaque(true);
At the end of your code you create a JScrollPane thwarting the setup you have created before. You pass txtrGhg in JScrollPane’s constructor. So the scroll pane reparents your text area as your JTextArea cannot be contained in two different containers at the same time. After that, panelTest1 does not contain your JTextArea “txtrGhg” anymore but the CardLayout will still show panelTest1 as you have requested. But it’s empty.

Categories

Resources