Disable JFrame but Want Access to Menu Windows - java

I dont know how to make this with simple and easy code.
I can just go to each piece of my code and use setEnabled to false on each component, but I want a easy way.
I want to disable the entire frame, but still want to close/maximize/minize it. do you understand?
And if I use setEnabled(false) on my frame, it disables that options too, the options of windows menu bar you know?
Thanks alot in advance..

Simple,Put all your components in a JPanel and disable the JPanel :-)

You can use a code snippet like below. Insert this method/routine in a utility class. Since the method is static you can call it without that class's instance. And make invocations to this method to enable/disable components (menu items, buttons, text fields etc.)
// Let's say you have a JFrame object called myFrame
// and a reference to its content pane.
// Container container = myFrame.getContentPane();
public static void toggleAbilities(Container container, boolean enabled)
{
Component[] components = container.getComponents();
for(Component component: components)
component.setEnabled( enabled );
}
If you get stuck, I can post overall working code as well.
(Toggling the ability of a set of buttons in a JFrame)

Related

Java Remote Client GUI

I've set up a Server which runs and accepts connections from my Remote Client, and now I'm working on my GUI.
Before anything else, my goal here is to create a nice looking client that will have a login screen (login/pw), and then a nice layout with my options/perhaps a chat box after the user has logged in.
I've searched a lot online and used this site to set up my server and get things working, but I've got a bit of a problem with the GUI/theory and hope someone here can guide me a bit.
At the moment, I've set up a class called ClientGUI which is called from my main class, and this produces a 420x240 size screen. After placing my login/password JTextField boxes here, is it "proper" to set up the other GUI's the way I've outlined below? I'm not sure if I should be putting them under one class or how I would advance from one GUI to the next. I'm thinking I should repaint and resize the screen as necessary, but I am not sure how to set it all up. A brief outline would be helpful (you don't need to give me exact code).
public class ClientGUI extends JFrame {
public ClientGUI() {
setSize(420,240);
setVisible(true);
setTitle("Title");
setResizable(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
}
public loginGUI() {
//code for my login/pw boxes, images, listener for entering information
}
public afterlogginginGUI() {
}
paint() {
//not too sure about how this should be setup either. Should I do all my textfield
//and image work in paint()?
}
}
I have never made anything like this, so I have the feeling I'm not setting this up in an ideal way.
An alternative is to have a different java class extending JFrame for each 'screen' I want, but if I do it this way, would I do it like this?
In my main RemoteClient class:
main {
ClientGUI();
//display whatever
LoginGUI();
//listen for login info
if (loginIsValid) {
afterlogginginGUI();
}
}
I think you're thinking in to much of a linear fashion, where the code flows from A then to B then to C ... where in fact, Swing (and GUI's in general) are event driven...
C happens, so you do B, which triggers F so you do E ...
Start by creating a JPanel, onto this add your JTextField and JPasswordField, this will act as you basic login view. You could then add this to a JFrame or JDialog depending on your needs. You will need some way for the user to either "cancel" or "validate" their credentials.
Typically, I do this a separate view, as I never know where my poor "user details" pane might end up, but you could do this a single view (including the buttons within the "user details" pane), that will come down to your requirements.
You can use a CardLayout to switch from the "login" view to the "application" view. This has the benefit of maintaining only a single frame and prevents windows from been splashed all about the place...
I would, personally, separate the core functionality of the views to separate classes, this means you can simply create an instance when you need it and add it to whatever container you want.
I would recommend against extending from JFrame directly. This locks you into a single container (making it hard to re-use components or extend the program later) and you're not adding any new functionality to the class anyway...
Start by having a look at Creating a GUI With JFC/Swing.
You'll probably also be interested in How to Use CardLayout, How to Make Dialogs, How to Use Buttons, Check Boxes, and Radio Buttons and How to Write an Action Listeners
You'll also need to have a look at Laying Out Components Within a Container
Because you're likely waiting for a response from the server at some point, you will need to have a look at Concurrency in Swing and Worker Threads and SwingWorker wouldn't hurt

Swing: Dynamically change visibility of components

When I set the visibility of components (mainly JPanels that have other child components) to false or back to true later, do I have to call .revalidate() and .repaint() on the parent container explicitly or do the LayoutManagers handle this automatically?
When I tried this with a component in BorderLayout-North position for example, everything looked fine without calling revalidate. Just not sure if it will work on all platforms and with all LayoutManagers.
when u set the visibility of components like JPanels. JVM automatically call revalidate() and repaint() method when u set the visibility to true. u does not need to call the all these method. and it will work for all layout
The following code may solve your problem
p2_wrkrreg=new JPanel();
p2_wrkrreg.setBounds(201,0,830,720);
// p2_wrkrreg.setLayout(null);
//p2_wrkrreg.setBackground(Color.white);
p2_wrkrreg.setVisible(false);
In this code,
The JPanel declared and make it invisible, it will be visible on a button click
#Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btn_wrkrreg)
{
p2_wrkrreg.setVisible(true);
}
here btn_wrkrreg is a JButton

Java requestFocus() not working at the constructor

In my Java Swing Application Reuestfocus method not working at the Constructor. But it works if I used requestfocus along with button action listener. I want set Requestfocus at the form load. I dont understand whats the wrong here. P.S I use JInternal Frame
txtItemName.requestFocusInWindow (); // did not work
txtItemName.requestFocus (); // did not work
public ItemMgt() {
initComponents();
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
txtItemName.requestFocus(); // did not work
}
});
}
private void formInternalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
txtItemName.requestFocus(); // did not work.
}
The case might by that your component is not visible, so the GUI manager can not set focus on it.
As is specified in documentation:
(..) component must be displayable, focusable, visible and all of its ancestors (with the exception of the top-level Window) must be visible for the request to be granted.
Generally you should avoid to have so robust logic in constructor. Your graphic components should be constructed first. When this is finished they should be adjusted to your preferences.
Instead of requestFocus(), it is recomended to use requestFocusInWindow() for more please visit the tutorial: How to Use the Focus Subsystem
If you are using Netbeans IDE, then its much easier. Just make sure that all controls before your textbox are not focusable by unchecking the 'focusable' property in the Properties window. This will leave your textbox as the first focusable control thus allowing it to receive focus when the internal frame is opened

add panel to combobox popup menu

i have jFrame = frame
it have jcombobox = combo
then i have jpanel = panel
i have many component inside this panel
i try to add this panel into combobox popupmenu
so if combobox clicked,
panel that have many components will show up
it is possible to add panel into combobox popup menu?!?!
how to do it???
i already read
http://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html
and
http://docs.oracle.com/javase/tutorial/uiswing/examples/components/ComboBoxDemoProject/src/components/ComboBoxDemo.java
but still not have any clue
how to do it?
thankz a lot for any help...
So from your description, you have a panel that is not visible, that you would like to appear if the combobox is clicked? So it will appear for any option in the combobox?
That should be simple enough. Lets modify the JLabel in this ComboBoxDemo from the Java Tutorials. Since they both inherit from JComponent, we will be able to make the JLabel and the JPanel visible in the same way.
First, make sure you understand what the demo is doing. The Combobox options are changing the format of the date's text in the JLabel. We want to edit the demo in such a way that this JLabel is not visible until after we select any option in our JComboBox.
First, we will want to include a boolean as a class variable, so that we can access it in any of our methods.
boolean visibleComp;
Next, in the constructor, you will want to change the JLabel "result" to be invisible by default. We can do this by using the setVisible method of the JComponent.
result.setVisible(false);
Now we need to control when and how result becomes visible -- as we continue through the code, we see that the actionPerformed method handles our events, and passes the formatting details off to another method called reformat.
Since reformat is also called in our constructor, we will want to set our boolean value in the actionPerformed method.
visibleComp = true;
We will then want to add a conditional statement to the try block in reformat -- this will check to see if our boolean is true, which would only occur if an action had been performed by the user. We can use this to set our component's visibility.
if(visibleComp){
result.setVisible(true);
}
You can easily interchange a JPanel with this example. Hope that helps.

Why is componentShown() not called?

I have a CardDetailsPanel class which contains several JLabels and JTextFields. This class in contained in a AddCardsPanel and is initialized as follows:
cardDetailsPanel = new CardDetailsPanel(true);
add(cardDetailsPanel, java.awt.BorderLayout.CENTER);
I also have a JLabel that contains instructions. I want to update this label when the CardDetailsPanel first appears and when focus changes to each JTextField. I have found the addFocusListener() method that will work for the later. However, my compenentShown() method isn't working for the former:
addComponentListener(new java.awt.event.ComponentAdapter() {
public void componentShown(java.awt.event.ComponentEvent evt) {
formComponentShown(evt);
}
});
(Okay, I know this is ugly. It was generated by NetBeans.)
private void formComponentShown(java.awt.event.ComponentEvent evt) {
this.frame = (BaseballFrame) this.getParent().getParent().getParent().getParent().getParent().getParent();
}
(Yah, this is even uglier. I'll deal with the chained getParent() calls later. I want to do other things here as well.)
So why doesn't my listener get called? And how do I write a listener that will perform some actions whenever my CardDetailsPanel appears on the screen?
Use an AncestorListener as described in dialog focus.
When a JDialog (or JFrame for that matter) is made visible, focus is placed on the first focusable component by default. There may be times when you want to change this behaviour. The obvious solution would be to invoke the requestFocusInWindow() method on the component you wish to receive focus. The problem is that this doesn’t work all the time.
...
The problem is .. a component can’t request focus unless it has been added to a “realized” dialog. A realized dialog basically means that the Swing JDialog has been added to a peer component that represents a dialog on the underlying OS. This occurs when you invoke the pack() or setVisible(true) methods on the JDialog.
And that is where the ancestor listener comes in handy. For a component in a modal dialog, it will be fired once the component becomes visible, and is realized & focusable.
Edit:
The above comment applies to components in any Swing container, including JFrame and JPanel.

Categories

Resources