Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Help with JApplet

Name: Anonymous 2008-12-19 18:04

I need to write an event driven JApplet, with 15 labeled buttons. The buttons say nearly identical things, with the exception of a number. I do not want to write them individually, and I need to know a way I can group them in a JApplet.

Essentially I need a way I can just write one string such as:
buttons [i] = new JButton ("Seat " + (i+1) + " is open.");
and have it label all the buttons.

I have an hour to figure out how to do this.
Help is greatly appreciated.

Name: Anonymous 2008-12-19 18:05

Do your own homework.

Name: Anonymous 2008-12-19 18:08

You know about JApplet and events, but not about loops?

Name: Anonymous 2008-12-19 18:09

I have... I just need help with this one piece of code. I can't figure out how to group the buttons under one label. and I can't get the code I've written to work if the buttons are labeled individually.

Name: Anonymous 2008-12-19 18:11

No... I'm Java Class. I needed to convert a program I've already written in a Java Class to a JApplet. I've done most of it already, I'm just stuck on grouping the buttons.

Name: Anonymous 2008-12-19 18:11

*more familiar with Java Class

Name: Anonymous 2008-12-19 18:14

JAPPLES

Name: Anonymous 2008-12-19 18:15

>>5
No... I'm Java Class
And then anon was an object

Name: Anonymous 2008-12-19 18:18

Why does every Java thing have to have a J in it?
We already know it's Java, just call the fucking thing "Button".

Name: Anonymous 2008-12-19 18:21

>>9
iI iDon't JKnow. PHPIt's iJust JA JStupid PyFad.

Name: Anonymous 2008-12-19 18:30

>>9
Exactly. You don't see a LispButton or a HaskellButton, why is there a JavaButton?

Name: Anonymous 2008-12-19 19:08

>>11
I saw a LispButton in Des Moines the other day. He was walking along the street.

Name: Anonymous 2008-12-19 19:14

>>9
AWT already has a Button. JButton is the Swing button.
It's just a convention so people can keep straight which toolset they're working with, since it's impossible to import just one of them, as Sun sucks massive ass at abstraction and library design.

Name: Anonymous 2008-12-19 19:31

>>13
import qualified javax.swing as Swing
import qualified java.awt as Awt

button1 <- Swing.button "Swing button"
button2 <- Awt.button "Awt button"


???

Name: Anonymous 2008-12-19 19:53

>>14
button1 = SwingComponentFactory.createComponent(Swing.button,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL)

Name: Anonymous 2008-12-19 20:01


package org.fourchan.dis.prog.t1229727637.p16;

public class Java101 extends Object {
  public static final int BUTTONS = 15;

  public static void main(String[] argv) {
    /* FRAME SECTION. */
    JFrame frame = new JFrame();
    frame.setSize(800, 600);

    /* CUNT SECTION. */
    Container cunt = frame.getContentPane();
    cunt.setLayout(new GridLayout(2, 1));

    /* LABEL SECTION. */
    JLabel label = new JLabel("fag");
    cunt.add(label, BorderLayout.NORTH);

    /* PANEL SECTION. */
    JPanel buttonPanel = new JPanel();
    cunt.add(buttonPanel, BorderLayout.CENTER);

    /* BUTTON SECTION. */
    JButton[] buttons = new JButton[NUM_BUTTONS];
    for (int i=0; i < buttons.length; i++) {
      buttons[i] = new JButton("Seat " + (i+1) + " is open");
      buttons[i].add(new );
      cunt.add(buttons[i]);
    }

    /* SETUP SECTION. */
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
  }
}

Name: Anonymous 2008-12-19 20:05

>>15
java.lang.NullPointerException

Name: Anonymous 2008-12-19 20:29

>>15
[code]try {
  >>15;
} catch (NullPointerException e) {}

Name: Anonymous 2008-12-19 20:32

>>16

Thank you, that helps a lot.

Name: Anonymous 2008-12-20 0:56

I have another question.

I need to know how I would write
(buttons [i] == e.getSource ()

as an e.ActionCommand, such as
(e.getActionCommand( ).equals("Exit"))

I've tried writing it as
(e.getActionCommand( ).equals(buttons [i])
and while it doesn't bring back any errors, it doesn't do what I need it to.

I'm trying to write an if-else statement for the buttons that brings up a dialog box such as
JOptionPane.YES_NO_OPTION
I know how to bring up the dialog box as long as I can find a way to call the buttons [i] into the statement.

It doesn't have to be an e.ActionCommand, it just needs to be able to call buttons [i] into the statement.

I know these seem like dumb questions... I just really suck at JApplets.

Any help is greatly appreciated.

Name: Anonymous 2008-12-20 1:02

>>20
**(e.getActionCommand( ).equals(buttons [i]))

Missing parenthesis was a typo, it's not missing in the actual code.

Name: Anonymous 2008-12-20 1:07

Here's the section of code I need it for:

    public void actionPerformed(ActionEvent e) {
 
        if (e.getActionCommand( ).equals("Exit")){
            int choice = JOptionPane.showConfirmDialog(null, "Do you want to exit?", "Confirm", JOptionPane.YES_NO_CANCEL_OPTION);
            if(choice == JOptionPane.YES_OPTION){
                createFile();
                System.exit(0);
            }
        }
       
        else {
               
                for(int i=0; i<buttons.length; i++){
                    if (buttons [i] == e.getSource ()) {
                   
                    if(buttons[i].getText().equals("Occupied")){
                        int choice = JOptionPane.showConfirmDialog(null, "This seat is occupied - do you want to unbook this seat?", "Confirm", JOptionPane.YES_NO_OPTION);
                        if(choice == JOptionPane.YES_OPTION){
                            String code = JOptionPane.showInputDialog(null,"Enter code to unbook");
                            if(code.equals("7777")){
                                buttons[i].setText("Seat " +(i+1)+" is open.");
                                seatCounter--;
                            }
                        }
                    }
                    else{
                        int choice = JOptionPane.showConfirmDialog(null, "Do you want to book this seat?", "Confirm", JOptionPane.YES_NO_CANCEL_OPTION);
                        if(choice == JOptionPane.YES_OPTION){

                            buttons[i].setText("Occupied");
                            seatCounter++;
                            SeatsFilled();
                        }
                    }
                }
       
            }
        }}

Name: Anonymous 2008-12-20 1:13

You're allowed to read the documentation. I realize this is hard to understand for many Java ``developers''.

Name: Anonymous 2008-12-20 1:16

>>20
wtf

Name: Anonymous 2008-12-20 1:31

>>24

Yeah, sorry if that sounds weird.

I just need something to call in buttons [i] to the if-else statement of this code:

>>22

The code was originally written for a Java Class and now I'm converting it to a JApplet.
I don't know if I would call in buttons [i] with some form of e.getActionCommand or not. I just assumed it would be because that's how the other buttons are called upon.

Name: Anonymous 2008-12-20 2:30

>>25
What the hell is a Java Class already?  Do you believe your new code somehow isn't in a class?

Name: Anonymous 2008-12-20 2:39

http://2003.gelaed.com/resources/java/ButtonJApplet/index5.html

BITCH NEEDS TO LEARN TO GOOGLE FUCK, ITS JUST THE BUTTONS LABEL

button1 = new JButton("Disable Button 2");

if (e.getActionCommand() == "Disable Button 2") {
do shit
}

Name: Anonymous 2008-12-20 3:13

>>27

I need to know how to write it with what the button's called in the program, not what it's "named" when you run the JApplet.

That means if I was writing with the program you linked to I would need to be able to write it with button1
EX: e.getActionCommand() == button1
not "Disable Button 2". The programed you linked to doesn't help with that.

The problem with my buttons [i] is that the "name" it's under changes, so I can't just write it down in the program like the other e.getActionCommands I have listed. I mean I can, it doesn't bring back any errors, but it doesn't work either.

>>26

Java Class = public class buttons
JApplet = pubic class buttons extends JApplet

You're right, they're pretty much the same thing. There's a few minute differences about how you call in the panels and frames and buttons and such, but that's about it.

Name: Anonymous 2008-12-20 3:47

>>26
CompSci 101 is often a Java Class

Name: Anonymous 2008-12-20 4:22

>>29

Heh, I was waiting for someone to bring that up...

Name: Anonymous 2008-12-20 5:10

>>16

I wanted to say thanks again. You're the only person on any of the boards/forums/sites I posted on that was able to provide me with anything that actually helped.

Name: Anonymous 2008-12-20 5:54

>>31
Learn to do you own fucking homework, you fucking leech.

Name: Anonymous 2008-12-20 6:52

>>32

For the program I wrote the only thing I asked for--and received--help with was:

    for (int i=0; i < buttons.length; i++) {
      buttons[i] = new JButton("Seat " + (i+1) + " is open");
      buttonPanel.add(buttons[i]);

I did also ask for help with:

if (buttons [i] == e.getSource ())

but ended up figuring out why it wasn't working shortly after posting the question.

Yep, those two pieces of code sure did make up the bulk of my program. You're right, I am a complete leech.

Learn how to use possessive pronouns.


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
import javax.swing.JButton;
import java.io.*;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class buttons extends JApplet
implements ActionListener{   
    private int seatCounter = 0;
    private static final int MAX_NUMBER_SEATS = 15;
    JButton[] buttons = new JButton[MAX_NUMBER_SEATS];
    JButton exitButton = new JButton("Exit"); 
    private JLabel statusLabel1 = new JLabel("Welcome!");
    private JLabel statusLabel2 = new JLabel("Click seat number to book.");
    private JLabel statusLabel3 = new JLabel("Click the exit button to exit program. Seat selection will be saved.");
    private JLabel statusLabel4 = new JLabel ("To unbook a seat you have reserved click the occupied seat and enter the code when prompted.");
    JPanel buttonPanel = new JPanel ( );
    JPanel exitPanel = new JPanel ( );
    JPanel instructionPanel = new JPanel ( );
    Container contentPane = getContentPane();
    JFrame frame = new JFrame ( );
 
 

    {       

        contentPane.setLayout(new BorderLayout( ));
        contentPane.add(buttonPanel, BorderLayout.CENTER);
        contentPane.add(exitPanel, BorderLayout.SOUTH); 
        contentPane.add(instructionPanel, BorderLayout.NORTH);
       
        buttonPanel.setBackground(Color.GRAY);
        buttonPanel.setLayout(new GridLayout(5,3)); 
       
        exitPanel.setBackground(Color.GRAY);
        exitPanel.setLayout(new FlowLayout( ));  
       
     
        instructionPanel.setBackground(Color.GRAY);
        instructionPanel.setLayout(new FlowLayout( ));
        instructionPanel.setLayout(new BoxLayout(instructionPanel,BoxLayout.Y_AXIS));     
       
        instructionPanel.add(statusLabel1);
        instructionPanel.add(statusLabel2);
        instructionPanel.add(statusLabel3);
        instructionPanel.add(statusLabel4);       
         
       
        for(int i=0; i<buttons.length; i++)
        {
          buttons [i] = new JButton("Seat " + (i+1) + " is open.");
          buttons [i].addActionListener ((ActionListener) this);         
          buttonPanel.add(buttons [i]); 
        }
       
        File file = new File("C:/file.txt");
       
        if (file.exists()){
            readDataFromFile();
        }       

        exitButton.addActionListener(this);
        exitPanel.add(exitButton);     
       
        SeatsFilled();
       
        }


    public void actionPerformed(ActionEvent e) {
 
        if (e.getActionCommand( ).equals("Exit")){
            int choice = JOptionPane.showConfirmDialog(null, "Do you want to exit?", "Confirm", JOptionPane.YES_NO_CANCEL_OPTION);
            if(choice == JOptionPane.YES_OPTION){
                createFile();
                System.exit(0);
            }
        }
       
        else {
               
                for(int i=0; i<buttons.length; i++){
                    if (buttons [i] == e.getSource ()) {
                   
                    if(buttons [i].getActionCommand().equals("Occupied")){
                        int choice = JOptionPane.showConfirmDialog(null, "This seat is occupied - do you want to unbook this seat?", "Confirm", JOptionPane.YES_NO_CANCEL_OPTION);
                        if(choice == JOptionPane.YES_OPTION){
                            String code = JOptionPane.showInputDialog(null,"Enter code to unbook");
                            if(code.equals("7777")){
                                buttons [i].setText("Seat " +(i+1)+" is open.");
                                seatCounter--;
                            }
                        }
                    }
                    else{
                        int choice = JOptionPane.showConfirmDialog(null, "Do you want to book this seat?", "Confirm", JOptionPane.YES_NO_OPTION);
                        if(choice == JOptionPane.YES_OPTION){

                            buttons[i].setText("Occupied");
                            seatCounter++;
                            SeatsFilled();
                        }
                    }
                }
       
            }
        }}
       
        public void createAndShowGUI () {
        frame = new JFrame ("buttons");
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        frame.setLayout (new GridLayout (3, 1));
        for (JButton button : buttons) {
            buttonPanel.add (button);
        }
        frame.add(instructionPanel);
        frame.add(buttonPanel);
        frame.add(exitPanel);
        frame.pack ();
        frame.setVisible (true);
    }
   
            public void createFile() {
            try
            {
                PrintWriter writer = new PrintWriter("c:/file.txt");
                for(int i=0; i<16; i++){
                    if(i==15){
                     writer.println("" + seatCounter);
                   
                }
                else{
                    writer.println(buttons[i].getText());
                }
            }
            writer.close();
        }
            catch (IOException e)
            {
                System.out.println("File not created or written correctly");
              
            }
        }           

        public void readDataFromFile()
            {
            try
            {
                BufferedReader reader = new BufferedReader (new FileReader("c:/file.txt"));
                for (int i=0; i<16; i++){
                    if(i==15){
                seatCounter = Integer.parseInt(reader.readLine());
                    }
                    else{
                        buttons[i].setText(reader.readLine());
                    }
                }
                reader.close();
            }
            catch(FileNotFoundException e)
            {
                System.out.println("File not founnd at c:/file.txt");
               
            }
            catch(IOException e)
            {
                System.out.println("Something went wrong with reading or closing the file.");
            }
}
    private void SeatsFilled(){
            if(seatCounter>=15){
                JOptionPane.showMessageDialog(null, "All seats are booked.");
          
            }
        }
}

Name: Anonymous 2008-12-20 12:13

          buttons [i].addActionListener ((ActionListener) this);
(ActionListener) this

Name: Anonymous 2008-12-20 12:30

>>33
Just shut up and go away, you failure.

Name: Anonymous 2008-12-20 13:53

>>34

Haha yeah, I forgot about that.
It should be:
buttons [i].addActionListener(this);
but you knew that already.
The program I had to use to write the code kept prompting me to add (ActionListener) in there, so I put it in to shut it up temporarily, but then forgot to take it out because it didn't bring up any errors or interfere with the functionality of the Applet.

>>35

You say you want me to "shut up and go away", but yet you bump my thread. And I'm the failure?

Name: Anonymous 2009-02-25 7:04

The fuck up IT   IS SO ORDERED.

Name: Anonymous 2011-02-03 1:21


Don't change these.
Name: Email:
Entire Thread Thread List