Java cardlayout dosen't show textarea - java

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.

Related

Java swing scrollpane doesn't show the scroll bar

I was working on a java swing gui project for my course. When I was doing that, I found that I had too much information in a panel and it was not able to show everything. As a result, I wanted to add a Jscrollpane and I did some research about how to use this function but it didn't seem to work for my project, and I had no reason why even after I tried almost everything I could find on google.
Here is my code:
JFrame frame = new JFrame();
JPanel listpanel = new JPanel();
JScrollPane musiclist = new JScrollPane();
JButton selectButton = new JButton("Select song");
frame.setTitle("Music Player");
frame.getContentPane().setBackground(Color.DARK_GRAY);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);
frame.setBounds(190,100,840,600);
Container c = frame.getContentPane();
musiclist.setViewportView (listpanel);
musiclist.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
musiclist.setPreferredSize(new Dimension(10, 1100));
musiclist.setBounds(0, 0, 190, 1000);
musiclist.setLayout(null);
listpanel.setLayout(null);
listpanel.setBackground(Color.GRAY);
listpanel.setBounds(0, 10, 160, 600);
selectButton.setBounds(55,20,100,30);
selectButton.setOpaque(true);
selectButton.setBackground(Color.LIGHT_GRAY);
selectButton.setBorder(null);
selectButton.setBorderPainted(false);
listpanel.add(selectButton);
c.add(musiclist, BorderLayout.WEST);
frame.setVisible(true);
musiclist.setVisible(true);
The problem now is that the scroll bar never shows up even I set the vertical to always show. I am just new to java, so any help will be helpful and thank you all for your time!
JFrame frame = new JFrame();
JPanel listpanel = new JPanel();
JScrollPane musiclist = new JScrollPane();
JButton selectButton = new JButton("Select song");
frame.setTitle("Music Player");
frame.getContentPane().setBackground(Color.DARK_GRAY);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(190,100,840,600);
Container c = frame.getContentPane();
musiclist.setViewportView (listpanel);
musiclist.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
musiclist.setPreferredSize(new Dimension(100, 1100));
musiclist.setBounds(0, 0, 190, 1000);
listpanel.setBackground(Color.GRAY);
listpanel.setBounds(0, 10, 160, 600);
selectButton.setBounds(55,20,100,30);
selectButton.setOpaque(true);
selectButton.setBackground(Color.LIGHT_GRAY);
selectButton.setBorder(null);
selectButton.setBorderPainted(false);
JList mlist=new JList();
DefaultListModel lmodel=new DefaultListModel();
lmodel.addElement("Song 1");
lmodel.addElement("Song 2");
lmodel.addElement("Song 3");
mlist.setModel(lmodel);
listpanel.setLayout(new BorderLayout());
listpanel.add(mlist, BorderLayout.CENTER);
listpanel.add(selectButton, BorderLayout.SOUTH);
c.add(musiclist, BorderLayout.WEST);
musiclist.setVisible(true);
frame.setVisible(true);

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.

Java swing jpanel dimension

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)

Java Panel overlapping Menu

I am new to Java, so this question might be obvious.
I have this Initialization code:
frame = new JFrame();
frame.setTitle("Test");
frame.setBounds(100, 100, 512, 468);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel panel = new MyJPanel();
FlowLayout flowLayout = (FlowLayout) panel.getLayout();
panel.setAlignmentY(Component.TOP_ALIGNMENT);
panel.setAlignmentX(Component.LEFT_ALIGNMENT);
panel.setPreferredSize(new Dimension(0, 0));
panel.setMinimumSize(new Dimension(0, 0));
frame.getContentPane().add(panel, BorderLayout.CENTER);
JMenuBar menuBar = new JMenuBar();
frame.getContentPane().add(menuBar, BorderLayout.NORTH);
JMenu mnFile = new JMenu("File");
menuBar.add(mnFile);
mntmOpenBBinary.setPreferredSize(new Dimension(149, 22));
mnFile.add(mntmOpenBBinary);
JSeparator separator = new JSeparator();
mnFile.add(separator);
JMenuItem mntmExit = new JMenuItem("Exit");
mntmExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
});
mnFile.add(mntmExit);
MyJPanel is a custom class that extends the JPanel class. Just as a test, it just writes "test" to the screen in the paintComponent method:
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.BLACK);
g.setFont(RenderFont);
g.drawString("TEST", 1, 1);
}
You can see from the image below that, for some reason, the drawString method is drawing behind the menu. The coordinates I give in drawString, I'd think, would be the coordinates relative to the JPanel window. Also, the JPanel is "filling" the entire space of the JFrame. I'd prefer that my MyJFrame be only 100x100, but it seems to always want to auto fill the JFrame. How can I solve these 2 issues?
The text is hidden by the menu bar because last parameter of drawString() is the text baseline, and not the upper bound: http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics2D.html#drawString(java.lang.String,%20int,%20int)
So you need to use something like:
g.drawString("TEST", 1, 50);
Or better, use Font.getStringBounds() to compute your text height:
Rectangle2D textBounds = g.getFont().getStringBounds("TEST", (((Graphics2D) g).getFontRenderContext());
And to avoid having your panel taking all Frame space, replace:
frame.getContentPane().add(panel, BorderLayout.CENTER);
with:
JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.add(panel, BorderLayout.NORTH);
frame.getContentPane().add(mainPanel, BorderLayout.CENTER);
Although, you should not need this:
panel.setAlignmentY(Component.TOP_ALIGNMENT);
panel.setAlignmentX(Component.LEFT_ALIGNMENT);
panel.setPreferredSize(new Dimension(0, 0));
panel.setMinimumSize(new Dimension(0, 0));

Categories

Resources