Frame Showing Problem - java

I have made one project which is showing the inventory of the stock of one store.
In that inventory the software should store data of the products with their images.
There is one problem...
Bcz of the lots of stock, the screen on which is image is loading taking a lot of time.
So, i thought i should give the frame in which there will be on label which will show the "Loading Software".
But now when i am setting visible = true for that frame, but bcz of that images screen class loading problem my frame is not showing correctly. I have put screen shot, now my code.
JFrame f;
try{
f = new JFrame("This is a test");
f.setSize(300, 300);
Container content = f.getContentPane();
content.setBackground(Color.white);
content.setLayout(new FlowLayout());
JLabel jl = new JLabel();
jl.setText("Loading Please Wait....");
content.add(jl);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}catch(Exception e){
e.printStackTrace();
}
initComponents();
try {
addInverntory = new AddInventoryScreen();
showstock = new showStock(); // this class will take big time.
mf = new mainForm();
f.setVisible(false);
}catch (Exception ex) {
ex.printStackTrace();
}
How Can show some message that, other class is loading or "Loading Software" kind of thing in this situation.
Just For the know....this class is not screen on which the image will load.

It's hard to answer this because it's not clear what the effects (Swing-wise) are of the calls to new AddInventoryScreen(); and new showStock();. You should only touch the UI that the user sees right at the end (when all the processing is done).
You should really spin off methods that will take a long time into their own Thread (see SwingWorker. There are alternatives for Java 5.0). That way, the UI won't be blocked while it's processing.
Maybe what you want is a Splash Screen?

Try calling validate(); and pack(); methods before calling f.setVisible(true);
Your code can be
validate();
pack();
f.setVisible(false);

I think one big problem in your code (maybe not the only one however) is the fact that you should use a different thread for long operations.
GUI operations (creating swing components, adding them to panels, changing labels...) are to be performed exclusively in the "EDT" and must be short (typically, less than 100ms or even 50ms).
Long operations can be easily done by another thread if you use the SwingWorker API (part of JDK 1.6).

Related

eclipse ide java not loading jpanels

so my problem is quite bizarre. So I am trying to write a java program in the eclipse ide. I have one JFrame and multiple JPanels added to it. You can navigate through the JPanels via buttons setting one panel to not be visible and another to be visible, you get the idea. However, after some time this started to occur: when I press a button the correct JPanel is being set to visible and I can see it, but it is completely empty. Sometimes its content is displayed correctly, but this is rather inconsistent.
As I said: sometimes this happens, sometimes it doesnt happen. I would start the program and close it 5 times and the 6th time it finally works. I don't think the code is necessary (as it is all correct because, after all, it does work sometimes), but if you want to see it feel free to ask. My eclipse workspace is on my C drive on an SSD and it is not too full. it has like 100 out of 250 gigs left. Thank you for your time and help in advance!
Edit:
So heres the code example.
Two of the JPanels Im trying to switch between:
The "main" panel
Var.menuPanel = new JPanel();
Var.jf1.add(Var.menuPanel);
Var.menuPanel.setBounds(0, 0, Var.screenwidth, Var.screenheight);
Var.menuPanel.setLayout(null);
Var.menuPanel.setVisible(true);
Var.menuPanel.revalidate();
Var.menuPanel.repaint();
This loads all fine.
The button to switch to the other panel:
Var.editCourse = new JButton("Edit");
Var.menuPanel.add(Var.editCourse);
Var.editCourse.setBounds(550, 120, 200, 50);
Var.editCourse.setVisible(true);
Var.editCourse.addActionListener(handler);
Var.editCourse.setBackground(Color.YELLOW);
The other JPanel + UI elements
Var.editCoursePanel = new JPanel();
Var.jf1.add(Var.editCoursePanel);
Var.editCoursePanel.setBounds(0, 0, Var.screenwidth, Var.screenheight);
Var.editCoursePanel.setLayout(null);
Var.editCoursePanel.setVisible(false);
Var.editCoursePanel.revalidate();
Var.editCoursePanel.repaint();
Var.editCourseToMenu = new JButton("Back");
Var.editCoursePanel.add(Var.editCourseToMenu);
Var.editCourseToMenu.setVisible(true);
Var.editCourseToMenu.setBounds(550, 400, 200, 50);
Var.editCourseToMenu.addActionListener(handler);
Var.editCourseSelection = new JComboBox<String>(JSONHandler.courses);
Var.editCoursePanel.add(Var.editCourseSelection);
Var.editCourseSelection.setBounds(550, 200, 200, 50);
Var.editCourseSelection.setVisible(true);
Methods.setArrowBounds(Var.editCourseSelection);
Var.editCourseSelection.setSelectedItem("Edit.");
Var.submitCourse = new JButton("Continue");
Var.editCoursePanel.add(Var.submitCourse);
Var.submitCourse.setBounds(550, 300, 200, 50);
Var.submitCourse.setVisible(true);
Var.submitCourse.addActionListener(handler);
The line in the AcionHandler:
else if(e.getSource() == Var.editCourse) {
MenuHandler.showEditCoursePanel();
}
The showEditCoursePanel methtod:
public static void showEditCoursePanel() {
Var.menuPanel.setVisible(false);
Var.editCoursePanel.setVisible(true);
}
Oh and also, sometimes when it doesnt load, there is the scrollbar of a JScrollPane on the side, which, as you can see, is never even mentioned.
Edit 2:
Okay, I found the source of the problem. When I add the raw JPanel to the JFrame and workt with that it all works fine, I cant scroll of course. But when I add the JScrollPane the newCoursePanel doesnt show up at all. Everything else is now working fluently, except for the panel with the scrollbar. Heres the code form the nwCoursePanel im talking about:
Var.newCoursePanel = new JPanel();
Var.newCoursePanel.setBounds(0, 0, Var.screenwidth, Var.screenheight);
Var.newCoursePanel.setLayout(null);
Var.newCoursePanel.revalidate();
Var.newCoursePanel.repaint();
Var.newCoursePane = new JScrollPane(Var.newCoursePanel);
Var.newCoursePane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
Var.newCoursePane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
Var.newCoursePane.setVisible(false);
Var.jf1.add(Var.newCoursePane);
I cant figure out how to get it working though. in to show the panel I just Var.menuPanel.setVisible(false) and Var.newCoursePane.setVisible(true).

Displaying a JFrame as the result of a JButton click?

I am trying to create a JPanel to display when a user clicks a button within my main JFrame. In Netbeans I first used the wizard to add a new JPanel to my project, I then used the GUI creator to fill in all the content. I am not trying to display the JPanel with the following code
private void m_jbShowSelAccResultsActionPerformed(java.awt.event.ActionEvent evt)
{
Account selAcc = getSelectedAccount();
if(selAcc != null)
{
AccountView accPanel = new AccountView(Account.getDeepCopy(selAcc));
accPanel.setVisible(true);
}
else
ShowMessage("Please select an account to view");
}
But nothing happens, no error is thrown and the JPanel is not shown. So I then changed the JPanel to a JFrame (Netbeans didn't complain). When I try again with the same code I receive the error GroupLayout can only be used with one Container at a time.
How can I display my JPanel/JFrame?
To change views within a Swing GUI, use a CardLayout as this is a much more robust and reliable way to do this.
Don't try to blindly "change a JPanel to a JFrame". It looks like you're just guessing here.
GroupLayout can't be reused as the error message is telling you. Likely this error comes from the point above. If you avoid trying to make a JFrame out of a JPanel, the error message will likely go away. As an aside, GroupLayout is not easily used manually, especially if you're trying to add components to an already rendered GUI.
So for instance, if your program had a JPanel say called cardHolderPanel, that used a CardLayout, this held by a variable say called cardLayout, and you've already added a "card" JPanel to this holder for accounts, say called accPanel, and if the accPanel had a method to set its currently displayed account, say setAccount(Accoint a), you could easily swap views by calling the CardLayout show(...) method, something like:
private void m_jbShowSelAccResultsActionPerformed(java.awt.event.ActionEvent evt) {
Account selAcc = getSelectedAccount();
if(selAcc != null) {
accPanel.setAccount(Account.getDeepCopy(selAcc));
cardLayout.show(cardHolderPanel, "Account View");
}
else {
showErrorMessage("Please select an account to view");
}
}

Animated GIF in Java Application is throwing MalformedURLException

I want to show an animated gif in my application. I followed the code found here: Show animated GIF
When I run my code I get the MalformedURLException error and my application will not run. Here is what I have that's not working.
The method that calls createVisuals():
private void defaultGUI() {
frame.setTitle("Class Map");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout(FlowLayout.LEADING, 0, 0));
frame.setBounds(0, 0, frameWidth, frameWidth/2);
frame.getContentPane().setBackground(Color.WHITE);
frame.setUndecorated(true);
try {
Visuals.createVisuals();
} catch (MalformedURLException e) {
e.printStackTrace();
}
frame.pack();
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setVisible(true);
}
The method that shows the gif:
public class Visuals {
public static void createVisuals() throws MalformedURLException{
URL cwURL = new URL("src\\images\\classmap_colorwheel-gif.gif");
Icon cwGif = new ImageIcon(cwURL);
JLabel cwIcon = new JLabel(cwGif);
GUI.frame.getContentPane().add(cwIcon);
}
What am I not doing correctly?
EDIT:
laksys pointed out that my URL constuction was wrong and gave a reference to fix it. The problem was that I was not giving the full file location alongside adding File: to the begining of the URL.
URL cwURL = new URL("src\\images\\classmap_colorwheel-gif.gif");
URL cwURL = new URL("File:C:/Users/01526460/Desktop/ClassMap/src/images/classmap_colorwheel-gif.gif");
This caused the exception to disappear, however the gif is not running properly. Only one frame of the gif sequence loads while the others only partially load. The gif is also looping faster than it should.
EDIT 2:
I figured out that the gif was not looping properly because of the way I made it, not because of Java. I used Photoshop CS6 to create a frame animation. When I ran the frame animation at 0 second delay between frames it looked fine in Photoshop. However, when the 0 second delay is interpreted through Java, the gif is actually trying to go 0 seconds between frames. If anyone else encounters this problem make sure the delay between your frames is not set to 0. Java does not automatically control the frame rate of gifs (like many browsers do).
I think your url construction is wrong. It may have protocol, host, port etc., please ref this

Java - Another JFrame On Same Window

What I want is to make a new JFrame, keeping my current JFrame visible, but not create a new window/program. I can't explain it well, so here is a picture of what I mean:
http://screensnapr.com/e/mkCMlm.png
Sorry if this is confusing in any way. Any help is appreciated.
You can try http://docs.oracle.com/javase/tutorial/uiswing/components/internalframe.html
Also you can try to use a dialog rather than frame for the new window.
If I understood correctly you want JInternalFrame which are a special component in swing that live inside a Container named Desktop. So if you want to have a behaviour like this:
You definitely need to have inside your JFrame a container named JDesktopPane, then you can add JInternalFrame inside this container like this:
MyInternalFrame frame = new MyInternalFrame();
frame.setVisible(true);
desktop.add(frame);
try {
frame.setSelected(true);
} catch (java.beans.PropertyVetoException e) {ex.printStackTrace();}
For more information you can see official oracle documentation or Java2SE code samples

How to close JFrame?

I have the main application frame. If user clicks the button he gets a new frame which has some chart in it. Now if I want to close that chart both chart and main application closes. How can I distinguish those two closings. Certainly I don't want my application to be closed after closing the frame in which I put chart.
Here's the code of the chart frame.
chartBttn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
final ShowChart sc = new ShowChart("Reserve Selection", getUtilExperiments() );
sc.pack();
RefineryUtilities.centerFrameOnScreen(sc);
sc.setVisible(true);
sc.setDefaultCloseOperation(ShowChart.DISPOSE_ON_CLOSE);
}
});
You can use the dispose() method. Or you can call setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); on the JFrame.
If I understood you correctly on your new JFrame build a method for your close button or X window button with:
setVisible(false);
dispose();
Otherwise please post your code on creating the new JFrame etc.
Certainly I don't want my application to be closed after closing the frame in which I put chart.
1) don't create lots of JFrames on the fly, create JFrame only once and re-use that for next usage(s), then
call only for visibility setVisible(false/true) with setDefaultCloseOperation(JFrame.NOTHING_ON_CLOSE)
or very simple workaround
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE)
2) or JDialog with setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE)
3) NOTICE: but in this case isn't possible close the current JVM instance, you have to add JButton or JMenu/JMenuItem which accelerate for System.exit(1)
simply use super.dispose(); for closing previous jframe

Categories

Resources