Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Adding add/remove button to add/remove tab

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post 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);
    }
    }
    }
    }
    Last edited by JMtrasfiero; March 26th, 2012 at 07:40 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Adding add/remove button to add/remove tab

    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.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Adding add/remove button to add/remove tab

    Quote Originally Posted by Norm View Post
    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
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Adding add/remove button to add/remove tab

    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
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    Junior Member
    Join Date
    Mar 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  7. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Adding add/remove button to add/remove tab

    When will this evaluate to false?

    if(add.equals(add)){
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. The Following User Says Thank You to KevinWorkman For This Useful Post:

    Norm (March 27th, 2012)

Similar Threads

  1. How to remove comma before an String?
    By bhba73 in forum Java Theory & Questions
    Replies: 4
    Last Post: July 28th, 2011, 08:51 PM
  2. How to remove the last comma
    By fride360 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 29th, 2011, 07:20 AM
  3. not able to remove from ArrayList
    By harsha_c in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 3rd, 2011, 03:28 AM
  4. Help Remove file from pad directory
    By georgybaja in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: January 8th, 2011, 05:29 PM
  5. How to remove letters
    By noobish in forum Java Theory & Questions
    Replies: 13
    Last Post: October 3rd, 2009, 10:36 PM