How to stop JTextArea from being placed on top of JTabbedPane? - java

I am trying to design a layout with a JTabbedPane at the top of the frame and then a jLogArea below the tabbed pane.
I am using this code:
setLayout(new BorderLayout());
tabbedPane.setSize(WIDTH, HEIGHT);
add(tabbedPane, BorderLayout.PAGE_START);
tabbedPane.add("Tab 0", null);
scrollableTextArea = new JScrollPane(jTextArea);
jTextArea.setEditable(false);
jTextArea.setLineWrap(true);
scrollableTextArea.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollableTextArea.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
add(scrollableTextArea, BorderLayout.PAGE_END);
However, the result of this is that the text area is placed behind the tabbed pane:
Does anyone know what I am doing wrong and how I can fix it? Thanks.
EDIT: Just to be clear, I am looking for the text area to be below the JTabbedPane, not in the tab iself.
Using BorderLayout.NORTH and BorderLayout.SOUTH does not help either. I added a label into the tab's contents just to see if that would make a difference but the text area still goes behind, this is how it looks:
Further code (the class extends JFrame):
public MainGUI() {
init();
pack();
super.setTitle("test");
}
public void init() {
setLayout(new BorderLayout());
setPreferredSize(new Dimension(WIDTH, HEIGHT + TEXT_AREA_HEIGHT));
setMaximumSize(new Dimension(WIDTH, HEIGHT + TEXT_AREA_HEIGHT));
setMinimumSize(new Dimension(WIDTH, HEIGHT + TEXT_AREA_HEIGHT));
tabbedPane = new JTabbedPane();
textArea = new JTextArea(WIDTH, TEXT_AREA_HEIGHT);
scrollableTextArea = new JScrollPane(textArea);
JLabel testLabel = new JLabel("Test!");
tabbedPane.add("Tab 0", testLabel);
tabbedPane.setBorder(null);
tabbedPane.setSize(WIDTH, HEIGHT);
add(tabbedPane, BorderLayout.NORTH);
textArea.setEditable(false);
textArea.setLineWrap(true);
scrollableTextArea.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollableTextArea.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
add(scrollableTextArea, BorderLayout.SOUTH);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
setVisible(true);
}

UPDATE I think you are looking for something like this:
import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
public class TabSample extends JFrame{
public void createAndShowGUI() {
JPanel panel = new JPanel();
JTextArea ta = new JTextArea(100,50);
JScrollPane jsp = new JScrollPane(ta);
JTabbedPane tabbedPane = new JTabbedPane();
panel.setLayout(new BorderLayout());
tabbedPane.addTab("Tab one", panel);
JSplitPane vPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, tabbedPane, jsp);
getContentPane().add(vPane);
setSize(400,500);
vPane.setDividerLocation(getHeight()/2);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String args[])
{
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
#Override
public void run()
{
TabSample ts = new TabSample();
ts.createAndShowGUI();
}
});
}
}

Your code should be like this:
setLayout(new BorderLayout());
tabbedPane.setSize(WIDTH, HEIGHT);
add(tabbedPane, BorderLayout.PAGE_START);
scrollableTextArea = new JScrollPane(jTextArea);
jTextArea.setEditable(false);
jTextArea.setLineWrap(true);
scrollableTextArea.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollableTextArea.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
tabbedPane.addTab("Tab 0", scrollableTextArea);
The problem in your code was that you added the JScrollPane at the same level as you added your JTabbedPane. Actually your JScrollPane has to be a tab component:
tabbedPane.addTab("Tab 0", scrollableTextArea);
EDIT: to have the scroll pane below the tabbed pane:
...
add(tabbedPane, BorderLayout.NORTH);
...
add(scrollableTextArea, BorderLayout.SOUTH);
Hope this works for you.

Related

How can I create a frame with a BorderLayout and assign each space a component?

When I type .setLayout(new BorderLayout());
It appears me this: The method setLayout(LayoutManager) in the type JFrame is not applicable for the arguments (BorderLayout)
I´m a beginner and I was following a video but this does not work and I already watched different videos, thank you so much for your help.
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class BorderLayout {
public static void main(String []args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setLayout(new BorderLayout());
frame.setVisible(true);
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JPanel panel4 = new JPanel();
JPanel panel5 = new JPanel();
panel1.setBackground(Color.red);
panel2.setBackground(Color.green);
panel3.setBackground(Color.yellow);
panel4.setBackground(Color.magenta);
panel5.setBackground(Color.blue);
panel1.setPreferredSize(new Dimension(100, 100));
panel2.setPreferredSize(new Dimension(100, 100));
panel3.setPreferredSize(new Dimension(100, 100));
panel4.setPreferredSize(new Dimension(100, 100));
panel5.setPreferredSize(new Dimension(100, 100));
frame.add(panel1, BorderLayout.NORTH);
frame.add(panel2, BorderLayout.WEST);
frame.add(panel3, BorderLayout.EAST);
frame.add(panel4, BorderLayout.SOUTH);
frame.add(panel5, BorderLayout.CENTER);
}
}
Move frame.setVisible(true); to the end of your method. Swing layouts are lazy, they won't "magically" update by themselves, instead, you need to tell it when you want a container to be updated, using revalidate and repaint to schedule a new layout and pass pass, but, as I've said, the easiest thing to do in your case, is simply setup the window before you make it visible.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.EventQueue;
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JPanel panel4 = new JPanel();
JPanel panel5 = new JPanel();
panel1.setBackground(Color.red);
panel2.setBackground(Color.green);
panel3.setBackground(Color.yellow);
panel4.setBackground(Color.magenta);
panel5.setBackground(Color.blue);
panel1.setPreferredSize(new Dimension(100, 100));
panel2.setPreferredSize(new Dimension(100, 100));
panel3.setPreferredSize(new Dimension(100, 100));
panel4.setPreferredSize(new Dimension(100, 100));
panel5.setPreferredSize(new Dimension(100, 100));
frame.add(panel1, BorderLayout.NORTH);
frame.add(panel2, BorderLayout.WEST);
frame.add(panel3, BorderLayout.EAST);
frame.add(panel4, BorderLayout.SOUTH);
frame.add(panel5, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
});
}
}
That's because the name of your class is the same as BorderLayout layout. Change name of your class and it should work perfectly fine. Never use a keyword or something like that in naming an object/class/method etc.

Java JFrame: Windows Layout & Embed

I need to define a layout for a Jframe Window, as in the picture above.
Below is my approach.
A Picture from my resources folder (/resources/...jpg) embed inside the middle(main).
Top, Bottom, Left and Right divided in four parts, whereas their content is a labeled button stretched, so I can map some methods on it later, that change the picture inside the main container.
I tried to display the picture, but I get the result you see in my screenshot. I can't see it inside my main container and I receive no error message.
I don't know if this is because of my wrong approach of using JFrame.
Below you can see my code, I'd be happy if you could help me solving my wrong design layout pattern too.
MyFrame.java
package ms0.gui;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class MyFrame extends JFrame {
public MyFrame () {
setTitle("This is an example title");
setSize(600,600);
setLocation(750,640);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
//Main Container
Container mainContainer = this.getContentPane();
mainContainer.setLayout(new BorderLayout(8,6));
mainContainer.setBackground(Color.BLUE);
this.getRootPane().setBorder(BorderFactory.createMatteBorder(4, 4, 4, 4, Color.green));
//JButton Positions
JButton topButton = new JButton("Oben");
JButton bottomButton = new JButton("Unten");
JButton leftButton = new JButton("Links");
JButton rightButton = new JButton("Rechts");
//Panel Top
JPanel topPanel = new JPanel();
topPanel.setBorder(new LineBorder(Color.BLACK, 3));
topPanel.setBackground(Color.ORANGE);
topPanel.setLayout(new FlowLayout(5));
topPanel.add(topButton);
mainContainer.add(topPanel, BorderLayout.NORTH);
//Panel Middle
JPanel middlePanel = new JPanel();
middlePanel.setBorder(new LineBorder(Color.black, 3));
middlePanel.setLayout(new FlowLayout(4,4,4));
middlePanel.setBackground(Color.cyan);
//Grid Panel Right
JPanel rightPanel = new JPanel();
rightPanel.setLayout(new FlowLayout(4,4,4));
rightPanel.setBorder(new LineBorder(Color.black, 3));
rightPanel.setBackground(Color.GREEN);
rightPanel.add(rightButton);
//Grid Panel Left
JPanel gridPanel = new JPanel();
gridPanel.setLayout(new GridLayout(4,1,5,5));
gridPanel.setBorder(new LineBorder(Color.black, 3));
gridPanel.setBackground(Color.red);
gridPanel.add(leftButton);
//Center Box
JLabel label = new JLabel("Center Box", SwingConstants.CENTER);
label.setOpaque(true);
label.setBorder(new LineBorder(Color.black,3));
middlePanel.add(gridPanel);
mainContainer.add(label);
mainContainer.add(middlePanel, BorderLayout.WEST);
mainContainer.add(rightPanel, BorderLayout.EAST);
//Panel Bottom
JPanel bottomPanel = new JPanel();
bottomPanel.setLayout(new FlowLayout(3));
bottomPanel.add(bottomButton);
bottomPanel.setBackground(Color.magenta);
bottomPanel.setBorder(new LineBorder(Color.BLUE, 3));
mainContainer.add(bottomPanel, BorderLayout.SOUTH);
//Siegel
String filepath = "/resources/siegel.jpg";
int picWidth = 150;
int picHeight = 150;
ImageIcon image1 = new ImageIcon(getClass().getResource(filepath));
//Image scaledImage = img.getScaledInstance(picWidth, picHeight, Image.SCALE_DEFAULT);
//ImageIcon icon = new ImageIcon(scaledImage);
mainContainer.add(new JButton(image1));
}
}
So, as a very basic example, nothing but BorderLayout
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
public class MyFrame extends JFrame {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
MyFrame frame = new MyFrame();
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public MyFrame() {
setTitle("This is an example title");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new BorderLayout());
add(new JButton("Top button (stretched)"), BorderLayout.NORTH);
add(new JButton("Left button (stretched)"), BorderLayout.WEST);
add(new JButton("Right button (stretched)"), BorderLayout.EAST);
add(new JButton("Bottom button (stretched)"), BorderLayout.SOUTH);
JLabel label = new JLabel("Picture");
label.setBorder(new EmptyBorder(100, 100, 100, 100));
add(label);
}
}
Remember, simple is often best.
Now, if you absolutely, positively must have the label/picture in another container, you can simply make use of GridBagLayout, as it will centre the child component(s) by default, for example...
JLabel label = new JLabel("Picture");
label.setBorder(new EmptyBorder(100, 100, 100, 100));
// Automatic center position
JPanel mainPane = new JPanel(new GridBagLayout());
mainPane.add(label);
add(mainPane);
And you don't have to use EmptyBorder. GridBagLayout will allow to supply insets which will do the same thing

how do you customise position of a JPanel within a Jpanel

The class extends JPanel,
public void createDisplay(){
frame = new JFrame();
frame.setTitle(title);
frame.setSize(new Dimension(width, height));
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setPreferredSize(new Dimension(width, height));
this.setMaximumSize(new Dimension(width, height));
this.setMinimumSize(new Dimension(width, height));
this.setLayout(null); //have tried default and BorderLayout
this.setSize(new Dimension(width, height));
this.setBounds(0, 0, width, height);
//basically trying everything
frame.add(this);
frame.pack();
}
on startup this code works fine and the JPanel completely covers the size of the Parent frame
However my program later tries to add a new JPanel to the class's extend JPanel with:
public void gameOverWindow(){ ;
JPanel panel = new JPanel(new BorderLayout());
panel.setPreferredSize(new Dimension(200, 100));
JLabel label = new JLabel("Game Over");
label.setPreferredSize(new Dimension(40, 15));
//trying setPosition also doesn't work with BorderLayout or FlowLayout
JButton button_01 = new JButton("new");
button_01.setPreferredSize(new Dimension(100, 10));
JButton button_02 = new JButton("exit");
button_02.setPreferredSize(new Dimension(100, 10));
panel.add(label, BorderLayout.NORTH);
panel.add(button_01, BorderLayout.WEST);
panel.add(button_02, BorderLayout.EAST);
this.add(panel);
this.revalidate();
}
This new JPanel appears with the contents within the correct BorderLayout format, however the JPanel itself will remain at the top center of the extended JPanel, I know this is because the default Layout is set to FlowLayout, however setting this to BorderLayout will just cause the panel to take up the entire screen. Setting the Layout to null completely breaks the frame and nothing appears but the Minimize and Close buttons of the Frame. Trying to set the position or Bounds of this JPanel doesn't work with any Layout either. I have read a lot of other post online about this but they all seem to differ and become confusing, how do I gain control of the position of my new JPanel?
Normally I'd recommend using a CardLayout for switching between different views, but it's difficult to ascertain from the available information if that would help or not
Instead, you could make use of compounding layouts. That is wrap one container in another using different layouts.
In this example, I simply use a combination of BorderLayout and GridBagLayout
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new BorderLayout());
JButton gameOver = new JButton("Game over man");
gameOver.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
JPanel panel = new JPanel(new BorderLayout());
panel.add(new JLabel("Game Over Man", JLabel.CENTER), BorderLayout.NORTH);
panel.add(new JButton("New"), BorderLayout.WEST);
panel.add(new JButton("Exit"), BorderLayout.EAST);
removeAll();
JPanel inner = new JPanel(new GridBagLayout());
inner.add(panel);
add(inner);
revalidate();
repaint();
}
});
JPanel inner = new JPanel(new GridBagLayout());
inner.add(gameOver);
add(inner);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
}
what is the purpose of removing the components of a different panel instead of just directly adding it to GridBagLayout?
Because they interfere with the layout of other components.
i then want a small Jpanel to popup within and be unobtrusive
You could make use of the frame's glassPane or use a OverlayLayout
For example:
Floating JPanel above a JPanel with BorderLayout
Rectangle is not drawn on top
Placing a marker within the image
Display a message on the screen
Much of this information should have been in your original question, it would have wasted less of each other's time

Java - Add two tabs into JPanel

I have problem with tabs in the JPanel. I know how to make new tabs in Mainframe, but I don't know how to make tabs into JPanel which is located in Mainframe.
Here are the pictures:
I have program looking like this -
http://www.bildites.lv/viewer.php?file=vklfhvfdfpwpcxllfqv.png
But I want to make it look like this -
http://www.bildites.lv/viewer.php?file=bvbrp4qfx2krn9bkx30j.png
And Here is my code of the blue JPanel:
package gui;
import java.awt.Color;
import javax.swing.JPanel;
public class CallsPanel extends JPanel {
private MainFrame frame;
Color color = new Color(99, 184, 255); // steelblue
public CallsPanel(MainFrame frame) {
this.frame = frame;
this.setLocation(0, 0);
this.setSize(300, 380);
this.setLayout(null);
this.setBackground(color);
this.initContent();
}
// -------------------------------------------------------------------------
// Declare New Things
private void initContent() {
// Add New Things
}
// -------------------------------------------------------------------------
}
Thanks a lot to people that will help!
JTabbedPane tabPane = new JTabbedPane();
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JLabel label1 = new JLabel("Tab 1");
JLabel label2 = new JLabel("Tab 2");
panel1.add(label1);
panel2.add(label2);
tabPane.add("Tab 1", panel1);
tabPane.add("Tab 2", panel2);
this.add(tabPane);
Play around with the size/color/shape of the tabPane and see what works for you. But this is the basic of a tabPane.
See this simple runnable example
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
public class MyPanel extends JPanel {
JButton button = new JButton("Button");
JTabbedPane tabPane = new JTabbedPane();
public MyPanel(){
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
tabPane.add("Panel 1", panel1);
tabPane.add("Panel 2", panel2);
tabPane.setBorder(new EmptyBorder(10, 10, 10, 10));
setLayout(new BorderLayout());
add(tabPane, BorderLayout.CENTER);
add(button, BorderLayout.SOUTH);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame();
frame.add(new MyPanel());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationByPlatform(true);
frame.setSize(300, 300);
frame.setVisible(true);
}
});
}
}

Aligning a JLabel with a JScrollPane in a BoxLayout

I'm trying to align a JLabel and a JScrollPane (containing a JTextArea) to the left of a JPanel. When I put the JTextArea directly in the panel, the alignment is correct. The alignment is only incorrect if the JTextArea is in the scroll pane.
import javax.swing.BoxLayout;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class Main {
public static void main(String[] args) {
JDialog dialog = new JDialog();
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
panel.add(new JLabel("My Label"));
// panel.add(new JTextArea(3, 15));
panel.add(new JScrollPane(new JTextArea(3, 15)));
dialog.add(panel);
dialog.pack();
dialog.setVisible(true);
}
}
The first image below is with the scroll pane and the second image is without it. How can I align the scroll pane correctly?
Try to use alignmentX:
import java.awt.Component;
import javax.swing.*;
public class Main {
public static void main(String[] args) {
JDialog dialog = new JDialog();
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
JLabel label = new JLabel("My Label");
label.setAlignmentX(Component.LEFT_ALIGNMENT);
panel.add(label);
JScrollPane pane = new JScrollPane(new JTextArea(3, 15));
pane.setAlignmentX(Component.LEFT_ALIGNMENT);
panel.add(pane);
dialog.add(panel);
dialog.pack();
dialog.setVisible(true);
}
}
Replace:
panel.add(new JLabel("My Label"));
By:
JPanel labelPan = new JPanel(new FlowLayout(FlowLayout.LEFT);
labelPan.add(new JLabel("My Label"));
panel.add(labelPan);

Categories

Resources