Close a JtabbedPane with JInternalFrame Inside - java

How can I close a JTabbedPane with an JInternalFrame included when I click a button inside this form?
My project has 3 JForms, 1 TabbedPane, 1 JInternalFrame and 1 Main window (to register data). To create my JTabbedPanes I use the method addTab() and from that form I have the button to close which calls another window (Main registry), I used the dispose() and setVisible(false) methods but they don't do anything.
An example (there are 3 clases -Administrador, -MenuPrincipal, -ventanaRegistro) :
First Class a JFrame and on top a JTabbedPane (this is where I create my tabs)
public class MenuPrincipal extends javax.swing.JFrame
{
Administrador ventanaAdministrador = new Administrador();
void showTabs()
{
JTabbedPane.addTab("Administrador", ventanaAdministrador);
}
public MenuPrincipal()
{
initComponents();
showTabs();
}
}
Second Class a JInternalFrame with a single button
public class Administrador extends javax.swing.JInternalFrame
{
void showAdminWindow()
{
ventanaRegistro ventanaRegistro = new ventanaRegistro();
ventanaRegistro.setVisible(true);
}
void closeThisWindow()
{
this.dispose();
}
public Administrador()
{
initComponents();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
showAdminWindow();
closeThisWindow();
}
}
Third Class a simple JFrame. This is just code to register data.
The real problem is that my method closeThisWindow() doesn't close the second Jtabbed window, and I want to make it that when I click a button (which is on the JTabbedPane) make the third class visible and the Second Class invisible/Close it.

See if the following code does what you expect. If it doesn't please elaborate so I can improve my answer.
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.WindowConstants;
public class MenuPrincipal extends javax.swing.JPanel {
Administrador ventanaAdministrador = new Administrador();
public MenuPrincipal()
{
setPreferredSize(new Dimension(290, 200));
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
ventanaAdministrador.setPreferredSize(new Dimension(350, 100));
tabbedPane.addTab("Admin", ventanaAdministrador);
tabbedPane.addTab("2nd Tab", new JPanel());
add(tabbedPane);
}
/**
* #param args
*/
public static void main(String[] args) {
JFrame testFrame = new JFrame("Main Test Window");
testFrame.setDefaultCloseOperation(WindowConstants.
DISPOSE_ON_CLOSE);
testFrame.setPreferredSize(new Dimension(450, 350));
MenuPrincipal menuP = new MenuPrincipal();
testFrame.getContentPane().add(menuP);
testFrame.pack();
testFrame.setVisible(true);
}
}
And:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
public class Administrador extends javax.swing.JInternalFrame {
void showAdminWindow()
{
JFrame ventanaRegistro = new JFrame("Admin Window");
ventanaRegistro.
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
ventanaRegistro.setPreferredSize(new Dimension(350, 200));
ventanaRegistro.pack();
ventanaRegistro.setVisible(true);
}
void closeThisWindow()
{
Window window = SwingUtilities.getWindowAncestor(this);
window.dispose();
}
public Administrador()
{
JPanel panel = new JPanel();
getContentPane().add(panel, BorderLayout.NORTH);
JButton closeWindowBttn = new JButton("Close Window and open a new one");
closeWindowBttn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
closeWindowActionPerformed();
}
});
panel.add(closeWindowBttn);
}
private void closeWindowActionPerformed() {
showAdminWindow();
closeThisWindow();
}
}

Related

Swing window not opening

I am creating a NotePad app in Java Swing but when I am trying to open a popup to set a title, it is not showing up.
The class that calls the popup:
import java.io.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
public class NewFile implements ActionListener{
public static String title;
public void actionPerformed(ActionEvent e){
PopupFileName popup = new PopupFileName();
/*try{
Thread.sleep(30000);
}catch (InterruptedException o){
o.printStackTrace();
}*/
JTextArea titl = popup.title;
title = titl.getText();
try{
File writer = new File(title+".txt");
if(writer.createNewFile()){
System.out.println("file created");
}else{
System.out.println("file exists");
}
}catch (IOException i) {
System.out.println("An error occurred.");
i.printStackTrace();
}
}
}
The popup class that is supposed to open:
import javax.swing.*;
public class PopupFileName{
static JFrame popup = new JFrame("File Title");
static JLabel titlel = new JLabel("Title:");
static public JTextArea title = new JTextArea();
public static void main(String[] args){
popup.setSize(200,300);
popup.setVisible(true);
popup.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
popup.add(titlel);
popup.add(title);
}
}
Is there any way I can make it visible and make it able to get the text before it is created?
Start by taking a look at:
Creating a GUI With Swing
How to Write an Action Listener
How to Use Scroll Panes
How to Use Buttons, Check Boxes, and Radio Buttons
How to Make Dialogs
You're running in an event driven environment, this means, something happens and then you respond to it.
The problem with your ActionListener is, it's trying to present a window and then, immediately, trying to get some result from it. The problem is, the window probably isn't even present on the screen yet.
What you need is some way to "stop" the code execution until after the user responds. This is where a modal dialog comes in.
import java.awt.BorderLayout;
import java.awt.Component;
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.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
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.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridBagLayout());
JButton btn = new JButton("Test");
btn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
String title = PopupFileName.getTitle(TestPane.this);
System.out.println(title);
}
});
add(btn);
}
}
public static class PopupFileName extends JPanel {
private JLabel titlel = new JLabel("Title:");
private JTextArea title = new JTextArea(20, 40);
public PopupFileName() {
setLayout(new BorderLayout());
add(titlel, BorderLayout.NORTH);
add(new JScrollPane(title));
}
public String getTitle() {
return title.getText();
}
public static String getTitle(Component parent) {
PopupFileName popupFileName = new PopupFileName();
int response = JOptionPane.showConfirmDialog(parent, popupFileName, "Title", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
switch (response) {
case JOptionPane.OK_OPTION:
return popupFileName.getTitle();
default: return null;
}
}
}
}

Searching for commands to replace a panel with another

I am making a graphic program with swing that involves placing various panels with components on the frame. One of them is a panel with buttons that, when an option in an options menu is chosen, is entirely replaced with a different one that has different buttons. We've been thinking and it seems that the best way to do this would be to, each time the option is selected, to rebuild the frame from scratch.
This is the snippet of code in which I'm building the main panels:
import java.awt.Color;
import java.awt.Dimension;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
import com.jtattoo.plaf.graphite.GraphiteLookAndFeel;
import com.nacher.calc.Main;
import com.nacher.calc.controller.Controller;
import net.miginfocom.swing.MigLayout;
/**
*
* #author chema && Sergio
* This Panel contains buttons and textbox to perform the calculator actions
*/
public class PanelCalculator extends JPanel implements PropertyChangeListener {
private static final long serialVersionUID = -8377215712645516042L;
private PanelNumbers panelNumbers;
private PanelScreen panelScreen;
private PanelOptions panelOptions;
private PanelScientific panelScientific;
public Controller controller;
public PanelCalculator(Controller controller) {
this.controller = controller;
//Initialize JTattoo Library
try{
UIManager.setLookAndFeel(new GraphiteLookAndFeel());
}catch(UnsupportedLookAndFeelException ulafe){
System.out.println("JTattoo Graphite failed to set");
}
this.setLayout(new MigLayout("wrap"));
buildComponents();
situateComponents();
//controller = new Controller();
controller.addPropertyChangeListener(panelScreen);//panelScreen escucha al controller
panelOptions.addPropertyChangeListener(controller);//controller escucha al panelOptions
panelNumbers.addPropertyChangeListener(controller);//controller escucha al panelNumbers
panelScreen.addPropertyChangeListener(controller);//controller escucha al panelScreen
}
private void buildComponents(){
this.setPreferredSize(new Dimension(210, 280));
this.setBorder(new EmptyBorder(0, 0, 0, 0));
panelOptions = new PanelOptions();
panelOptions.setBorder(new EmptyBorder(0, 0, 0, 0));
panelOptions.setPreferredSize(new Dimension(200, 50));
panelScreen = new PanelScreen();
panelScreen.setBorder(BorderFactory.createEtchedBorder());
panelScreen.setPreferredSize(new Dimension(200, 40));
panelScreen.setBackground(new Color(255, 255, 255));
panelNumbers = new PanelNumbers();
panelNumbers.setBorder(new EmptyBorder(0, 0, 0, 0));
panelNumbers.setPreferredSize(new Dimension(200, 185));
panelOptions.addPropertyChangeListener(panelScreen);//panelScreen escucha al panelOptions
panelNumbers.addPropertyChangeListener(panelScreen);//panelScreen escucha al panelNumbers
}
private void situateComponents(){
this.add(panelOptions, "span");
this.add(panelScreen, "span");
this.add(panelNumbers, "span" );
}
/**
* To build the Scientific Calculator
*/
private void buildScientific() {
//Instantiate the panelScientific
panelScientific = new PanelScientific();
panelScientific.setBorder(new EmptyBorder(0, 0, 0, 0));
panelScientific.setPreferredSize(new Dimension(200, 185));
//Reordering the Panel Calculator
}
#Override
public void propertyChange(PropertyChangeEvent evt) {
String c = evt.getPropertyName();
if (c.equals(Constants.SET_CALC_SCIENTIFIC)) {
System.out.println("Set the Scientific Calculator");
buildScientific();
}
}
}
And this is the main class where we build the frame:
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import com.jtattoo.plaf.bernstein.BernsteinLookAndFeel;
import com.jtattoo.plaf.fast.FastLookAndFeel;
import com.jtattoo.plaf.graphite.GraphiteLookAndFeel;
import com.jtattoo.plaf.hifi.HiFiLookAndFeel;
import com.jtattoo.plaf.luna.LunaLookAndFeel;
import com.nacher.calc.controller.Controller;
import com.nacher.calc.ui.Constants;
import com.nacher.calc.ui.ImageConstants;
import com.nacher.calc.ui.PanelCalculator;
public class Main extends JFrame implements PropertyChangeListener {
private static final long serialVersionUID = -4136829057174783241L;
private static Main mySelf;
private static PanelCalculator panelCalc;
public static Controller controller;
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
controller = new Controller();
panelCalc = new PanelCalculator(controller);
controller.addPropertyChangeListener(panelCalc);
mySelf = new Main();
try {
mySelf.setTitle("Calculator");
mySelf.setLocationRelativeTo(null);
mySelf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mySelf.setIconImage(new ImageIcon(mySelf.getClass().getResource(ImageConstants.CALC_IMAGE_THREE)).getImage());
mySelf.getContentPane().add(panelCalc);
mySelf.setResizable(false);
mySelf.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
System.out.println("Quitting the application.");
}
});
mySelf.pack();
mySelf.setVisible(true);
controller.addPropertyChangeListener(mySelf);
}catch(Exception ex) {
System.out.println("The application could not run.");
System.out.println(ex.getMessage());
System.exit(-1);
}
}
What commands should I use to 'unbuild' the frame, then rebuild it from scratch? Is there a more efficient way to replace one panel with another?
The Card Layout was designed for this type of functionality. It allows you to define multiple panels to share the same space of the frame. You then just specify which panel is currently displayed.
Read the section from the Swing tutorial on How to Use CardLayout for more information and working examples.
I don't know if I totally get your problem, but if, that would be my solution (just a simple example):
public class Main extends JFrame{
public Main(){
setTitle("Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
Panels panels = new Panels();
add(panels, BorderLayout.CENTER);
setLocationRelativeTo(null);
pack();
setVisible(true);
}
}
.
public class Panels extends JPanel{
public JPanel p1, p2;
public Panels(){
p1 = new JPanel();
add(p1);
p2 = new JPanel();
add(p2);
//p1 and p2 might be more complex Components
//place them using your preferred LayoutManager
//(set their preferredSize etc.)
}
public void updateP1(JPanel np1){
remove(p1);
p1 = np1;
add(p1);
}
public void updateP2(JPanel np2){
remove(p2);
p2 = np2;
add(p2);
}
}
I think what you're looking for is the method remove(Component comp) of a JComponent, i.e. a JPanel. Hope that helps.

How could I make the colour BLACK be transparent on top of the uploaded image?

Right now all I have is created the black background and uploaded my image I wanted. I want to make it so the image is behind a transparent background. I initially had this all in separate class files but put them all in one main class
package Flashlight;
import java.awt.Color;
import java.awt.GridLayout;
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.JLabel;
import javax.swing.JPanel;
public class Flashlight3 extends JFrame {
public class FlashLabelChange extends JPanel {
Flashlight.FlashDisp flashDisp;
public FlashLabelChange(Flashlight.FlashDisp _flashDisp) {
flashDisp = _flashDisp;
JButton btn1 = new JButton("Start");
add(btn1);
class Button implements ActionListener {
public void actionPerformed(ActionEvent event) {
if (event.getActionCommand().equals("Start")) {
flashDisp.UpdateLabel("Start");
}
}
}
ActionListener button = new Button();
btn1.addActionListener(button);
}
}
public class FlashDisp extends JPanel {
private JLabel lblName;
private String sLabel;
private JLabel lblImage;
public FlashDisp() {
lblImage = new JLabel();
add(lblImage);
lblName = new JLabel("Start?");
add(lblName); //add it to the Frame
}
void UpdateLabel(String _sNew) {
sLabel = _sNew;
lblName.setText(sLabel);
}
void UpdateBackground(String _sNew) {
sLabel = _sNew;
if (sLabel == ("Black")) {
setBackground(Color.black);
lblImage.setIcon(new ImageIcon("Hallway.png"));
}
}
}
public class FlashColour extends JPanel {
FlashDisp flashDisp;
public FlashColour(FlashDisp _flashDisp) {
flashDisp = _flashDisp;
setLayout(new GridLayout(3, 1));
JButton btnDark = new JButton("Dark");
add(btnDark);
class ColourChangeListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
if (event.getActionCommand().equals("Dark")) {
flashDisp.UpdateBackground("Black");
}
}
}
ActionListener colourChangeListener = new ColourChangeListener();
btnDark.addActionListener(colourChangeListener);
}
}
public static void main(String[] args) {
JFrame frame = new JFrame();
FlashMain flashMain = new FlashMain();
frame.setSize(400, 400);
frame.setTitle("FLAAAASH LIGHT");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(flashMain);
frame.setVisible(true);
}
}
I think that you need to use the opaque method.
Have a look here
setOpaque(true/false); Java for using opaque.
Hope that helps.

Java Swings Select Tab

I have 4 JPanels. In one of the panel I have a combo Box.Upon selecting "Value A" in combo box Panel2 should be displayed.Similarly if I select "Value B" Panel3 should be selected....
Though action Listener should be used in this context.How to make a call to another tab with in that action listener.
public class SearchComponent
{
....
.
public SearchAddComponent(....)
{
panel = addDropDown(panelList(), "panel", gridbag, h6Box);
panel.addComponentListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ItemSelectable is = (ItemSelectable)actionEvent.getSource();
Object name=selectedString(is);
}
});
}
public static final Vector<String> panelList(){
List<String> panelList = new ArrayList<String>();
panelList.add("A");
panelList.add("B");
panelList.add("C");
panelList.add("D");
panelList.add("E");
panelList.add("F);
Vector<String> panelVector = null;
Collections.copy(panelVector, panelList);
return panelVector;
}
public Object selectedString(ItemSelectable is) {
Object selected[] = is.getSelectedObjects();
return ((selected.length == 0) ? "null" : (ComboItem)selected[0]);
}
}
Use a Card Layout. See the Swing tutorial on How to Use a Card Layout for a working example.
Try This code:
import java.awt.EventQueue;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import javax.swing.BorderFactory;
import javax.swing.border.Border;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.Container;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class CardLayoutExample {
JFrame guiFrame;
CardLayout cards;
JPanel cardPanel;
public static void main(String[] args) {
//Use the event dispatch thread for Swing components
EventQueue.invokeLater(new Runnable()
{
#Override
public void run()
{
new CardLayoutExample();
}
});
}
public CardLayoutExample()
{
guiFrame = new JFrame();
//make sure the program exits when the frame closes
guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
guiFrame.setTitle("CardLayout Example");
guiFrame.setSize(400,300);
//This will center the JFrame in the middle of the screen
guiFrame.setLocationRelativeTo(null);
guiFrame.setLayout(new BorderLayout());
//creating a border to highlight the JPanel areas
Border outline = BorderFactory.createLineBorder(Color.black);
JPanel tabsPanel = new JPanel();
tabsPanel.setBorder(outline);
JButton switchCards = new JButton("Switch Card");
switchCards.setActionCommand("Switch Card");
switchCards.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent event)
{
cards.next(cardPanel);
}
});
tabsPanel.add(switchCards);
guiFrame.add(tabsPanel,BorderLayout.NORTH);
cards = new CardLayout();
cardPanel = new JPanel();
cardPanel.setLayout(cards);
cards.show(cardPanel, "Fruits");
JPanel firstCard = new JPanel();
firstCard.setBackground(Color.GREEN);
addButton(firstCard, "APPLES");
addButton(firstCard, "ORANGES");
addButton(firstCard, "BANANAS");
JPanel secondCard = new JPanel();
secondCard.setBackground(Color.BLUE);
addButton(secondCard, "LEEKS");
addButton(secondCard, "TOMATOES");
addButton(secondCard, "PEAS");
cardPanel.add(firstCard, "Fruits");
cardPanel.add(secondCard, "Veggies");
guiFrame.add(tabsPanel,BorderLayout.NORTH);
guiFrame.add(cardPanel,BorderLayout.CENTER);
guiFrame.setVisible(true);
}
//All the buttons are following the same pattern
//so create them all in one place.
private void addButton(Container parent, String name)
{
JButton but = new JButton(name);
but.setActionCommand(name);
parent.add(but);
}
}

JPopupMenu.show shows popupmenu with strange behaviour (sometimes appears as grey box)

I have a JFrame containing a JTabbedPane containing a JPanel in a Tab.
In this JPanel, I want a JPopupMenu to show at Mouse Position when clicking the right mouse button.
To do this, I use the show(invoker, x, y) method.
My Problem: The JPopupMenu has a very strange behaviour; sometimes it displays without containing everything (just a grey box) and sometimes it displays in the top left corner of the Panel, behaving completely as expected.
Code:
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
class Testframe extends JFrame {
public static JFrame frame;
private static final long serialVersionUID = 1L;
public Testframe(String string) {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle(string);
setSize(200,200);
setVisible(true);
}
public static void main(String[] args) {
frame = new Testframe("Title");
JTabbedPane tabpane = new JTabbedPane(JTabbedPane.TOP);
tabpane.addTab("title", new TestPanel());
frame.add(tabpane);
tabpane.setVisible(true);
}
}
import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;
public class TestPanel extends JPanel implements MouseListener {
private static final long serialVersionUID = 1L;
JPopupMenu activeDropdown;
TestPanel() {
setBackground(Color.GREEN);
setVisible(true);
addMouseListener(this);
}
private void dropdown(MouseEvent e) {
activeDropdown = new JPopupMenu();
JMenuItem item = new JMenuItem("Eintrag 0");
activeDropdown.add(item);
activeDropdown.show(Testframe.frame, e.getX(), e.getY());
this.add(activeDropdown);
}
#Override
public void mouseClicked(MouseEvent e) {
if(SwingUtilities.isRightMouseButton(e)) {
if (activeDropdown != null)
this.remove(activeDropdown);
dropdown(e);
}
}
#Override
public void mouseEntered(MouseEvent e) {}
#Override
public void mouseExited(MouseEvent e) {}
#Override
public void mousePressed(MouseEvent e) {}
#Override
public void mouseReleased(MouseEvent e) {}
}
If I try to put the JTabbedPane into a separate Class, the JPopupMenu appears anywhere (seems to be a fixed position) on the screen, completely independent from the window position.
Change your dropdown method as below. That should work as expected.
private void dropdown(MouseEvent e) {
activeDropdown = new JPopupMenu();
JMenuItem item = new JMenuItem("Eintrag 0");
activeDropdown.add(item);
this.add(activeDropdown);
activeDropdown.show(this, e.getX(), e.getY());
}
However, I don't understand why you are removing the existing JPopMenu and adding a new one on every right-mouse click.
You can simply use JComponent.setComponentPopupMenu to handle righ-click popup-menus. This is much simpler and will handle all the wiring code for you.
Small example with your code:
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;
public class TestPanel extends JPanel {
private static final long serialVersionUID = 1L;
JPopupMenu activeDropdown;
TestPanel() {
setBackground(Color.GREEN);
activeDropdown = new JPopupMenu();
JMenuItem item = new JMenuItem("Eintrag 0");
activeDropdown.add(item);
setComponentPopupMenu(activeDropdown);
}
protected void initUI() {
JFrame frame = new JFrame("Title");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTabbedPane tabpane = new JTabbedPane(JTabbedPane.TOP);
tabpane.addTab("title", this);
frame.add(tabpane);
frame.setSize(200, 200);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new TestPanel().initUI();
}
});
}
}
NB: Avoid using static variables!

Categories