|
-
Apr 11th, 2001, 08:50 PM
#1
Thread Starter
Hyperactive Member
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!!
-
Apr 12th, 2001, 12:22 AM
#2
Do you want to post some code? You look like you are using the correct method. ".setVisible(boolean b)"
-
Apr 12th, 2001, 09:11 AM
#3
Thread Starter
Hyperactive Member
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);
}
}
}
-
Apr 12th, 2001, 02:01 PM
#4
Dazed Member
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?
-
Apr 12th, 2001, 02:17 PM
#5
Dazed Member
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
-
Apr 12th, 2001, 03:03 PM
#6
Thread Starter
Hyperactive Member
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!
-
Apr 12th, 2001, 03:11 PM
#7
Dazed Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|