Results 1 to 5 of 5
  1. #1
    jian0203 is offline Member
    Join Date
    Jan 2011
    Posts
    1
    Rep Power
    0

    Default How to read data from .txt and update the status of button

    Can anyone help me with this coding ?
    I got no clue on how to implement the function ~
    It's a booking system, when someone click the "Seat 1" or so on. The "Seat 1" should be changed to "BOOKED" but when the constructor run for the second time , the "BOOKED" disappear and reset back to "Seat 1".
    Can anyone help me to solve this ?

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;

    public class FirstClassSeating extends JFrame{
    private static final long serialVersionUID = 1L;
    JButton fcButton[] = new JButton[20];
    JButton returnButton = new JButton("RETURN");

    JLabel titleLabel = new JLabel("First Class Seating");
    JLabel aisleLabel = new JLabel("Aisle");

    JPanel buttonPanelL = new JPanel();
    JPanel buttonPanelR = new JPanel();
    JPanel returnPanel = new JPanel();
    JPanel titlePanel = new JPanel();
    JPanel aislePanel = new JPanel();

    public String str;
    public int number;
    public FirstClassSeating(){
    setLayout(new BorderLayout());
    setPreferredSize(new Dimension(450,450));
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
    setTitle("First Class Seating");

    //aisle

    aisleLabel.setBorder(new javax.swing.border.EmptyBorder(new Insets(0, 0, 0, 0)));
    aislePanel.add(aisleLabel);
    getContentPane().add(aislePanel,BorderLayout.CENTE R);

    //title
    titlePanel.setBorder(new javax.swing.border.EtchedBorder());
    titleLabel.setBorder(new javax.swing.border.EmptyBorder(new Insets(1, 1, 1, 1)));
    titlePanel.add(titleLabel);
    getContentPane().add(titlePanel,BorderLayout.NORTH );

    //button panel
    returnPanel.setLayout(new GridLayout(1,1,4,4));
    returnPanel.setBorder(new javax.swing.border.EtchedBorder());
    buttonPanelL.setLayout(new GridLayout(5,2,4,4));
    buttonPanelL.setBorder(new javax.swing.border.EtchedBorder());
    buttonPanelR.setLayout(new GridLayout(5,2,4,4));
    buttonPanelR.setBorder(new javax.swing.border.EtchedBorder());

    returnPanel.add(returnButton);
    //buttons
    for(int i=0; i<10; i++){
    fcButton[i] = new JButton();
    if(fcButton[i].getText() != "Booked"){
    fcButton[i].setText("Seat"+(i+1));
    buttonPanelL.add(fcButton[i]);
    fcButton[i].addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
    ButtonActionPerformed(e);
    }
    });
    }
    else{
    fcButton[i].setText("Booked");
    buttonPanelL.add(fcButton[i]);
    fcButton[i].addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
    ButtonActionPerformed(e);
    }
    });
    }
    }
    for(int i=10; i<20; i++){
    fcButton[i] = new JButton();
    if(fcButton[i].getText() != "Booked"){
    fcButton[i].setText("Seat"+(i+1));
    buttonPanelR.add(fcButton[i]);
    fcButton[i].addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
    ButtonActionPerformed(e);
    }
    });
    }
    else{
    fcButton[i].setText("Booked");
    buttonPanelL.add(fcButton[i]);
    fcButton[i].addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
    ButtonActionPerformed(e);
    }
    });
    }
    }
    getContentPane().add(buttonPanelL,BorderLayout.WES T);
    getContentPane().add(buttonPanelR,BorderLayout.EAS T);
    getContentPane().add(returnPanel,BorderLayout.SOUT H);

    returnButton.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
    returnButtonActionPerformed(e);
    }
    }
    );

    }

    public void ButtonActionPerformed(ActionEvent e){
    for(int i=0; i<20; i++){
    if(e.getSource() == fcButton[i]){
    str = fcButton[i].getText();
    JOptionPane.showMessageDialog(null,"This is: "+str);

    }
    }

    }

    @SuppressWarnings("deprecation")
    public void returnButtonActionPerformed(ActionEvent e){
    this.hide();
    ChooseClass cc = new ChooseClass();
    cc.showAndDisplayGUI();
    }


    public void showAndDisplayGUI(){
    FirstClassSeating fcs = new FirstClassSeating();
    fcs.pack();
    fcs.setVisible(true);
    }

    public static void main(String a[]){
    FirstClassSeating fcs = new FirstClassSeating();
    fcs.pack();
    fcs.setVisible(true);
    }
    }
    Thanks in advance.
    Last edited by jian0203; 01-20-2011 at 03:26 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Rather than download a whole program, please create a small compilable program that demonstrates your problem (an SSCCE), and then post the code in the forum with code tags. We're all volunteers and appreciate it when posters put in effort to simplify their problem and make it easier for us to solve. Luck.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Also please use proper indentation. I suspect the forum blatted your code before you stuck the tags in.

    I do question this though:
    Java Code:
    public void showAndDisplayGUI() {
        FirstClassSeating fcs = new FirstClassSeating();
        fcs.pack();
        fcs.setVisible(true);
    }

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Tolls View Post
    Also please use proper indentation. I suspect the forum blatted your code before you stuck the tags in.
    The problem is he posted the code in quote tags, not code tags.

    To the OP, edit your original post and change the quote tags:
    [quot&#101;]
    //.... your code is here
    [/quot&#101;]

    to code tags:
    [cod&#101;]
    //.... your code is here
    [/cod&#101;]

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Oh yes.
    I didn't see that was a Quote box and not a Code box.

Similar Threads

  1. Replies: 1
    Last Post: 11-09-2010, 10:28 AM
  2. Update JTable data
    By DC200 in forum AWT / Swing
    Replies: 8
    Last Post: 03-30-2010, 11:10 PM
  3. Replies: 2
    Last Post: 05-11-2009, 11:07 AM
  4. How to update data for a JPA many-to-many relationship?
    By abhijit.sarkar in forum Enterprise JavaBeans (EJB)
    Replies: 1
    Last Post: 11-04-2008, 09:48 AM
  5. Replies: 2
    Last Post: 08-12-2008, 01:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •