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.
Related
I have a Java Swing Dialog with a hidden JLabel above each input component (i.e. JTextField). The purpose of this hidden JLabel is to use it as validation output for its input component.
Let's say, there is an input field for the description of some entity, which has to be non empty, and should contain some special stuff. On error, the action could call the following method:
private void invalidateDescription(String errMessage) {
errDescriptionLabel.setText(errMessage);
errDescriptionLabel.setVisible(true);
descriptionTextField.setBackground(ERR_COLOR);
}
After that, I call pack() and invalidate()
The problem is, that the JDialog still has the same vertical size, so that some of the components (the buttons in the bottom of the dialog) disapear (because they're out of view).
Do you have any suggestion how to fix it?
Best Regards.
edit: I forgott to mention, that the JDialog has a "Free Design" Layout (Netbeans GUI Builder default).
edit 2: I'm looking for a solution which doesn't require kind of a placeholder for (error) JLabel. "Empty Space" is not a desired solution because the dialog doesn't look balanced.
Use CardLayout place your labesl and empty JPanels ans swap thm when necessary.
Instead of using setVisible(), give errDescriptionLabel a background color that matches that of the enclosing panel when the entry is valid.
I have a Java Swing Dialog with a hidden JLabel above each input component
I would not use a hidden component for this. I would change:
private void invalidateDescription(String errMessage)
to
private void invalidateDescription(String errMessage, component inputComponent)
Then I would display a popup with the error message. You could use a non-decorated JDialog as the popup. You might even be able to use a JPopupMenu as the popup.
When you display the popup you would position the popup releative to the input component.
I am using three JButtons in my swing application. When I click on each button, the corresponding data (formatted in JTable with JScrollPane) will display on JPanel.
Problem: when I resize the JFrame, the JPanel is replacing with default button (the button which i was clicked first) information instead of current JButton information.
My sample code:
jbutton1.addActionListener(this);
jbutton2.addActionListener(this);
public void actioPerformed(ActionEvent e){
if(e.getActionCommand.equals("button1"))
JPanel.add(table1);
}
if(e.getActionCommand.equals("button2"))
JPanel.add(table1);
}.......
Resizing the JPanel will not suddenly replace components or add other components to the panel.
My best guess (and it is a guess due to the limited information in the question) is that none of your buttons actually work and just show the wrong information.
The code you posted only contains an add without any revalidation of the layout. Consult the javadoc of the Container#add method. When you resize, the layout gets revalidated and you see what is actually contained in the JPanel.
Possible solutions:
Call invalidate and repaint on your panel as well in your ActionListener
Use a CardLayout to switch between the different components
I personally prefer the CardLayout option, but it might depend a bit on the situation.
Note that in the code you posted, you add table1 for both buttons. Might be a copy-paste problem, or a problem with your actual code.
I was unable express problem clearly.Sorry for your inconvenience.
JPanel.removeAll() method has fixed my problem.
I added this method before adding any new component to JPanel. That fixes JPanel unexpected behavior.
I have a left Panel with multiples Jlabels which i use them as buttons to change a Main Panel's content which is layouted with a CardLayout.
I cant work perfectly with these events:
mouseEntered : to make highlight effect to the jlabel
mouseExited : to take off the highlight effect.
mouseClicked : to change the content of the main Panel and start some threads
The problem here that can't found an event or a method tell me that another Jlabel has been clicked so i can stop my threads started in the mouseClicked event,
OR
an event or method tell me that a JPanel in the CardLayout has been displayed or hidden.
Your problem is not finding an appropriate event. I think you are doing this using a visual GUI builder and expect to solve everything out-of-the-box. It's not going to work that way, you will need to write some real code. For example, write a method that you will call from the mouse click listener of each of the three JLabels. Thus you will have arranged for this method to be called for each JLabel click. Then in the method do the appropriate handling. This is just a rough outline, you haven't provided much detail to give any further advice.
It sounds like you need FocusEvents and FocusListeners. These are supported by all JComponents like JPanel, JLabel, and JButton, such as by calling addFocusListener();
Basically a FocusListener can tell you when a JComponent gains focus (such as by clicking on the JComponent) and when it looses focus (such as by clicking on a different JComponent).
Refer to http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/event/FocusListener.html for further information
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)
class Deal implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
dl.setDeck();
dl.shuffle();
dl.firstDraw(pl);
for(Card c:pl.showHand())
panelplay.add(new JLabel(c.getImageIcon()));
panelplay.validate();
}
}
This is an event handler for a Jbutton. The method pl.showHand() returns a ArrayList of a user defined class 'Card'. Inserting a println() inside the loop shows the print, so the code is being executed but the Panel panelplay isnt showing card Images.
What about the existing labels on the panel? You don't remove them. I'm guessing you are using a FlowLayout and the labels just get added to the end of the panel so you don't see them.
So one solution is to use panel.removeAll() before adding the labels back to the panel. I then use:
panel.revalidate();
panel.repaint();
Or the better option as suggested earlier is to not replace the labels but just replace the Icons using the setIcon() method.
Do as Gilbert says, look at the Swing Tutorial part that concerns Labels.
JLabel has the following methods...
void setIcon(Icon)
Icon getIcon()
Also look at the SplitPaneDemo It does exactly what you want, you can even run it with JNLP to see.
You don't want to add the JLabel in the ActionListener.
You want to use an already added JLabel setText() method in the ActionListener.
You define all the Swing components once, when you create the GUI.