How to resize a JFile Chooser - java

I am designing a file browser using Java Swing, and here is what I have so far:
I have a JFileChooser in a panel, however it stays the same size when I reshape the window.
However, I want to make it look like this:
Is it possible to make the actual Browser box resize along with the form?
EDIT: I do not want a popup JFileChooser, the JFileChooser is INSIDE the Frame.

You don't need to add the file chooser to a panel - if you just initialize one and set it visible, it will automatically be resizable.
JFileChooser chooser = new JFileChooser();
chooser.setVisible(true);
int returnVal = chooser.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
//continue your code here
To incorporate the file chooser into the panel, try this:
JFrame frame = new JFrame();
JPanel panel = new JPanel(new BorderLayout());
JFileChooser chooser = new JFileChooser();
panel.add(chooser);
frame.add(panel);
frame.pack();
frame.setVisible(true);
I'm not sure how you used the BorderLayout before but this code works perfectly on my computer.

If you just add the file chooser panel to a panel it will retain its preferred size because by default a panel uses a FlowLayout.
Try adding the file chooser panel to the CENTER of a panel using a BorderLayout. Then hopefully the components will resize as you size the frame (assuming the file chooser panel uses appropriate layout managers).

Related

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

Using a JScrollPane in a JEditorPane

Hi so I'm struggling to apply a JScrollPanel within a JEditorPane.
I have a main content window (which has a scroll bar) and within that a JEditorPane that I would like to scroll slide horizontal and vertical. So far I have
JEditorPane monApp = new JEditorPane();
and have tried
ScrollablePanel panel = new ScrollablePanel();
panel.setScrollableWidth( ScrollablePanel.ScrollableSizeHint.FIT );
along with a few other things but couldn't seem to get any of them working.

Setting a .gif on JButton in Java (NetBeans)

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 :)

Set Icon to JApplet titlebar

I am using JApplet embedded in HTML. How can I set an icon or logo to the title bar of JApplet, just like we set icons to JFrame using setIconImage?
An applet has no title bar, so that is not possible. You could always try to have the applet set an icon for the web page itself.

Change JPanel at runtime with a button

Hello I have a main layout made in this way:
| |
Main pane |Menu pane|
| |
Now, menu pane is made just of button: clicking on a button switches main panel with another panel.
On click event is made this way:
public void actionPerformed(ActionEvent evt){
mainPanel = new MyNewPanel();
this.revalidate();
}
But, for some reason, main panel does not change!
You are not setting the main component of your container. You need to add your new panel and call validate() on that container.
Note, depending on your the layout of your container you may need to remove the currently visible component first.
CardLayout could manage all this for you.

Categories

Resources