Results 1 to 7 of 7

Thread: It's driving me crazy!!

  1. #1

    Thread Starter
    Hyperactive Member rockies1's Avatar
    Join Date
    Jul 1999
    Location
    Stuck at work
    Posts
    375
    Coming from a VB background, Java is driving me crazy!!!


    I have some code for a GUI application. It has several buttons on it, with one .setVisible(false);

    I want to click another button and make the invisible one visible. Then I want to click the one that was invisible, and make it invisible again...

    What ends up happening is this:

    -Click the button, invisible one becomes visible
    -Click the now visible one and it goes grey, looking like a button, but not clickable...

    Anyone know how to make that button TRULY invisible so I see the back of the panel, and not just the greyed out button?

    Thanks!!
    Morgan
    [email protected] - Home
    [email protected] - Work
    Using VB6 SP6 but trying to learn VB2005EE

  2. #2
    VirtuallyVB
    Guest
    Do you want to post some code? You look like you are using the correct method. ".setVisible(boolean b)"

  3. #3

    Thread Starter
    Hyperactive Member rockies1's Avatar
    Join Date
    Jul 1999
    Location
    Stuck at work
    Posts
    375
    Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Lab5 extends JFrame implements ActionListener
    {
      JButton btnSaveName;
      JButton btnUpdateName;
      JButton btnSaveTest;
      JButton btnUpdateTest;
      JButton btnSaveUpdate;
      JButton btnClear;
      JButton btnClose;
    
      JLabel lblFName;
      JLabel lblLName;
      JLabel lblEMail;
      JLabel lblTestScore;
    
      JTextField txtFName;
      JTextField txtLName;
      JTextField txtEMail;
      JTextField txtTestScore;
    
      public Lab5()
      {
        super("Morgan Erickson - CIS362 - Lab 5");
    
        Container container = getContentPane();
        container.setLayout(new BorderLayout());
    
        //Panels
        JPanel panelStudentName = new JPanel();
        JPanel panelScoreEntry = new JPanel();
        JPanel panelButtons = new JPanel();
        JPanel panelOutput = new JPanel();
        panelStudentName.setLayout(new GridLayout(3,4));
        panelButtons.setLayout(new GridLayout(7,1));
        panelStudentName.setBackground(Color.green);
        panelScoreEntry.setBackground(Color.blue);
        panelButtons.setBackground(Color.red);
        panelOutput.setBackground(Color.cyan);
    
        //Buttons
        btnSaveName = new JButton("Save Student");
        btnUpdateName = new JButton("Update Student");
        btnSaveTest = new JButton("Save Test");
        btnUpdateTest = new JButton("Update Test");
        btnSaveUpdate = new JButton("Save Update");
        btnClear = new JButton("Clear");
        btnClose = new JButton("Exit");
    
        btnSaveName.addActionListener(this);
        btnUpdateName.addActionListener(this);
        btnSaveTest.addActionListener(this);
        btnUpdateTest.addActionListener(this);
        btnSaveUpdate.addActionListener(this);
        btnClear.addActionListener(this);
        btnClose.addActionListener(this);
    
        btnSaveUpdate.setVisible(false);
    
        //Labels
        lblFName = new JLabel("First Name:");
        lblLName = new JLabel("Last Name:");
        lblEMail = new JLabel("E-Mail Address:");
        lblTestScore = new JLabel("Test Score:");
    
        //Text Boxes
        txtFName = new JTextField(10);
        txtLName = new JTextField(10);
        txtEMail = new JTextField(20);
        txtTestScore = new JTextField(3);
    
        //Panel Contents
        panelButtons.add(btnSaveName);
        panelButtons.add(btnUpdateName);
        panelButtons.add(btnSaveTest);
        panelButtons.add(btnUpdateTest);
        panelButtons.add(btnSaveUpdate);
        panelButtons.add(btnClear);
        panelButtons.add(btnClose);
    
        panelStudentName.add(lblFName);
        panelStudentName.add(txtFName);
        panelStudentName.add(lblLName);
        panelStudentName.add(txtLName);
        panelStudentName.add(lblEMail);
        panelStudentName.add(txtEMail);
        panelScoreEntry.add(lblTestScore);
        panelScoreEntry.add(txtTestScore);
    
        container.add(panelButtons,BorderLayout.WEST);
        container.add(panelStudentName,BorderLayout.NORTH);
        container.add(panelScoreEntry,BorderLayout.CENTER);
        container.add(panelOutput,BorderLayout.SOUTH);
      }
    
      public static void main(String[] args)
      {
        Lab5 frame = new Lab5();
        frame.pack();
        frame.setVisible(true);
      }
      public void actionPerformed(ActionEvent event)
      {
        if(event.getSource() == btnClose)
        {
          System.exit(0);
        }
        if(event.getSource() == btnUpdateTest)
        {
          btnSaveUpdate.setVisible(true);
        }
        if(event.getSource() == btnSaveUpdate)
        {
          btnSaveUpdate.setVisible(false);
        }
      }
    }
    Morgan
    [email protected] - Home
    [email protected] - Work
    Using VB6 SP6 but trying to learn VB2005EE

  4. #4
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    First off you might want to add a
    window monitor to
    monitor for a window
    closing event.. Either as a seperate
    class or code it in the Lab5 class itself.
    The exit button works great
    but dos tends to hang if the
    close button on the frame is fired.

    import java.awt.event.*;
    import java.awt.Window;

    class BasicWindowMonitor extends WindowAdapter {
    public void WindowClosing(WindowEvent e){
    System.exit(0);
    }
    }

    Ok so let me try and understand what you want.

    "-Click the button, invisible one becomes visible
    -Click the now visible one and it goes grey, looking like a button, but not clickable..."

    click save update and it becomes invisible...... which it
    already does. then click update test and it becomes grey. am i right?

    just change the:

    if(event.getSource() == btnSaveUpdate)
    {
    btnSaveUpdate.setVisible(false);
    }
    }
    }

    to:

    if(event.getSource() == btnSaveUpdate)
    {
    btnSaveUpdate.setEnabled(false);
    }
    }
    }

    i dont see why you would want to set any buttons
    to setVisible(false) just disable them. Nice layout
    though. Are you gong to add a JFileChooser to save
    your changes?

  5. #5
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    you could do something like this.....
    just some thoughts.


    /* Programmer Brandon Rohlfs 3/7/01
    ---------------------------------------
    This class shows a JFileChooser dialog box.
    The FileChooser class works in conjunction with the
    */ ExtensionFilter class to set a filter for the files

    import javax.swing.*; // for JFileChooser
    import java.io.*;

    public class FileChooser{
    public static void main(String[] args){

    JFileChooser jfc = new JFileChooser();
    jfc.setDialogTitle("Please choose a file to be encrypted");
    jfc.addChoosableFileFilter(new ExtensionFilter("txt", "Test Files"));
    jfc.addChoosableFileFilter(new ExtensionFilter("java", "Java Source Code"));
    int result = jfc.showOpenDialog(new JFrame());

    try{
    FileOutputStream fout = new FileOutputStream("C:\\Java\\outfile.txt");

    if(result == JFileChooser.APPROVE_OPTION){

    File f = jfc.getSelectedFile();
    byte[] buffer = new byte[64];

    if(f != null){ // make sure the user didnt choose a directory
    FileInputStream fin = new FileInputStream(f);
    while(true){
    int bytesread = fin.read(buffer);
    if(bytesread == -1) break;
    fout.write(buffer, 0, bytesread);
    }
    fin.close();
    fout.close();
    }
    }
    System.exit(0);

    }catch (IOException e){System.err.println(e);}

    }
    }

    now here is the ExtensionFilter class


    import javax.swing.filechooser.*;
    import java.io.*;


    public class ExtensionFilter extends javax.swing.filechooser.FileFilter{

    String extension;
    String description;

    public ExtensionFilter(String extension, String description){
    if(extension.indexOf(".") == -1){
    extension = "." + extension;
    }

    this.extension = extension;
    this.description = description;
    } // end method

    public boolean accept(File f){
    if(f.getName().endsWith(extension)){
    return true;
    }
    return false;
    }

    public String getDescription(){
    return this.description + "(*" + extension + ")";
    }
    } // end class

  6. #6

    Thread Starter
    Hyperactive Member rockies1's Avatar
    Join Date
    Jul 1999
    Location
    Stuck at work
    Posts
    375
    I could have SWORN I tried btnSaveUpdate.setEnabled(false); last night as I was pulling my hair out trying to get this!!

    I REALLY appreciate this, and the other suggestions/Code you provided.

    I'm not gettng much better at this, so I may be back with more questions.

    Again, Thank You!
    Morgan
    [email protected] - Home
    [email protected] - Work
    Using VB6 SP6 but trying to learn VB2005EE

  7. #7
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    You quite welcome. I know Java can
    be a pain in the A@! sometimes. I still
    have trouble with the language too.
    I figured you wanted to add a Filedialog
    since you have a save button on there.

    The code just filters java files only. I encrypt
    all of my source code files so if a person gains
    remote access to my system all of the files are
    unable to be read. Good luck!

Posting Permissions

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



Click Here to Expand Forum to Full Width