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.
Related
I have a JFrame named MainGUI. Inside of MainGUI I have passed three LinkedList ll1, ll2, ll3.
These LinkedList are full of data and I'm trying to just print one of them on the screen into my JPanel. I'm use to just doing a for loop and using System.out.println to print things out onto the screen.
So right now I have MainGUI which hosts three buttons.
New Tasks In Progress Tasks Completed Tasks
Each button has a different LinkedList ll1, ll2, ll3 etc.
I want to be able to click the button and have the data elements listed below in the JPanelI created which rests under the buttons.
Any help is deeply appreciated.
Since you provided no code, I assume you are having trouble understanding how LinkedList can interact in programs that have a GUI.
First off, when using buttons you always need to instruct them to do something when they are clicked by adding an ActionListener, as explained in this answer.
Secondly, if you want to add the list data to the JPanel, there are a few ways you can do it. A JList, or if you'd like the user to be able to copy and paste the data (I find it to be very handy), a JTextArea, ... Just make sure to call setEditable(false) in order to stop the user from fiddling with the data you provide. Considering a JTextArea, here's what that would look like, if ll1 contained Strings:
Adding somewhere that our JPanel contains a JTextArea:
JTextArea txtArea = new JTextArea();
txtArea.setEditable(false);
panel.add(txtArea);
Now, we order the button to do something when clicked:
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
txtArea.setText(null); //clear out old text
for(String str: ll1) {
txtArea.append(str+"\n");
}
panel.revalidate(); //repaint JPanel
}
});
This way, you can click the button as many times as you want. Note that if you add more content to ll1 after it is displayed it won't get visually updated by itself, you will always need to click the button again or look further into Listeners.
You can try adding a JTextArea or whichever JComponent that suits what you want to display to the JPanel that you want to display the data from. Write the data from your linked list to that JComponent using its method e.g. append() if you're using JTextArea.
I am trying to set gmailPasswordField.setVisible(true) & senderPasswordLabel.setVisible(true) if the JTextField is changed, but it's not working.
doc.addDocumentListener(
new DocumentListener() {
public void changedUpdate(DocumentEvent e) {
int minLengthEmail = "n#gmail.com".length();
if (countDocumentChangeLength > minLengthEmail) {
gmailPasswordField.setVisible(true); // Doesn't appear to do anything
senderPasswordLabel.setVisible(true); // Nor this
}
}
});
Did you add a System.out.println(...) to see if the code is being executed? This is a basic debugging technique to see if the code is executing as you expect.
The changedUpdate(...) method is invoked when you change an attribute of the text.
I suspect you want to implement the other DocumentListener methods so you will be notified when the "text" in the Document changes. Read the section from the Swing tutorial on How to Write a DocumentListener for more information and working examples.
Don't use the method setVisible(boolean). Use CardLayout instead.
Little howto:
Move your widgets, which visibility must be switched, into a new JPanel (layout panel).
Create another JPanel with CardLayout (card panel) and place the layout panel into it.
Place an empty JPanel into the card panel.
Make sure that the appropriate card is visible (in your case - the empty card).
Make another card visible when required (in your document listener).
I'm trying to set a variable to be a new JPanel and then add it once a button is pressed, but it is not working and I don't know why.
code:
private void nextButtonActionPerformed(java.awt.event.ActionEvent evt) {
remove(scriptPanel);
scriptPanel = new GemPanel();
add(scriptPanel);
validate();
repaint();
pack();
}
GemPanel is just a JPanel class I made. When I press the next button, it re-sizes the frame to be as small as possible and nothing actually happens. If I re-size it to normal, the original scriptPanel is still there.
What gives?
Instead of trying to remove and add entire panels, a better, less problem prone approach would be to use a CardLayout that will allow to swap views. You can see more at How to use Cardlayout
Also, by the looks of your method signature, it seems you're using the Netbeans builder too. You may also want to take a look at How to Use CardLayout with Netbeans Gui Builder
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 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.