I'm doing a mini-program in Java, that would manage files in the computer.
I tried put the JFileChooser.showSaveDialog() in the middle of the frame, by creating a panel and put it in the middle of the frame :
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(150,150) );
JFileChooser chooseFile = new JFileChooser();
chooseFile.showSaveDialog(panel);
frame.getContentPane().add(BorderLayout.CENTER,panel);
frame.setSize(400,400);
frame.setVisible(true);
But actually, when the showSaveDialog() command happens, the frame becomes not visible.
Can I change it?
It is not that the JFrame is not visible, but rather it was never set to be visible. I assume you want the JFrame to be visible when the showSaveDialog() is called. Due to the lack of detail, this is what I have to offer:
javax.swing.JFrame frame = new javax.swing.JFrame();
frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
javax.swing.JPanel panel = new javax.swing.JPanel();
panel.setPreferredSize(new java.awt.Dimension(150,150) );
javax.swing.JFileChooser chooseFile = new javax.swing.JFileChooser(); frame.getContentPane().add(java.awt.BorderLayout.CENTER,panel);
frame.setSize(400,400);
frame.setVisible(true);
chooseFile.showSaveDialog(panel);
EDIT: I reread your question and have the idea that you may want to place the JFileChooser on the panel for the question is ambiguous. You may have wanted this:
javax.swing.JFrame frame = new javax.swing.JFrame();
frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
javax.swing.JPanel panel = new javax.swing.JPanel();
panel.setPreferredSize(new java.awt.Dimension(150,150) );
javax.swing.JFileChooser chooseFile = new javax.swing.JFileChooser(); frame.getContentPane().add(java.awt.BorderLayout.CENTER,panel);
chooseFile.setPreferredSize(new java.awt.Dimension(400, 400));
frame.setSize(400,440);
frame.setVisible(true);
panel.add(chooseFile);<br><br>
The save dialog FileChooser is a modal dialog. This means that when we call chooseFile.showSaveDialog(panel); the file dialog is given focus and the user cannot interact with the panel until the dialog is closed. This is usually the behavior we want.
If we want to create some sort of file manager, then maybe you want to try adding the JFileChooser to your panel instead. Since JFileChooser extends component, you can do this, but it requires a bit more code and more of understanding of swing. This SO question addresses adding a JFileChooser to JPanel.
Related
I have written a java gui code for many options available on it. the gui is also set visible true but it doesn't show until I pick its border and drag them to resize the gui window. After manually resizing it, it shows everything. Also, the textlabels and the textfields and buttons are not in new lines, they are placed one after one. Please tell me whats wrong with that: here is a part of code:
public static void initGUI(){
JFrame fr = new JFrame();
Container cont = fr.getContentPane();
cont.setLayout( new FlowLayout( ) );
FlowLayout layout = new FlowLayout();
cont.setLayout(layout);
frame.setSize(200,300) ;
frame.setVisible(true) ;
JTextField tName = new JTextField(30);
JTextField tCNIC = new JTextField(15);
JLabel nameLabel = new JLabel("Name:");
JLabel cnicLabel = new JLabel("CNIC #:");
cont.add(nameLabel);
cont.add(tName);
cont.add(cnicLabel);
cont.add(tCNIC);
JButton Cancel = new JButton ("Canel" );
JButton OK = new JButton ("OK" );
savebtn.setPreferredSize(new Dimension(150, 50));
retbtn.setPreferredSize(new Dimension(150, 50));
cont.add(savebtn);
cont.add(retbtn);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
frame.setVisible(true) ;
The above statement should be invoked AFTER all the components have been added to the frame. So it should be the last statement in your method.
Also, you should be invoking:
frame.pack();
instead of setSize(), before making the frame visible so all the components are displayed at their preferred size.
frame.setVisible(true);
This Statement should be invoked in the last of adding other components to the Frame.
I've finished my store management program with many classes (many JForm panels actually. Thanks to people in this forum whom helped me so much).
I just need to call JPanel Login when I click Run Project.
Any idea how to call it? what code I have to insert to main project?
You cannot display JPanel independently.
So you have to create a JFrame and then add this JPanel object to this JFrame and then display the JFrame.
write these lines in your main method.
JFrame frame = new JFrame("Title");
frame.setSize(500,500);
frame.setLayout(new BorderLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); // as per your requirement.
frame.add(new Login(), BorderLayout.CENTER);
frame.setVisible(true);
it will work properly.
JFrame myFrame = new JFrame("MyTitle");
myFrame.setSize(width, height);
myFrame.setLocation(x, y);
myFrame.setContentPane(new Login());
myFrame.setVisible(true);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// if you want add your jcomponent
myFrame.getContentPane().add(yourJComponent);
try it!
If you want add JComponent(JButton)
JButton myBtn = new JButton("btn name");
myBtn.setLocation(x, y);
myFrame.getContentPane().add(myBtn);
If you want add JComponent(JText)
JText myText = new JText("text");
myText.setLocation(x, y);
myFrame.getContentPane().add(myText);
JComponent [http://docs.oracle.com/javase/7/docs/api/javax/swing/JComponent.html]
JFrame frame1 = new JFrame("frame");
frame1.setVisible(true);
frame1.setPreferredSize(new Dimension(800, 600));
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel Panel1 = new JPanel();
JPanel Panel2 = new JPanel();
frame1.add(Panel1,BorderLayout.SOUTH);
frame1.add(Panel2,BorderLayout.North);
How do i do something like this that if something happens the frame is blank
if(SOMETHINGHAPPENS)
{
//remove all panels from frame 1 so i have a blank frame
//now i want to add some new panels
}
Simple answer, don't.
Instead, use a CardLayout on the JFrame
This will allow you to setup a series of "views" which you can switch between as needed
Try this. Jframe.remove(JPanel). Then call JFrame.pack()
, and possibly JFrame.repaint() in order to refresh the graphics if necessary.
JPanel extends JComponent, so it should work just fine. Remember to substitute JFrame and JPanel in the code above with the names of the frames/panels you are using the methods on.
I am working with JDBC.
My Class has a JFrame with a JTabbedPane to display my JPanels with the UI's for my different methods.
On this panel I want to display a result set in a JTable with to additional columns of buttons.
This is all currently working when I display it on a new JFrame but not when I try to display it on the existing one. Could somebody please explain why.
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
contentPane.add(tabbedPane, BorderLayout.CENTER);
JPanel panel = new JPanel();
tabbedPane.addTab("Main", null, panel, null);
panel.setLayout(null);
JTable table = new JTable(model);
DisplayButtonColumn testWithButtons1 = new DisplayButtonColumn(table,
displayHandler.getColCount());
DisplayButtonColumn testWithButtons2 = new DisplayButtonColumn(table,
displayHandler.getColCount() + 1);
panel.add(new JScrollPane(table));
// JFrame f = new JFrame();
// f.setSize(1000, 500);
// f.getContentPane().add(new JScrollPane(table));
// f.setVisible(true);
panel.setLayout(null);
The problem is the null layout. Don't do that. Swing is designed to be used with layout managers and not doing that will lead to trouble almost without exception. Using absolute layout would require you to manually manage the bounds to the components, and you're not doing that. If you need something else than the default FlowLayout, just create one that matches your needs:
panel.setLayout(new BorderLayout());
(It would be also possible to add to JTabbedPane without the helper panel, but I suppose you want more components in the tab).
i am doing a small Gui in java. i am using setBounds methods to set the position of buttons etc on my JFrame , but problem is that when i use it with JPanel button is not visible on JFrame , and without JPanel its quite ok ,, see both the codes and please help me as i am beginner and facing these foolish problems .
This one is working fine
JFrame jframe = new JFrame("Working Fine");
jframe.setLayout(null);
JButton jbutton = new JButton("Position Test");
jbutton.setBounds(0, 0, 100, 100);
jframe.add(jbutton);
jframe.setSize(300,300);
jframe.setVisible(true);
Same code when i add Button to Jpanel then it does not work so whats wrong , please guide me
JFrame jframe = new JFrame("causing problem ");
jframe.setSize(300,300);
JPanel p = new JPanel();
jframe.setLayout(null);
JButton jbutton = new JButton("Position Test");
jbutton.setBounds(0, 0, 100, 100);
jframe.add(p);
p.add(jbutton);
p.setVisible(true);
//jframe.add(jbutton);
jframe.setVisible(true);
please help me in this small problem
You must get rid of the JPanel's layout, in order to set absolute positions:
p.setLayout(null);
The problem is that when you use absolute positioning, the JPanel component has no default size so does not appear. To get it to appear you could do
JFrame frame = new JFrame("No Problem");
JPanel panel = new JPanel() {
#Override
public Dimension getPreferredSize() {
return new Dimension(300, 300);
};
};
panel.setLayout(new FlowLayout(FlowLayout.CENTER));
JButton button = new JButton("Position Test");
panel.add(button);
frame.add(panel);
frame.pack();
frame.setVisible(true);
From Doing Without a Layout Manager
Although it is possible to do without a layout manager, you should use a layout manager if at all possible. A layout manager makes it easier to adjust to look-and-feel-dependent component appearances, to different font sizes, to a container's changing size, and to different locales.
The choice of layout manager will depend on how you wish to lay out the components.
See A Visual Guide to Layout Managers.