So, I would like my items to be positioned in the format below; I'm really not confident with positioning but would like to learn it a bit more. Here is the code as of yet:
package com.bleh.harry;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Main
{
private JMenuBar menuBar;
private JMenu fileMenu, windowMenu, helpMenu;
private JMenuItem fileNew, fileOpen, fileSave, windowTheme, windowLayout, windowProperties, helpWelcome, helpHelp, helpAbout;
private JTextArea mainTextArea;
public
Main()
{
JPanel mainCard = new JPanel(new BorderLayout(8,8));
JPanel mainTop = new JPanel(new FlowLayout(FlowLayout.CENTER));
menuBar = new JMenuBar();
fileMenu = new JMenu("File");
windowMenu = new JMenu("Window");
helpMenu = new JMenu("Help");
menuBar.add(fileMenu);
menuBar.add(windowMenu);
menuBar.add(helpMenu);
final CardLayout layout = new CardLayout(); //ADDS CARDS TO CONTAINER
final JPanel cards = new JPanel(layout);
cards.add(mainCard, "2");
mainCard.add(mainTop, BorderLayout.NORTH);
JFrame window = new JFrame("Pseudo code text editor");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().add(cards);
window.setSize(1280, 720);
window.setLocationRelativeTo(null);
window.setVisible(true);
}
public static void main(String args[])
{
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new Main();
}
});
}
}
You could use a BorderLayout...
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
public class LayoutTest {
private JMenuBar menuBar;
private JMenu fileMenu, windowMenu, helpMenu;
private JMenuItem fileNew, fileOpen, fileSave, windowTheme, windowLayout, windowProperties, helpWelcome, helpHelp, helpAbout;
private JTextArea mainTextArea;
public LayoutTest() {
JPanel mainCard = new JPanel(new BorderLayout(8, 8));
menuBar = new JMenuBar();
fileMenu = new JMenu("File");
windowMenu = new JMenu("Window");
helpMenu = new JMenu("Help");
menuBar.add(fileMenu);
menuBar.add(windowMenu);
menuBar.add(helpMenu);
final CardLayout layout = new CardLayout();
final JPanel cards = new JPanel(layout);
cards.add(mainCard, "2");
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.add("One", createPane());
tabbedPane.add("Two", createPane());
tabbedPane.add("Three", createPane());
tabbedPane.add("Four", createPane());
mainTextArea = new JTextArea(20, 40);
mainCard.add(tabbedPane, BorderLayout.WEST);
mainCard.add(new JScrollPane(mainTextArea));
JFrame window = new JFrame("Pseudo code text editor");
window.setJMenuBar(menuBar);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().add(cards);
window.pack();
window.setLocationRelativeTo(null);
window.setVisible(true);
}
protected JPanel createPane() {
return new JPanel() {
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
};
}
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new LayoutTest();
}
});
}
}
The problem here, is the amount of space that the JTabbedPane wants is depended on it's content...
You could even try using a GridBagLayout which might give you a little more control...
import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
public class LayoutTest {
private JMenuBar menuBar;
private JMenu fileMenu, windowMenu, helpMenu;
private JMenuItem fileNew, fileOpen, fileSave, windowTheme, windowLayout, windowProperties, helpWelcome, helpHelp, helpAbout;
private JTextArea mainTextArea;
public LayoutTest() {
JPanel mainCard = new JPanel(new GridBagLayout());
menuBar = new JMenuBar();
fileMenu = new JMenu("File");
windowMenu = new JMenu("Window");
helpMenu = new JMenu("Help");
menuBar.add(fileMenu);
menuBar.add(windowMenu);
menuBar.add(helpMenu);
final CardLayout layout = new CardLayout(); //ADDS CARDS TO CONTAINER
final JPanel cards = new JPanel(layout);
cards.add(mainCard, "2");
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.add("One", createPane());
tabbedPane.add("Two", createPane());
tabbedPane.add("Three", createPane());
tabbedPane.add("Four", createPane());
mainTextArea = new JTextArea(20, 40);
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 0.25;
gbc.weighty = 1;
mainCard.add(tabbedPane, gbc);
gbc.weightx = 0.75;
mainCard.add(new JScrollPane(mainTextArea), gbc);
JFrame window = new JFrame("Pseudo code text editor");
window.setJMenuBar(menuBar);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().add(cards);
window.pack();
window.setLocationRelativeTo(null);
window.setVisible(true);
}
protected JPanel createPane() {
return new JPanel() {
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
};
}
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new LayoutTest();
}
});
}
}
Just remember, the JMenuBar belongs to the window ;)
To lay components out next to each other, I'd recommend using a BoxLayout. A BoxLayout takes an orientation parameter and lays components out accordingly. The two most used options are X_AXIS and Y_AXIS. X_AXIS lays things out left to right while Y_AXIS lays things out top to bottom. You want X_AXIS.
To set the layout with a BoxLayout using an instance of JFrame named window, do:
window.setLayout(new BoxLayout(window, BoxLayout.X_AXIS));
Related
I'm attempting use flow layout to make a menu screen that orients with a single column, but whenever I add a button, it adds it to a single row.
import java.awt.CardLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Proj
{
JPanel card1,card2;
ActionListener listener;
JFrame menu;
JFrame catagories;
JButton menu1,addOrTake,cata,payd,showd;
//JButton
public Proj(){
card2=new JPanel();
menu = new JFrame("Card Layout");
catagories = new JFrame();
//final Container contentPane = menu.getContentPane();
final CardLayout layout = new CardLayout();
menu.setLayout(layout);
card1=new JPanel();
menu1 = new JButton("");
menu1.setIcon(new ImageIcon("C:/Users/sabar/Menu.jpg"));
menu1.setSize(60,600);
menu1.setVisible(true);
addOrTake = new JButton();
addOrTake.setIcon(new ImageIcon("C:/Users/Public/Pictures/Sample Pictures/Penguins.jpg"));
cata = new JButton("");
cata.setIcon(new ImageIcon("C:/Users/Public/Pictures/Sample Pictures/Penguins.jpg"));
showd = new JButton("");
showd.setIcon(new ImageIcon("C:/Users/Public/Pictures/Sample Pictures/Penguins.jpg"));
payd = new JButton("");
payd.setSize(60, 600);
payd.setIcon(new ImageIcon("C:/Users/Public/Pictures/Sample Pictures/Penguins.jpg"));
payd.setVisible(true);
card1.add(menu1);
card1.add(addOrTake);
card1.add(cata);
card1.add(showd);
card1.add(payd);
menu1.addActionListener(listener);
addOrTake.addActionListener(listener);
cata.addActionListener(listener);
showd.addActionListener(listener);
payd.addActionListener(listener);
listener = new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {}};
// TODO Auto-generated method stub
menu.setSize(60, 600);
menu.setVisible(true);
JButton poo=new JButton("Poo");
poo.setSize(60,600);
card2.add((poo));
card2.add(new JButton("Pee"));
card2.add(new JButton("Per"));
card2.add(new JButton("POt"));
card2.setVisible(false);
menu.add(card2);
menu.add(card1);
catagories.pack();
menu.pack();
card1.setVisible(false);
}
public static void main(String[]args)
{
Proj poop =new Proj();
}
}
Problems:
You're adding components to menu, the CardLayout-using container, without use of a 2nd parameter String constant. Without this, it might be hard to easily swap "cards" (components).
If you want a single column type of layout, you need to use a layout manager that allows this, and the JPanel default layout isn't it. Better options:
GridLayout(0, 1) for variable number of rows, one column
BoxLayout(Container, BoxLayout.PAGE_AXIS)
GridBagLayout with the right GridBagConstraint gridx and gridy attributes.
Start by using a layout manager which is capable of achiving your results.
Start by taking a look at Laying Out Components Within a Container, How to Use GridLayout and How to Use GridBagLayout for some ideas.
import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Proj {
JPanel card1, card2;
ActionListener listener;
JButton menu1, addOrTake, cata, payd, showd;
//JButton
JFrame menu;
public Proj() {
menu = new JFrame("Card Layout");
//final Container contentPane = menu.getContentPane();
final CardLayout layout = new CardLayout();
menu.setLayout(layout);
card1 = new JPanel(new GridBagLayout());
card2 = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.fill = GridBagConstraints.HORIZONTAL;
menu1 = new JButton("Mneu");
menu1.setVisible(true);
addOrTake = new JButton("Add or take");
cata = new JButton("Cata");
showd = new JButton("ShowD");
payd = new JButton("payd");
card1.add(menu1, gbc);
card1.add(addOrTake, gbc);
card1.add(cata, gbc);
card1.add(showd, gbc);
card1.add(payd, gbc);
menu1.addActionListener(listener);
addOrTake.addActionListener(listener);
cata.addActionListener(listener);
showd.addActionListener(listener);
payd.addActionListener(listener);
listener = new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
}
};
// TODO Auto-generated method stub
JButton poo = new JButton("Poo");
card2.add((poo), gbc);
card2.add(new JButton("Pee"), gbc);
card2.add(new JButton("Per"), gbc);
card2.add(new JButton("POt"), gbc);
menu.add(card2, "Card2");
menu.add(card1, "Card1");
layout.show(menu.getContentPane(), "Card1");
menu.pack();
menu.setLocationRelativeTo(null);
menu.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
Proj poop = new Proj();
}
});
}
}
I'd also have a closer look at How to Use CardLayout for more details about how it works, because you're using it wrong
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);
}
});
}
}
Basically, i am trying to make it so there are 3 buttons on the bottom of the screen and then have a label which has words in it in the middle of the screen. However, i cant seem to have both buttons and the label in the GUI at the same time. I am a beginner and dont know much about layouts (even though i have read into them) so any help/guidance would be helpful on why i cannot see both the label and the buttons.enter code here
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
public class AWorldPanel implements ActionListener {
/** Declaring all the menu items within the GUI **/
private JMenuItem Fileitem1 = new JMenuItem("New configuration");
private JMenuItem Fileitem2 = new JMenuItem("Open configuration file ");
private JMenuItem Fileitem3 = new JMenuItem("Save");
private JMenuItem Fileitem4 = new JMenuItem("Save As");
private JMenuItem Fileitem5 = new JMenuItem("Exit");
private JMenuItem Viewitem1 = new JMenuItem("Display configuration");
private JMenuItem Viewitem2 = new JMenuItem("Edit configuration");
private JMenuItem Viewitem3 = new JMenuItem("Info about Bugs");
private JMenuItem Viewitem4 = new JMenuItem("Info about Map");
private JMenuItem Edititem1 = new JMenuItem("Remove");
private JMenuItem Edititem2 = new JMenuItem("Add");
private JMenuItem Simulationitem1 = new JMenuItem("Simulation");
private JMenuItem Helpitem1 = new JMenuItem("Info about application");
private JMenuItem Helpitem2 = new JMenuItem("Info about author");
private JLabel theLabel;
private JPanel thePanel;
JButton Run, Pause, Reset;
JFrame GUI = new JFrame("Graphical User Interface");
private static AWorld guiworld;
public AWorldPanel() {
/** Creating the menu **/
JMenuBar menubar = new JMenuBar();
JMenu File = new JMenu("File");
JMenu View = new JMenu("View");
JMenu Edit = new JMenu("Edit");
JMenu Help = new JMenu("Help");
/** welcome label **/
theLabel = new JLabel("Hello ", JLabel.CENTER);
theLabel.setVisible(true);
theLabel.setVerticalTextPosition(JLabel.TOP);
theLabel.setHorizontalTextPosition(JLabel.CENTER);
/** file sub menus **/
menubar.add(File);
File.add(Fileitem1);
File.add(Fileitem2);
File.add(Fileitem3);
File.add(Fileitem4);
File.add(Fileitem5);
menubar.add(View);
View.add(Viewitem1);
View.add(Viewitem2);
View.add(Viewitem3);
View.add(Viewitem4);
menubar.add(Edit);
Edit.add(Edititem1);
Edit.add(Edititem2);
menubar.add(Help);
Help.add(Helpitem1);
Help.add(Helpitem2);
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
panel.setBorder(new EmptyBorder(new Insets(300, 125, 100, 100)));
Run = new JButton("Run");
Pause = new JButton("Pause");
Reset = new JButton("Reset");
panel.add(Run);
panel.add(Box.createRigidArea(new Dimension(0, 5)));
panel.add(Pause);
panel.add(Box.createRigidArea(new Dimension(0, 5)));
panel.add(Reset);
GUI.add(panel);
GUI.add(theLabel);
GUI.setJMenuBar(menubar);
}
private static void createAndShowGUI() {
AWorldPanel newworld = new AWorldPanel();
// Create the container
JFrame frame = new JFrame("Graphical User Interface");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// changing the menu settings
newworld.GUI.setLocation(300, 100);
newworld.GUI.setSize(500, 500);
newworld.GUI.setVisible(true);// Now the frame will appear on screen
}
public static void main(String args[]) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
The default layout manager for a JFrame is a BorderLayout. If you don't specify a constraint then the component will be added to the BorderLayout.CENTER. You can't add multiple components to the same location in the layout. Try something like:
GUI.add(panel, BorderLayout.SOUTH);
GUI.add(theLabel, BorderLayout.CENTER);
Also, learn standard Java naming conventions. Every book, tutorial or example you will read uses these standards so don't make up your own conventions. Variable names do not start with an upper case character.
Edit - #Heuster linked another question that answers this.
I just found out about WindowBuilder and I'm making a simple chat client using it to teach myself. Right now I've got the basic chat frame done, but only some of the components that I've added are accessible in the code. Specifically, I can't access my input JTextArea, taInput. Is there something I need to do to be able to reference it (to get the text in it for sending, etc.)?
Here's a picture of the Design view:
And here's a the generated code:
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.EmptyBorder;
public class frame extends JFrame
{
private JPanel contentPane;
private JButton btnSend;
private JTextArea taDisplay;
/**
* Launch the application.
*/
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try
{
frame frame = new frame();
frame.setVisible(true);
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public frame()
{
setResizable(false);
setTitle("Client");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 440, 316);
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu mnFile = new JMenu("File");
menuBar.add(mnFile);
JMenuItem mntmConnect = new JMenuItem("Connect...");
mnFile.add(mntmConnect);
JMenuItem mntmSaveChatLog = new JMenuItem("Save chat log...");
mnFile.add(mntmSaveChatLog);
JMenuItem mntmSettings = new JMenuItem("Settings...");
mnFile.add(mntmSettings);
JMenuItem mntmClose = new JMenuItem("Close");
mnFile.add(mntmClose);
JMenu mnEdit = new JMenu("Edit");
menuBar.add(mnEdit);
JMenu mnView = new JMenu("View");
menuBar.add(mnView);
JMenu mnHelp = new JMenu("Help");
menuBar.add(mnHelp);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.CENTER);
panel.setLayout(null);
btnSend = new JButton("Send");
btnSend.addMouseListener(new MouseAdapter()
{
#Override
public void mouseClicked(MouseEvent arg0)
{
taDisplay.append("Send clicked.\n");
}
});
btnSend.setBounds(314, 197, 100, 50);
panel.add(btnSend);
taDisplay = new JTextArea();
taDisplay.setLineWrap(true);
taDisplay.setEditable(false);
taDisplay.setBounds(10, 11, 404, 180);
panel.add(taDisplay);
JScrollPane spInput = new JScrollPane();
spInput.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
spInput.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
spInput.setBounds(10, 197, 294, 49);
panel.add(spInput);
JTextArea taInput = new JTextArea();
taInput.setLineWrap(true);
spInput.setViewportView(taInput);
}
}
From the design tab you could right click on the item (taInput) then click rename on the context menu, in the diaog, at the right of the name there where 2 buttons, clic on the (f) button (field) and then ok.
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();
}
}