Removing panel from JTabbedPane via button on panel - java

I have 2 classes, mainFrame and panel. By clicking the button on mainFrame I call panel from another class and set it in tabbed pane which is in JFrame (mainFrame class). Now, I have another button (btnRemove) on my panel in panel class. So when I click that button I want to remove my panel from tabbed pane in mainFrame class. How do I write my listener properly?
mainFrame class:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class MainFrame extends JFrame {
JTabbedPane tPane = new JTabbedPane();
JButton btn = new JButton("Add panel");
public MainFrame(){
setSize(400,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setLayout(new BorderLayout());
add(tPane, BorderLayout.CENTER);
add(btn,BorderLayout.NORTH);
btn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
panel p = new panel();
tPane.add("Panel",p);
}
});
}
public static void main(String[] args){
new MainFrame();
}
}
panel class:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Panel extends JPanel{
JButton btnRemove = new JButton("Remove panel");
public Panel(){
setLayout(new FlowLayout());
add(btnRemove);
btnRemove.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
}
});
}
}

panel.this.getParent().remove(panel.this);
If you want the code to keep working even if you nest the button inside a sub-panel, you should use the follwoing:
SwingUtilities.getAncestorOfClass(JTabbedPane.class, panel.this).remove(panel.this);
Side note: please respect Java naming conventions: classes start with upper-case letters.

Related

Jpanel textbox no output

I'm working in java using Jpanel and my work is compiling fine however is showing no output. hopefully, someone could tell me why this is. I'm using jscrollpane and I'm calling it at the end idk if it's something to do with the listener or what.
FileDemoPanel.java
package Tutoiral03Task01;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class FileDemoPanel extends JPanel implements ActionListener {
public void actionPerformed(ActionEvent e) {
JButton openBtn, saveBtn;
JTextArea workTa;
openBtn = new JButton ("Open");
openBtn.setEnabled (false);
openBtn.setMnemonic('g');
openBtn.setToolTipText("open button");
setLayout(new BorderLayout());
saveBtn = new JButton ("Save");
saveBtn.setEnabled (false);
saveBtn.setMnemonic('f');
saveBtn.setToolTipText("Save button");
JTextArea logTA = new JTextArea (5, 100);
logTA.setEditable(false);
logTA.setBackground(Color.lightGray);
logTA.setMargin(new Insets(5,5,5,5));
JScrollPane logScrollPane = new JScrollPane(logTA);
add(logScrollPane);
}
}
FileDemo.java
package Tutoiral03Task01;
import javax.swing.*;
public class FileDemo {
public static void main (String[] args){
JFrame frame = new JFrame("Working with files");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new FileDemoPanel());
frame.pack();
frame.setVisible(true);
}
}
The problem is that you create all the buttons and others in a actionperformed method.
This is wrong, because that is used as a ButtonListener, so if you dont press a button nothing will happened. We use to write the GUI frame in the constructor of the class.Then we create an object type of the GUI class. So i think i fixed it and i did some extra changes to make the program more simple. The step i didnt do is to add a ButtonListener, so Buttons do nothing.
i wish it will helps you.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class FileDemoPanel extends JFrame {
private JPanel panel = new JPanel();
private JButton openBtn = new JButton("Open");
private JButton saveBtn = new JButton ("Save");
private JTextArea workTa;
public FileDemoPanel(){
openBtn.setEnabled (false);
openBtn.setMnemonic('g');
openBtn.setToolTipText("open button");
setLayout(new BorderLayout());
saveBtn.setEnabled (false);
saveBtn.setMnemonic('f');
saveBtn.setToolTipText("Save button");
JTextArea logTA = new JTextArea (5, 100);
logTA.setEditable(false);
logTA.setBackground(Color.lightGray);
logTA.setMargin(new Insets(5,5,5,5));
JScrollPane logScrollPane = new JScrollPane(logTA);
panel.add(openBtn);
panel.add(saveBtn);
panel.add(logTA);
panel.add(logScrollPane);
this.setContentPane(panel);
this.setVisible(true);
this.setResizable(true);
this.setSize(350,150);
this.setTitle("Κεντρική Σελίδα");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
And the mainclass.As you can see is too small.
public class FileDemo {
public static void main (String[] args){
new FileDemoPanel();
}
}

how to get jpanel components in another jframe class

I have a JFrame call availabilty details. In this frame I have a JPanel and a button. In that JPanel I have two toggle buttons. When I click the button it goes to a new JFrame call reservation.
I need java code for this. That when I select two toogle buttons and press the button, it wants to display the toggle button name and JPanel name in reservation..
here is my availabiltydetails frame.
I want that toogle button name and jpanel name wants to display in reservation frame when I clicked the button.
and here this is the code i already typed in availabilitydetails on add button event
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
Reservation r = new Reservation();
Reservation.bf.getSelectedItems(this.b.getValue());
r.setVisible(true);
this.dispose();
}
is the code is wrong??
The Code Like This:
JFrameT.java:
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class JFrameT extends JFrame {
private JButton testButton;
private JPanel panelMain;
private JPanelOne panel;
public JFrameT() {
// setting up JFrame
setLayout(null);
setPreferredSize(new Dimension(420, 90));
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// creating main JPanel (white)
panelMain = new JPanel();
panelMain.setBackground(Color.WHITE);
panelMain.setBounds(0, 0, 420, 90);
panelMain.setPreferredSize(new Dimension(200, 40));
add(panelMain);
// creating JButton in the main JPanel (white)
testButton = new JButton("Button from main class");
panelMain.add(testButton);
// creating new JPanelOne object from JPanelOne class containing black JPanel
panel = new JPanelOne();
// adding black JPanel to main JPanel (white)
panelMain.add(panel);
pack();
}
public static void main(String[] arguments) {
//Creat JFrame object and setting it visible
JFrameT frame = new JFrameT();
frame.setVisible(true);
}
}
JPanelOne.java:
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
public class JPanelFirst extends JPanel
{
public JPanelFirst()
{
// setting up black JPanel
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(220, 40));
panel.setBackground(Color.BLACK);
// creating button on external JPanel
JButton button = new JButton("Button (+JPanel) from external class");
// adding button to the black JPanel
panel.add(button);
// adding blackJPanel
add(panel);
}
}
You can find it helpful more in this problem.

Refresh JFrame? Java Swing

I don't know how to resolve this case:
I have a JFrame with JPanel on it. I added two JButtons to this JPanel.
Class MainFrame
import java.awt.Color;
import javax.swing.JFrame;
public class MainFrame extends JFrame{
public MainFrame(){
this.setSize(100,100);
MainPanel panel = new MainPanel();
this.add(panel);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
and MainPanel with two buttons
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
public class MainPanel extends JPanel implements ActionListener{
JButton button, example;
public MainPanel(){
this.setLayout(new BorderLayout());
JButton button = new JButton("New");
button.addActionListener(this);
JButton example = new JButton("example");
this.add(button, BorderLayout.NORTH);
this.add(example, BorderLayout.CENTER);
}
#Override
public void actionPerformed(ActionEvent event) {
if (event.getSource().equals(button)){
example.setEnabled(false);
example.setBackground(Color.yellow);
}
}
}
and start class Main
public class Main {
public static void main (String[] args){
MainFrame frame = new MainFrame();
}
}
What should I do to change background color second button?
You have your button variables defined twice, once as an instance variable and once as a local variable.
Get rid of the local variable:
//JButton example = new JButton("example");
example = new JButton("example");
Now your ActionListener code can reference the instance variable.
In your example:
JButton button, example; // <-- Here, you create your two (protected) variables
public MainPanel(){
this.setLayout(new BorderLayout());
JButton button = new JButton("New"); // <-- And here, you create a local variable
button.addActionListener(this);
JButton example = new JButton("example"); // <-- Here, another local variable
this.add(button, BorderLayout.NORTH);
this.add(example, BorderLayout.CENTER);
}
#Override
public void actionPerformed(ActionEvent event) {
if (event.getSource().equals(button)){
example.setEnabled(false);
example.setBackground(Color.yellow);
}
}

How to add a button to a JFrame Gui

I'm trying to add a button to a frame gui.
i tried making a panel and adding it to that, but it does not work.
please help!
here is my code:
import javax.swing.*;
public class Agui extends JFrame {
public Agui() {
setTitle("My Gui");
setSize(400, 400);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JButton button;
JPanel panel;
// my error lines are under the "panel" and "button"
// it says i must implement the variables. what does that mean???
panel.add(button);
}
public static void main(String[] args) {
Agui a = new Agui();
}
}
Change:
JButton button;
JPanel panel;
to:
JButton button = new JButton();
JPanel panel = new JPanel();
You can also pass a String value in JButton() constructor for that string value to be shown on the JButton.
Example: JButton button = new JButton("I am a JButton");
Example Code:
import javax.swing.*;
public class Agui extends JFrame {
public Agui() {
setTitle("My Gui");
setSize(400, 400);
// Create JButton and JPanel
JButton button = new JButton("Click here!");
JPanel panel = new JPanel();
// Add button to JPanel
panel.add(button);
// And JPanel needs to be added to the JFrame itself!
this.getContentPane().add(panel);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
Agui a = new Agui();
}
}
Output:
Note:
Create the JButton and JPanel using new JButton("..."); and new JPanel()
Add the JPanel to the JFrame's content pane using getContentPane().add(...);
If you can Change this Program, You can adjust the button place also
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class agui extends JFrame
{
agui()
{
setTitle("My GUI");
setSize(400,400);
setLayout(null);
JButton button = new JButton("Click Here..!");
button.setBounds(50,100,100,50); /*Distance from left,
Distance from top,length of button, height of button*/
add(button);
setVisible(true);
}
public static void main(String[] args)
{
JFrame agui = new agui();
}
}
Output

setVisible() method not working for JButton placed on JPanel

In my Swing application I have MainFrame with "Add Customer" Button. When I click on the "Add Customer" Button I want Customer form to appear while MainFrame disappearing. Customer form has only JTabbedPane. AddCustomerPanel is a separate class which has only cancel button. AddCustomerPanel has added to Customer form's JTabbedPane as a tab. When I click on cancel button I want Customer frame disappear and mainframe appear again.I tried using setVisible() method. But it didn't work. Please help me to do this.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class MainFrame extends JFrame{
private JButton btnMain;
MainFrame(){
setSize(400,200);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new FlowLayout());
btnMain = new JButton("Add Customer");
btnMain.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
new Customer().setVisible(true);
this.setVisible(false); // Not working
}
});
add(btnMain);
}
public static void main(String args[]){
new MainFrame();
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Customer extends JFrame{
private JTabbedPane tabMain;
Customer(){
setSize(500,300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new FlowLayout());
tabMain = new JTabbedPane();
tabMain.setPreferredSize(new Dimension(490,290));
tabMain.add("Add Customer",new AddCustomerPanel());
add(tabMain);
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class AddCustomerPanel extends JPanel{
private JButton btnCancel;
AddCustomerPanel(){
setSize(400,200);
setVisible(true);
setLayout(new FlowLayout());
btnCancel = new JButton("Cancel");
btnCancel.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
this.setVisible(false); // Not working
new MainFrame().setVisible(true);
}
});
add(btnCancel);
}
}
I found the solution.
SwingUtilities.getWindowAncestor(this).setVisible(false);
new MainFrame().setVisible(true);

Categories

Resources