Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Other Java > Java GUI
|
Java GUI Discussions specific to programming Java GUI.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java GUI section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 22nd, 2003, 09:34 AM
Authorized User
 
Join Date: Aug 2003
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to egolileopablo Send a message via Yahoo to egolileopablo
Default help me add, delete from a JList in a Vector

help me add, delete from a JList in a Vector
try this code and fix how to add to the JList through a vector and delete from
the JList through the vector.
this the code below

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import java.util.*;
public class TabPane extends JFrame {

    JTabbedPane tabpane = new JTabbedPane(JTabbedPane.TOP);
    private Vector vector = new Vector(0);
    //String[] data = {"2D","3B", "12C", "47E", "47F", "Shiva", "Sammuel",
"Daniel", "William", "Horatio", "Biney", "Theodore"};
    //private JList dataList = new JList();//data);
    //Component panel;
    public TabPane() {
      super("Blue Star Airlines");

      Component panel = textPanel("View");
      tabpane.addTab("View", panel);

      Component panel1 = textPane("Add");
      tabpane.addTab("Add", panel1);


      getContentPane().add(tabpane, new BorderLayout().NORTH);



      setSize(450,350);
      setLocation(100, 100);
      setVisible(true);

    }

    protected Component textPanel(String str) {

      getContentPane().setLayout(new GridLayout(1,1));

      JPanel allPanel = new JPanel();

      JPanel listPanel = new JPanel();

      Box box = new Box(BoxLayout.Y_AXIS);

      String[] data = {"2D","3B", "12C", "47E", "47F", "Shiva", "Sammuel",
"Daniel", "William", "Horatio", "Biney", "Theodore"};
      final JList dataList = new JList(data);
      dataList.setVisibleRowCount(4);
      dataList.setSelectionMode(ListSelectionModel.SINGL E_SELECTION);
      dataList.addListSelectionListener(new ListSelectionListener() {
          public void valueChanged(ListSelectionEvent sevent) {
            dataList.getSelectedIndex();
          }
      }
      );
      box.add(dataList);

      JScrollPane scrollPane = new JScrollPane();
      scrollPane.getViewport().setView(dataList);
      box.add(scrollPane);
      listPanel.add(box, new BorderLayout().WEST);


      JPanel pane = new JPanel();
      pane.setLayout(new GridLayout(0,2));

      JLabel slabel = new JLabel("Seat No");
      pane.add(slabel);
      final JTextField sfield = new JTextField(15);
      pane.add(sfield);

      JLabel nlabel = new JLabel("Name");
      pane.add(nlabel);
      final JTextField nfield = new JTextField(15);
      pane.add(nfield);

      JLabel plabel = new JLabel("Price");
      plabel.setHorizontalAlignment(JLabel.LEFT);
      pane.add(plabel);
      final JTextField pfield = new JTextField(15);
      pfield.setHorizontalAlignment(JTextField.RIGHT);
      pane.add(pfield);

      JPanel boxPanel = new JPanel();
      Box cbox = new Box(BoxLayout.X_AXIS);
      JCheckBox check = new JCheckBox("Priority");
      cbox.add(check);

      final JButton abtn = new JButton("Delete");
      abtn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
      if (event.getSource() == abtn)
        vector.removeElement(sfield.getText());
        vector.removeElement(nfield.getText());
        vector.removeElement(pfield.getText());
      JOptionPane.showMessageDialog(null, "Data Deleted");
    }
      });
      cbox.add(abtn);
      boxPanel.add(cbox);


      allPanel.add(listPanel, new BorderLayout().WEST);
      allPanel.add(pane, new BorderLayout().CENTER);
      allPanel.add(boxPanel, new BorderLayout().SOUTH);

      return allPanel;
    }


     protected Component textPane(String str) {
      final JLabel statusLabel = new JLabel();
      statusLabel.setBorder(new BevelBorder(BevelBorder.LOWERED));
      JPanel paneall = new JPanel();

      JPanel dpane = new JPanel();
      dpane.setLayout(new GridLayout(0,2));
      JLabel slabel = new JLabel("Seat No");
      dpane.add(slabel);
      final JTextField sfield = new JTextField(10);
      dpane.add(sfield);

      JLabel nlabel = new JLabel("Name");
      dpane.add(nlabel);
      final JTextField nfield = new JTextField(10);
      dpane.add(nfield);

      JLabel plabel = new JLabel("Price");
      dpane.add(plabel);
      final JTextField pfield = new JTextField(10);
      dpane.add(pfield);

      JPanel pan = new JPanel();

      JPanel checkPanel = new JPanel();
      //checkPanel.setLayout(new GridLayout(0,2));

      Box checkBox = new Box(BoxLayout.Y_AXIS);
      JCheckBox check = new JCheckBox("Business Class");
      checkBox.add(check);
      checkPanel.add(checkBox);
      pan.add(checkPanel, new BorderLayout().WEST);

      JPanel buttonPanel = new JPanel();
      buttonPanel.setBounds(100,50,100,50);
      Box buttonBox = new Box(BoxLayout.Y_AXIS);
      final JButton abtn = new JButton("ADD");
      abtn.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent event) {



            if (event.getSource() == abtn ) {

            vector.addElement(sfield.getText());
            vector.addElement(nfield.getText());
                vector.addElement(pfield.getText());
            //Object ob = vector.set(20, sfield.getText());
            //dataList = (JList)ob;
                sfield.getText();
            nfield.getText();
            pfield.getText();
            sfield.setText( "" );
            nfield.setText( "" );
            pfield.setText( "" );

            JOptionPane.showMessageDialog(null, "Data Stored");

            }

            else if (event.getSource() == abtn ) {
            if(sfield.getText().equals(""))
                  JOptionPane.showMessageDialog(null, "Enter seat number");
            else if (nfield.getText().equals(""))
              JOptionPane.showMessageDialog(null, "Enter seat number");
            else if (pfield.getText().equals(""))
                  JOptionPane.showMessageDialog(null, "Enter seat number");
            }
          }
        }
      );
      buttonBox.add(abtn);

      JButton cbtn = new JButton("Cancel");
      cbtn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
      System.exit(0);
    }
      }
      );

      buttonBox.add(cbtn);
      buttonPanel.add(buttonBox);
      buttonPanel.add(statusLabel, new BorderLayout().SOUTH);
      pan.add(buttonPanel, new BorderLayout().EAST);


      paneall.add(dpane, new BorderLayout().NORTH);
      paneall.add(pan, new BorderLayout().SOUTH);

      return paneall;

    }


    public static void main(String args[]) {
      JPasswordField field = new JPasswordField();
      JOptionPane.showInputDialog(null, "Enter Username:", field.getPassword());
      JOptionPane.showInputDialog(null, "Enter Password:");
      if (field.getText().equals("")) {

        JOptionPane.showInputDialog(null, "Enter Username:",
field.getPassword());
        JOptionPane.showInputDialog(null, "Enter Password:");
      }
      TabPane tab = new TabPane();
      tab.setDefaultCloseOperation(EXIT_ON_CLOSE);

    }
}







Similar Threads
Thread Thread Starter Forum Replies Last Post
XSLT Node add/delete aintvoguish XSLT 2 December 20th, 2007 03:01 AM
vector.add(); -- Reference or Object? Firedawn Servlets 2 February 23rd, 2005 01:04 AM
Edit/Add/Delete within a DataGrid ozzy VB.NET 2002/2003 Basics 5 January 23rd, 2005 01:17 AM
help add, delete from a JList in a Vector egolileopablo J2EE 1 January 1st, 2005 06:16 AM
vector.add(); chikodi Servlets 2 October 23rd, 2003 02:03 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.