GeeCON Prague 2014*
The moose likes Swing / AWT / SWT and the fly likes Removing user-interface components- HELP! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Removing user-interface components- HELP!" Watch "Removing user-interface components- HELP!" New topic
Author

Removing user-interface components- HELP!

Naf Rash
Ranch Hand

Joined: Feb 19, 2004
Posts: 85
I am trying to remove some items from a container/contentpane. These include JButtons, JLabels and JTextFields. Even after these things have been used they remain visible and when other things need to be added to the content pane they go on top of the ones present before. Clearly the whole window becomes messy and incomprehensible. I have tried using, dispose and remove but these don't work - they are not recogised; the following errors are evident:
method dispose() not found in class javax.swing.JTextField.
and other similar methods. I am aware of usinf setVisible and setEnabled but they retain the space that they are using and so nothing can be placed there. Please help!!
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
Have you tried remove method from Container?
Naf Rash
Ranch Hand

Joined: Feb 19, 2004
Posts: 85
Can you exaplin what you mean by this? If you mean just removing the whole container then I don't think that this is a very good idea becuase there are some components within that which I would have to recreate. I would prefer to clear the stuff that I no longer need so that it can be used for some additional things ie, Buttons
Don Kiddick
Ranch Hand

Joined: Dec 12, 2002
Posts: 580

does that make sense ?
D.
Naf Rash
Ranch Hand

Joined: Feb 19, 2004
Posts: 85
Yeah that did seem to make sense but it doesn't really work for JButtons- I can't find the methods that actually get them! Also all this is being done in an inner class and when I tell it to get something ie, a JTextField from the outer class -it won't do so and the JTextField that I am trying to get isn't being recognised??? I need help DESPERATELY!!!
Don Kiddick
Ranch Hand

Joined: Dec 12, 2002
Posts: 580
Originally posted by Naf Rash:
Yeah that did seem to make sense but it doesn't really work for JButtons- I can't find the methods that actually get them! !!!

You have to write them yourself or access the relevent fields directly.
Also all this is being done in an inner class and when I tell it to get something ie, a JTextField from the outer class -it won't do so and the JTextField that I am trying to get isn't being recognised???

Not sure I fully understand. You would have to post the code and any error message that you are getting.
I need help DESPERATELY
Folks round here are pretty helpfull but you need to ask the right questions. Try to focus on one problem at a time.
D.
[ February 25, 2004: Message edited by: Don Kiddick ]
Naf Rash
Ranch Hand

Joined: Feb 19, 2004
Posts: 85
Below is the code that I have come up with so far. The problems start at line 18 :
JButton sb = startButton.getButton();
JButton eb = exitButton.getButton();
Container c1 = contentPane.getContainer();
c1.remove(sb);
c1.remove (eb);
The error I am getting is startButton cannot be resolved and the same for exitButton and contentPane. However, I don't think getButton() is actually a method? I don't know what I can use. PLEASE HELP!

The second set of errors are at line 39:
String f = url.getText();
Container c1 = c.getContentPane();
c1.remove( f );
The errors are:
Errorc cannot be resolved
ErrorThe method remove(int) in the type Container is not applicable for the arguments (String)

The reason I have done JTextField(String)f = url.getText(); is because getText() gets a String or atleast in my code its manipulated as a string. As a resu
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.sql.*;
import java.util.*;
public class connectionBox extends JFrame {
private JTextField url, uid;
private JPasswordField password;
private JLabel urllabel, uidlabel, passwordlabel;
private connectionBox cb;
private JButton mine;
Connection connection;

public connectionBox(){
super ("Establishing a connection");
JMenuBar bar = new JMenuBar();//creates the menu bar
setJMenuBar(bar);//sets the menu-bar for the Frame
//create File Menu and Exit Menu
JMenu fileMenu = new JMenu ("File");
fileMenu.setMnemonic('F');
final JMenuItem startMining = new JMenuItem ("Start Mining");
startMining.setMnemonic('N');
startMining.addActionListener(
new ActionListener(){
public void actionPerformed (ActionEvent e){
if (e.getSource() == startMining)
connectionForm();
}
}
);
fileMenu.add(startMining);
JMenuItem exit = new JMenuItem ("Exit");
exit.setMnemonic ('X');
exit.addActionListener(
new ActionListener(){
public void actionPerformed (ActionEvent e){
System.exit(0);
}
}
);
fileMenu.add(exit);
bar.add(fileMenu); //add the File menu

//create the Help menu
JMenu helpMenu = new JMenu ("Help");
helpMenu.setMnemonic ('h');
JMenuItem startHelp = new JMenuItem ("Start Help");
startHelp.setMnemonic('s');
startHelp.addActionListener(
new ActionListener(){
public void actionPerformed (ActionEvent e){
// if this happens then implement the documentation

}
}
);

helpMenu.add(startHelp);
bar.add(helpMenu); //add the File menu


/* The following code is for the buttons that user sees, allowing the user to utilise the data
* mining tool without having to go through the menus avaialable, ie, having to go through the
* file menu. */
Container contentPane = getContentPane();
JPanel panel = new JPanel();
final JButton startButton = new JButton ("Start");
final JButton exitButton = new JButton ("Exit");

startButton.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
if (e.getSource() == startButton)
connectionForm();

}
}
);

exitButton.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
}
);
panel.add(startButton);
panel.add (exitButton);
contentPane.add(panel, "South");

setSize(500,200);
show();




}
public static void main(String args[]){
connectionBox app = new connectionBox();
app.addWindowListener(
new WindowAdapter(){
public void windowClosing (WindowEvent e){
System.exit(0);
}
}
);
}
//inner class for event handling
private void connectionForm(){

JButton sb = startButton.getButton();
JButton eb = exitButton.getButton();
Container c1 = contentPane.getContainer();
c1.remove(sb);
c1.remove (eb);


Container c = getContentPane();
c.setLayout(new FlowLayout());
/* The following code provides a text box and asks for the URL of the database that needs to be
* connected to.*/
JPanel northPanel = new JPanel();
urllabel = new JLabel (" Enter the name of the database.");
northPanel.add(urllabel,BorderLayout.NORTH);
c.add(northPanel,BorderLayout.NORTH);
url = new JTextField (15);
c.add(url,BorderLayout.NORTH);
/* The following code provides a text box and asks for the User-ID to allow a connection to the
* database to be established. */
JPanel centrePanel = new JPanel();
uidlabel = new JLabel (" Enter the User-ID for the specified database."
+ "NB: If no User-ID exists enter 'null'.");
centrePanel.add(uidlabel);
c.add(centrePanel,BorderLayout.CENTER);
uid = new JTextField (15);
c.add(uid,BorderLayout.CENTER);
/* The following code provides a text box and asks for the Password to allow a connection to the
* database to be established. */
JPanel southPanel = new JPanel();
passwordlabel = new JLabel (" Enter the Password for the specified" +
"database. NB: If no Password exists enter 'null'.");
southPanel.add(passwordlabel);
c.add(southPanel, BorderLayout.CENTER);
password = new JPasswordField (15);
c.add(password, BorderLayout.CENTER);
JPanel minePanel= new JPanel();
mine = new JButton("Mine!");
minePanel.add(mine, BorderLayout.SOUTH);
c.add(minePanel, BorderLayout.SOUTH);
ActionHandler handler = new ActionHandler();
url.addActionListener(handler);
uid.addActionListener(handler);
password.addActionListener(handler);
mine.addActionListener(handler);
setSize (600, 300);
show();

}
//inner class for event handling
private class ActionHandler implements ActionListener{

public void actionPerformed (ActionEvent e){

String url1="null", uid1="null", password1="null";

if ((url.getText().length() > 0 )&&(uid.getText().length() > 0 ) && (password.getText().length() > 0) && e.getSource()==mine){
url1 = ("jdbc dbc:" + url.getText()+ "");
uid1 = uid.getText();
password1 = password.getText();
//char[] password1 = password.getPassword();

tryConnection(url1, uid1, password1);

String f = url.getText();
Container c1 = c.getContentPane();
c1.remove( f );

}
}
}
Don Kiddick
Ranch Hand

Joined: Dec 12, 2002
Posts: 580
Your refrencing variables & methods that do not exist !
#1 Make startButton and exitButton member variables of your class. startButton *is-a* button so you don't need to call getButton().
To get a handle to the content pane call getContentPane().
D.
Naf Rash
Ranch Hand

Joined: Feb 19, 2004
Posts: 85
You said:
Make startButton and exitButton member variables of your class. startButton *is-a* button so you don't need to call getButton().
But if you look at the code startButton and exitButton are part of my class- unless I have misunderstood what you have said. They are defined in the outer class and as a result I should have to call them shouldn't I - otherwise the inner class will not recognise those butons.
Don Kiddick
Ranch Hand

Joined: Dec 12, 2002
Posts: 580
Read this
Naf Rash
Ranch Hand

Joined: Feb 19, 2004
Posts: 85
Thanks- I understand what you mean now. But I still can't find any way of removing the components. As a result I think I can get over this problem by getting rid of a whole contentPane and replacing it with another. Although I don't know how to do this. I am therefore going to pose another question about this to the forum with respect to removing contentPanes. Please help if you can.
 
GeeCON Prague 2014
 
subject: Removing user-interface components- HELP!