Adding add/remove button to add/remove tab
Can anyone help me with this code. I'm a beginner on java programming. I'm using netbeans JFrame Form, I added 2 buttons for my add and remove then a tabbed pane. The buttons doesn't work.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class BulletinBoard extends javax.swing.JFrame {
public class Tab{
public Tab(){
add.addActionListener(new MyAction());
remove.addActionListener(new MyAction());
}
public class MyAction implements ActionListener{
public void actionPerformed(ActionEvent e){
String str = e.getActionCommand();
if(add.equals(str)){
String st = JOptionPane.showInputDialog(null, "Enter Tab Name.");
if(!st.equals("")){
JPanel panel2 = new JPanel();
JLabel label = new JLabel("Your program is working successfully.");
panel2.add(label);
tab.add(st, panel2);
}
}
else if(str.equals(remove)){
tab.remove(tab.getTabCount()-1);
}
}
}
}
/** Creates new form BulletinBoard */
public BulletinBoard() {
initComponents();
Tab ar = new Tab();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
}
public void run() {
new BulletinBoard().setVisible(true);
}
});
}
This is the code that I follow, it is hard coded, but I can't implement it.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class Tab{
JTabbedPane tab;
public static void main(String[] args){
Tab ar = new Tab();
}
public Tab(){
JFrame frame = new JFrame("Add Remove Tab Frame");
tab = new JTabbedPane();
frame.add(tab, BorderLayout.CENTER);
JPanel panel = new JPanel();
JButton button = new JButton("Add Tab");
button.addActionListener(new MyAction());
panel.add(button);
tab.add("Add", panel);
JPanel panel1 = new JPanel();
JButton button1 = new JButton("Remove Tab");
button1.addActionListener(new MyAction());
panel1.add(button1);
tab.add("Remove ", panel1);
frame.setSize(400, 400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
}
public class MyAction implements ActionListener{
public void actionPerformed(ActionEvent e){
String str = e.getActionCommand();
if(str.equals("Add Tab")){
String st = JOptionPane.showInputDialog(null, "Enter Tab Name.");
if(!st.equals("")){
JPanel panel2 = new JPanel();
JLabel label = new JLabel("Your program is working successfully.");
panel2.add(label);
tab.add(st, panel2);
}
}
else if(str.equals("Remove Tab")){
tab.remove(tab.getTabCount()-1);
}
}
}
}
Re: Adding add/remove button to add/remove tab
Quote:
buttons doesn't work.
Please explain.
Does the code compile without errors? If not post the full text of the error messages.
Is the listener method for the buttons called? Add a println first thing in the listener method that prints out the value of the event that is passed to the method to see if the method is called.
Re: Adding add/remove button to add/remove tab
Quote:
Originally Posted by
Norm
Please explain.
Does the code compile without errors? If not post the full text of the error messages.
Is the listener method for the buttons called? Add a println first thing in the listener method that prints out the value of the event that is passed to the method to see if the method is called.
I have done the add button. But the remove button don't work properly, it als. There are no errors, Here's the code. The code for adding/removing tabs is highlighted.
import javax.swing.*;
import java.awt.event.*;
public class Main extends javax.swing.JFrame {
/** Creates new form Main */
public Main() {
initComponents();
add.addActionListener(new MyAction());
remove.addActionListener(new MyAction());
}
public class MyAction implements ActionListener{
public void actionPerformed(ActionEvent e){
if(add.equals(add)){
String st = JOptionPane.showInputDialog(null, "Enter Tab Name.");
if(!st.equals("")){
JPanel panel = new JPanel();
tab.add(st, panel);
}
}
else if(remove.equals(remove)){
tab.remove(tab.getTabCount()-1);
}
}
}
private void removeActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void addActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
Main ar = new Main();
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Main().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton add;
private javax.swing.JButton remove;
private javax.swing.JTabbedPane tab;
// End of variables declaration
}
Re: Adding add/remove button to add/remove tab
Quote:
the remove button don't work properly, it als.
Please explain what "als" means?
Is the listener method for the buttons called? Add a println first thing in the listener method that prints out the value of the event that is passed to the method to see if the method is called.
Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting
Re: Adding add/remove button to add/remove tab
What do you mean by "don't work properly"? Does it not display something? Does it display something you don't want it to? Does it sprout a head and start talking to you?
By the way, this looks like a job for CardLayout. Save yourself the trouble and don't reinvent the wheel.
Re: Adding add/remove button to add/remove tab
The remove button when pressed, it also adds another tab, that it should be to remove. The add button works properly.
Re: Adding add/remove button to add/remove tab
When will this evaluate to false?
if(add.equals(add)){