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);
}
});
}
}
Related
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
Problem - the given codes below is not displaying my JPanel(PageOne) and I am not sure why is it not displaying my JPanel(PageOne). Please Help.
I have added the JPanel(PageOne) to my panel which has a cardLayout();
I have set my JFrame to visible already.
PageOne.java
import javax.swing.JLabel;
import javax.swing.JPanel;
public class PageOne extends JPanel {
public PageOne() {
JLabel label = new JLabel("Page 1");
JPanel panel = new JPanel();
panel.add(label);
} }
PageTwo.java
import javax.swing.JLabel;
import javax.swing.JPanel;
public class PageTwo extends JPanel {
public PageTwo() {
JLabel label = new JLabel("Page 2");
JPanel panel = new JPanel();
panel.add(label);
}
}
DisplayUI.java
import java.awt.CardLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class DisplayUI {
public static void main(String[] args) {
new DisplayUI();
}
public DisplayUI() {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
CardLayout cardLayout = new CardLayout();
JFrame frame = new JFrame("frame");
JPanel panel = new JPanel();
panel.setLayout(cardLayout);
panel.add(new PageOne(), "1");
panel.add(new PageTwo(), "2");
cardLayout.show(panel,"1");
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
You're not actually adding anything to PageOne or PageTwo panels...
public PageOne() {
JLabel label = new JLabel("Page 1");
JPanel panel = new JPanel();
panel.add(label);
// But nothing is actually added to "this"...
}
Unless you "really" need it, you can get rid of the second JPanel and add the label directly to PageOne (and the same thing goes for PageTwo)
public PageOne() {
JLabel label = new JLabel("Page 1");
add(label);
}
Or add the JPanel you create (which contains the label)
public PageOne() {
JLabel label = new JLabel("Page 1");
JPanel panel = new JPanel();
panel.add(label);
add(panel);
}
Remember, JPanel is type of Container, it can have child components.
Get the content pane of frame and than try adding :
Container container=frame.getContentPane();
container.add(panel);
Hope this helps you.
I seem to be having some major issues with JLayeredPane. I have a BorderLayout() pane, and I'd like for the West-side element to contain a few JLayeredPane's on top of each other, so I can switch between them to show the right information.
The west pane should be 200 pixels wide and should be as long as the total window is. In my sample code I have added two layers to the JLayeredPanel, but they don't show up. They should be in the west pane.
Here is my code:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
public class Main {
private static JFrame mainFrame = new JFrame();
private static JPanel mainPane = new JPanel();
public Main(){}
public static void initGui(){
JLayeredPane westPanel = new JLayeredPane();
westPanel.setPreferredSize(new Dimension(200,0));
westPanel.setBackground(Color.blue);
JPanel layerOne = new JPanel();
layerOne.add(new JLabel("This is layer 1"));
westPanel.add(layerOne, new Integer(0), 0);
JPanel layerTwo = new JPanel();
layerTwo.add(new JLabel("This si layer 2"));
westPanel.add(layerTwo, new Integer(1), 0);
JPanel centerPanel = new JPanel();
centerPanel.setBackground(Color.yellow);
JPanel eastPanel = new JPanel();
eastPanel.setPreferredSize(new Dimension(200,0));
eastPanel.setBackground(Color.red);
mainPane = new JPanel(new BorderLayout());
mainPane.add(westPanel, BorderLayout.WEST);
mainPane.add(centerPanel, BorderLayout.CENTER);
mainPane.add(eastPanel, BorderLayout.EAST);
mainFrame = new JFrame("Learning to use JLayeredPane");
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setBounds(200, 200, 800, 500);
mainFrame.setContentPane(mainPane);
mainFrame.setVisible(true);
}
public static void main(String[] args) {
initGui();
}
}
What this results in:
JLayeredPane uses a null layout and so you are responsible for stating the size and location of all components added to it. If not they will default to a location of [0, 0] and a size of [0, 0].
try this, its working
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
public class Main {
private static JFrame mainFrame = new JFrame();
private static JPanel mainPane = new JPanel();
public Main(){}
public static void initGui(){
JLayeredPane westPanel = new JLayeredPane();
westPanel.setLayout(null);
westPanel.setPreferredSize(new Dimension(200,0));
westPanel.setBackground(Color.blue);
JPanel layerOne = new JPanel();
layerOne.add(new JLabel("This is layer 1"));
layerOne.setBounds(0, 0, 100, 100);
westPanel.add(layerOne, new Integer(0), 0);
JPanel layerTwo = new JPanel();
layerTwo.add(new JLabel("This si layer 2"));
layerTwo.setBounds(0, 100, 100, 100);
westPanel.add(layerTwo, new Integer(1), 0);
JPanel centerPanel = new JPanel();
centerPanel.setBackground(Color.yellow);
JPanel eastPanel = new JPanel();
eastPanel.setPreferredSize(new Dimension(200,0));
eastPanel.setBackground(Color.red);
mainPane = new JPanel();
mainPane.setLayout(new BorderLayout());
mainPane.add(westPanel, BorderLayout.WEST);
mainPane.add(centerPanel, BorderLayout.CENTER);
mainPane.add(eastPanel, BorderLayout.EAST);
mainFrame = new JFrame("Learning to use JLayeredPane");
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setBounds(200, 200, 800, 500);
mainFrame.setContentPane(mainPane);
mainFrame.setVisible(true);
}
public static void main(String[] args) {
initGui();
}
}
I'm creating a simple Java JFrame in Eclipse with a label, 2 radio buttons with 2 textfields, and a JButton. When i run the program, the objects inside it are messed up, the buttons and textfields don't show up and sometimes a textfield takes the entire size of the frame. However, when I minimize/maximize the frame and then restore it, they work normally. Here's the code:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class myframe {
public static void main(String s[]) {
JFrame frame = new JFrame("Title");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
// This is an empty content area in the frame
JLabel jlbempty = new JLabel("");
jlbempty.setPreferredSize(new Dimension(500, 400));
frame.getContentPane().add(jlbempty, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
frame.setBounds(0, 0, 500, 400);
JPanel panel = new JPanel();
frame.getContentPane().add(panel, BorderLayout.NORTH);
panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
panel.add(Box.createRigidArea(new Dimension(0,5)));
panel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
JLabel label = new JLabel("My label");
panel.add(label);
JPanel buttonPane = new JPanel();
frame.add(buttonPane);
buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS));
buttonPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
JRadioButton cb = new JRadioButton("1");
buttonPane.add(cb);
JTextField tf = new JTextField(0);
tf.setText("");
buttonPane.add(tf);
JPanel panel3 = new JPanel();
frame.add(panel3);
panel3.setLayout(new BoxLayout(panel3, BoxLayout.LINE_AXIS));
panel3.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
JRadioButton cb2 = new JRadioButton("2");
panel3.add(cb2);
JTextField tf2 = new JTextField(0);
tf.setText("");
panel3.add(tf2);
JPanel panel2 = new JPanel();
JButton button = new JButton("click me");
frame.add(panel2);
panel2.add(button);
button.addActionListener(new Action());
panel.add(buttonPane);
panel.add(panel3);
panel.add(panel2);
}
static class Action implements ActionListener{
public void actionPerformed(ActionEvent arg0) {
JFrame frame2 = new JFrame("Clicked");
frame2.setVisible(true);
frame2.setSize(100, 200);
JLabel label2 = new JLabel("You clicked me");
JPanel panel2 = new JPanel();
frame2.add(panel2);
frame2.add(label2);
}
}
}
In your main method you need to do this at the very end:
frame.pack();
frame.setVisible(true);
You should call frame.pack(); again after adding all the components, so that all the container elements can resize to fit their components best.
http://docs.oracle.com/javase/7/docs/api/java/awt/Window.html#pack()
You should also call frame.SetVisible(true); at the very end, so the form is only displayed ones all components are loaded (otherwise you can see a black box while it loads).
I want to know how to change the content of a JFrame at runtime. Like adding a new JPanel and removing the old JPanel.
You can consider using CardLayout to change the active panel in a frame.
Changing JPanel at runtime here is the Code :
package stack;
import java.awt.BorderLayout;
import java.awt.Dimension;
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;
public class RemoveAndAddPanel implements ActionListener{
JFrame frame;
JPanel firstPanel;
JPanel secondPanel;
JPanel controlPanel;
JButton nextButton;
JPanel panelContainer;
JButton preButton;
JPanel contentPane;
public RemoveAndAddPanel() {
JFrame.setDefaultLookAndFeelDecorated(true);
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
firstPanel = new JPanel();
firstPanel.add(new JLabel("FirstPanel"));
firstPanel.setPreferredSize(new Dimension(100,100));
secondPanel = new JPanel();
secondPanel.add(new JLabel("Second panel"));
secondPanel.setPreferredSize(new Dimension(100,100));
panelContainer = new JPanel();
contentPane = new JPanel(new BorderLayout());
nextButton = new JButton("Next panel");
preButton = new JButton("PreButton");
controlPanel = new JPanel();
nextButton.addActionListener(this);
preButton.addActionListener(this);
preButton.setEnabled(false);
controlPanel.add(preButton);
controlPanel.add(nextButton);
panelContainer.setLayout(new BorderLayout());
panelContainer.add(firstPanel,BorderLayout.CENTER);
contentPane.add(controlPanel, BorderLayout.SOUTH);
contentPane.add(panelContainer,BorderLayout.CENTER);
frame.setContentPane(contentPane);
frame.setVisible(true);
frame.setSize(300,100);
}
#Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == nextButton) {
panelContainer.removeAll();
panelContainer.setSize(0,0);
panelContainer.setSize(secondPanel.getSize());
panelContainer.add(secondPanel,BorderLayout.CENTER);
panelContainer.revalidate();
nextButton.setEnabled(false);
preButton.setEnabled(true);
}
if (e.getSource() == preButton) {
panelContainer.removeAll();
panelContainer.setSize(0,0);
panelContainer.setSize(firstPanel.getSize());
panelContainer.add(firstPanel,BorderLayout.CENTER);
nextButton.setEnabled(true);
preButton.setEnabled(false);
}
}
public static void main(String args[]) {
new RemoveAndAddPanel();
}
}
JFrame.setContentPane()