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.
Related
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);
}
});
}
}
I am adding checkboxes on JPanel in FlowLayout the checkboxes are being added horizontally.
I want to add checkboxes vertically on the Panel. What is the possible solution?
I hope what you are trying to achieve is like this. For this please use Box layout.
package com.kcing.kailas.sample.client;
import javax.swing.BoxLayout;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.WindowConstants;
public class Testing extends JFrame {
private JPanel jContentPane = null;
public Testing() {
super();
initialize();
}
private void initialize() {
this.setSize(300, 200);
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
}
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(61, 11, 81, 140);
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
jContentPane.add(panel);
JCheckBox c1 = new JCheckBox("Check1");
panel.add(c1);
c1 = new JCheckBox("Check2");
panel.add(c1);
c1 = new JCheckBox("Check3");
panel.add(c1);
c1 = new JCheckBox("Check4");
panel.add(c1);
}
return jContentPane;
}
public static void main(String[] args) throws Exception {
Testing frame = new Testing();
frame.setVisible(true);
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
}
I used a BoxLayout and set its second parameter as BoxLayout.Y_AXIS and it worked for me:
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
As I stated in comment i would use a box layout for this.
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout());
JButton button = new JButton("Button1");
button.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(button);
button = new JButton("Button2");
button.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(button);
button = new JButton("Button3");
button.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(button);
add(panel);
JPanel testPanel = new JPanel();
testPanel.setLayout(new BoxLayout(testPanel, BoxLayout.Y_AXIS));
/*add variables here and add them to testPanel
e,g`enter code here`
testPanel.add(nameLabel);
testPanel.add(textName);
*/
testPanel.setVisible(true);
I want to put JTabbedPane in the middle,and clicking any tab I want to change will reflect in both above and below panel of tabbedpane.
I tried it but it works only on the below panel.
How to overcome from this problem? Please help me.
Thanks in advance.
Here is my code:
jTabbedPane1 = new javax.swing.JTabbedPane();
jTabbedPane1.addTab("Daily Market", jScrollPane1);
jTabbedPane1.addTab("Weekly Market", jScrollPane2);
On assumption that you want to change something in the panels above and below your tabbed pane. Sample code changing text of a label in top and bottom panel below:
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class TestJTabbedPane extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private void init(){
this.setLayout(new BorderLayout());
JPanel topPanel = new JPanel();
final JLabel topLabel = new JLabel("North");
topPanel.add(topLabel);
this.add(topPanel, BorderLayout.NORTH);
JTabbedPane tabbedPane = new JTabbedPane();
JPanel firstTabCont = new JPanel();
firstTabCont.add(new JLabel("First"));
tabbedPane.addTab("First", firstTabCont);
JPanel secondTabCont = new JPanel();
secondTabCont.add(new JLabel("Second"));
tabbedPane.addTab("Second", secondTabCont);
this.add(tabbedPane, BorderLayout.CENTER);
JPanel bottomPanel = new JPanel();
final JLabel bottomLabel = new JLabel("South");
bottomPanel.add(bottomLabel);
this.add(bottomPanel, BorderLayout.SOUTH);
tabbedPane.addChangeListener(new ChangeListener() {
#Override
public void stateChanged(ChangeEvent evt) {
JTabbedPane pane = (JTabbedPane)evt.getSource();
int selectedIndex = pane.getSelectedIndex();
if(selectedIndex == 0){
topLabel.setText("");
topLabel.setText("Hi");
bottomLabel.setText("");
bottomLabel.setText("Bye");
} else {
topLabel.setText("");
topLabel.setText("Bye");
bottomLabel.setText("");
bottomLabel.setText("Hi");
}
}
});
this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
new TestJTabbedPane().init();
}
}
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()
I have tried to find the answer but I'm coming up short. I am fairly new to java. I have 4 classes (1 main with a JFrame and 3 JPanels).
The main class creates the JFrame and adds the 3 panels to it.
What I am trying to accomplish is update a JLabel or JTextField in 1 panel (panelA) from an ActionEvent in another panel (panelB).
The ActionEvent in panelB runs a method in panelA that runs the setText() method and the repaint() method. I cannot get the JLabel or JTextField to update with the new text.
Here is my code:
App.java
public class App {
public static void main(String[] args) {
JFrame nameFrame = new JFrame("Name Form");
nameFrame.setLayout(new BorderLayout());
nameFrame.setSize(300,150);
nameFrame.setLocationRelativeTo(null);
nameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MiddlePanel middlePanel = new MiddlePanel();
nameFrame.add(middlePanel, BorderLayout.CENTER);
BottomPanel bottomPanel = new BottomPanel();
nameFrame.add(bottomPanel, BorderLayout.SOUTH);
nameFrame.setVisible(true);
}
}
BottomPanel.java
import java.awt.FlowLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class BottomPanel extends JPanel {
private JLabel welcomeLabel = new JLabel("Old Text");
BottomPanel() {
super(new FlowLayout());
add(welcomeLabel);
}
public void setText(String text) {
welcomeLabel.setText(text);
repaint();
}
}
MiddlePanel.java
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JPanel;
import java.awt.event.*;
import java.awt.Graphics;
public class MiddlePanel extends JPanel {
MiddlePanel() {
super(new BorderLayout());
JButton okButton = new JButton("OK");
okButton.setSize(20, 20);
OKButtonListener okButtonListener = new OKButtonListener();
okButton.addActionListener(okButtonListener);
add(okButton, BorderLayout.EAST);
}
class OKButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
BottomPanel bottomPanel = new BottomPanel();
bottomPanel.setText("New Text");
}
}
}
Thanks for any help.
In your OKButtonListener you create a new instance of BottomPanel which has nothing to do with the BottomPanel you added to your JFrame.
You will need the actual reference of the BottomPanel that you added in your JFrame.
d1rk nailed it. Here is one way to achieve that effect.
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.*;
import javax.swing.*;
public class App {
public static void main(String[] args) {
JFrame nameFrame = new JFrame("Name Form");
nameFrame.setLayout(new BorderLayout());
nameFrame.setSize(300,150);
nameFrame.setLocationRelativeTo(null);
nameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BottomPanel bottomPanel = new BottomPanel();
MiddlePanel middlePanel = new MiddlePanel(bottomPanel);
nameFrame.add(middlePanel, BorderLayout.CENTER);
nameFrame.add(bottomPanel, BorderLayout.SOUTH);
nameFrame.setVisible(true);
}
}
class BottomPanel extends JPanel {
private JLabel welcomeLabel = new JLabel("Old Text");
BottomPanel() {
super(new FlowLayout());
add(welcomeLabel);
}
public void setText(String text) {
welcomeLabel.setText(text);
repaint();
}
}
class MiddlePanel extends JPanel {
private BottomPanel bottomPanel;
MiddlePanel(BottomPanel bottomPanel) {
super(new BorderLayout());
this.bottomPanel = bottomPanel;
JButton okButton = new JButton("OK");
okButton.setSize(20, 20);
OKButtonListener okButtonListener = new OKButtonListener();
okButton.addActionListener(okButtonListener);
add(okButton, BorderLayout.EAST);
}
class OKButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
//BottomPanel bottomPanel = new BottomPanel();
bottomPanel.setText("New Text");
}
}
}