Setting a .gif on JButton in Java (NetBeans) - java

I ve been experimenting some trouble trying to insert a .gif image on a JButton. I ve made a gif on photoshop and I set it as an Icon on a JButton. However, whenever I run the project and the Window containing that button appears, the gif starts to "twinkle" really fast. It appears and dissapears like if java were refreshing the screen in a matter of miliseconds. Why is that happening?
I do attach the .gif file in case it's needed, and the code as well.
jPanel1 = new javax.swing.JPanel();
jButton1 = new javax.swing.JButton();
jPanel1.setLayout(null);
jButton1.setText("jButton1");
jButton1.setBorder(null);
jButton1.setBorderPainted(false);
jButton1.setContentAreaFilled(false);
jButton1.setDisabledIcon(new javax.swing.ImageIcon("C:\\Users\\alumnofi\\Desktop\\gif.gif"));
jButton1.setEnabled(false);
jPanel1.add(jButton1);
jButton1.setBounds(303, 233, 70, 60);
Thanks in advance and sorry for my english :)

Related

Vaadin: Using an image as a button

I wanted to use a image as a button. I got it working, but it is not very well made, please take a look at the screenshot. As you can see the Button itself is a lot bigger than the image, but I wanted it to be as big as the image:
The actual Button is bigger than the Image. The goal here is that there is nothing but the image to click. How can I achieve this? Here is the code ofthe button on the screenshot:
Button testButton = new Button();
String basepath = VaadinService.getCurrent().getBaseDirectory().getAbsolutePath();
testButton.setIcon(new FileResource(new File(basepath + "/VAADIN/themes/mytheme/img/Button.png")));
loginForm.addComponent(testButton);
I know that
testButton.setStyleName(BaseTheme.BUTTON_LINK)
makes the button invisible, but unfortunately that does not adjust the size of the button, just the visbility..
You can simply add a click listner to an image instead of using a button.
Image image = new Image(null, new ClassResource("/images/button-img.jpg");
image.addClickListener(e -> System.out.println("click"));
image.addStyleName("my-img-button");
And add this css, I use the #Stylesheet annotation to add in CSS.
.my-img-button {
cursor: pointer;
}
It works for me:
Button button = new Button();
button.setStyleName(ValoTheme.BUTTON_LINK);
button.setIcon(new ClassResource("/images/button-img.jpg"));
button.addClickListener(e -> System.out.println("click"));
Maybe you have additional css defined?
Maybe your button is contained in a layout with a fixed height?
Also make sure that your button has no width/height configured, so it can automatically adjust its size to that of the icon image.
The next problem you'll probably run into is the focus border:
Another approach would be to use a layout click listener, and add you own mouse-over/hover/focus styling via CSS.
VerticalLayout layout = new VerticalLayout(new Image(null, new ClassResource("/images/test.png")));
layout.addLayoutClickListener(e -> System.out.println("click"));
With Vaadin 14:
Image img = new Image("src");
Button testButton = new Button(img);
Quite straightforward.

JDialog cannot be converted to frame

I want to make applcation where you can add students and then assign to each one of them 20 books.
I have 3 windows:
Main windows (JFrame)
List of books (JDialog)
Add a book (JDialog)
I have JFrame where I can see list of all students, then I click on "List of books" where I can see the list of all books in database (.txt file). So when I click on that button in JFrame, I open JDialog, that works fine. But now I want to add some books to the list, so I click on button "Add a book" in "List of Books" JDialog. So I just want to open another JDialog a top of previous JDialog.
So I the window "List of books" (which I opened from Main window) I want to open windows "Add a book".
Now when I do it by the same method as I open JDialog from JFrame, it shows error :
private void pridatKnihuJButtonActionPerformed(java.awt.event.ActionEvent evt) {
addBookJDialog newwindow = new addBookJDialog(this, true);
newwindow.setLocationRelativeTo(null);
newwindow.setVisible(true);
}
It shows :
Incompatible types: addBookJDialog cannot be converted to Frame.
Is there a simple way to do this ?
I'm creating those windows in NetBeans design function.
I found a few topics about opening some JDialog on another JDialog, but I didnt understand how to do it :/ There are 3 lines of code that opens another JDialog. Is there a way to just simple open it ?
Thanks.
Ok. I found the answer... or more likely, I finally understood the code everyone was posting :D 2 hours of looking for this :D
If someone have same question. The answer is:
When you click on button in your dialog button, then head to the ActionPerformed section of that button (double-click on that button in NetBeans design section).
and write this :
JFrame frame = (JFrame) SwingUtilities.getWindowAncestor(this);
<target dialog> <optional name> = new <target dialog>(frame,true);
<optional name (but same as above)>.setLocationRelativeTo(null);
<optional name (but same as above)>pridatzaznam.setVisible(true);
Import what is needs to be imported and there you go.
It'll probably need these two:
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
Example :
JFrame frame = (JFrame) SwingUtilities.getWindowAncestor(this);
dialogIWantToOpen openthis = new dialogIWantToOpen(frame,true);
openthis.setLocationRelativeTo(null);
openthis.setVisible(true);
Will open a dialog atop of your current dialog, but only if your current dialog was opened from JFrame.
If you want to open a dialog atop a dialog atop a dialog etc... it will probably need some editation after (JFrame) where
SwingUtilities.getWindowAncestor(this);
will probably have to be something like
SwingUtilities.getWindowsAncestor(SwingUtilities.getWindowAncestor(this));
So this will open a dialog, that was opened from a dialog that was opened from a dialog that was opened from a JFrame. But I'm not sure if it will work. Didnt tried that.

How to put two components at the same place

I am currently building a game and I don't know how to make a main menu. I don't know how to put the text on the GIF background. Now the thing is when I try to put them on I try this code:
private void initializeDisplay() {
playBtn.setBorderPainted(false);
playBtn.setFocusPainted(false);
playBtn.setContentAreaFilled(false);
playBtn.setText("PLAY");
playBtn.setFont(new Font("Poiret One", Font.TRUETYPE_FONT, 27));
playBtn.setForeground(Color.WHITE);
mainPanel.add(playBtn);
mainPanel.add(bgImg);
mainPanel.setLayout(new GridBagLayout());
bgImg.setLocation(new Point(200, 200));
mainPanel.setOpaque(true);
mainPanel.addKeyListener(new Keyboard());
mainPanel.addMouseListener(new Mouse());
mainPanel.setFocusable(true);
}
Also when I do that I use a GridBagLayout for the layout type but it seems that I'm not get the result I want, the problem is the button not the image in the background because that's appearing but the button isn't appearing.
You could also add an image to a jlabel's icon field, and then add the label to your panel's content pane.
This post explains it more:
How to set a background picture in JPanel

JavaFx Button adds ellipsis when rendered on a E4 ToolControl

I've an E4 application which has a ToolControl, the class that handles the tool control creates a JavaFX button, for some reason the button adds ellipsis and I've no clue why.
Here is the link to the sample application
https://github.com/SDSethia/ColoredButton.git
JavaFX shortens the label automatically, when the size of the control (a button in your case) is too small for the text. This is independent of E4. So if you increase the size of the button, the complete text will show.
I looked at your project and I am wondering why you are using the SWT renderers, although you want to use JavaFX!
If you want to use E4 + JavaFX I recommend to use the e(fx)clipse renderers. This tutorial should get you started: https://wiki.eclipse.org/Efxclipse/Tutorials/Tutorial4
The button needs a layout (I wrapped mine in an HBox) for it to render correctly. Here is the modified code
final FXCanvas canvas = new FXCanvas(parent, SWT.NONE);
button = new Button();
button.setText("FxButton (1)");
button.setStyle("-fx-background-color: #186dee; -fx-text-fill: white;");
final HBox box = new HBox();
box.getChildren().add(button);
final Scene scene = new Scene(box);
canvas.setScene(scene);
This solved the issue.

How to show scroll bar in the text area in eclipse

I am learning Java.I just created an application (liked desktop one in c#) by adding some labels,a text view and a button.
Its fun learning this new thing,but i soon encountered an issue ,when u tried to add vertical scrolling to the text view i added on the UI.
I also tried adding vertical scroll to the text area but still my text area is not displaying the scroll bar.
The part of the code created when i added the controls from the panel to the UI is as below:
thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()
.addContainerGap(17, 17)
.addComponent(getJtxtArea(), GroupLayout.PREFERRED_SIZE, 158, GroupLayout.PREFERRED_SIZE)
The code for the function getJtxtArea() is as below:
private JTextArea getJtxtArea() {
if(jtxtArea == null) {
jtxtArea = new JTextArea();
jtxtArea.setBackground(new java.awt.Color(255,255,255));
jtxtArea.setFont(new java.awt.Font("Segoe UI",3,14));
jtxtArea.setWrapStyleWord(true);
jtxtArea.setLineWrap(true);
JScrollPane scroll = new JScrollPane(jtxtArea);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
}
return jtxtArea;
}
Can anyone tell me why i am not getting the scroll bar on the text view.Thanks in advance.
Note:I am using Eclipse Helios as the IDE and using Jigloo plugin in eclipse for the GUI.
Add the component scroll instead of the jtxtArea. In addition to that, you might also want to resize your JScrollPane.

Categories

Resources