Working with frames and Java AWT - java

I am currently making a program with the AWT GUI and I'm running into a problem. I basically want an image in the top left hand corner of the screen, and a column of buttons on the right of the image. This isn't what's happening though. When I run the applet, I click a popup saying "Start Program" and then the picture I want is in the applet window itself and the column of buttons is in another window by itself. This is what it looks like:
Is there anyway to fix this so that the image and the buttons are in the same window?

Yeah. You're creating a frame but your graphic isn't inside the frame. Can't tell much without the code, but the AWT Tutorial at java.sun.com isn't bad on this stuff.
Okay, a little more (I haven't used AWT in a long time.)
Here's the couple of issues you have. A Frame is a kind of Window -- it wants to be a separate window with its own close button and so forth.
When you create your graphic, you have to tell it was component its parent is; you're somehow parenting it to the Applet. So you have some piece of code that looks like
add(myComponent);
in the context of the Applet as this.
public class myApplet extends Applet {
// lots of stuff here creating your canvas, putting the image in it
// and so forth. There's an example, see fn 1.
// When you're done, you have a component, call it myImage.
add(myImage);
}
You have a Frame, and you're adding your buttons to that.
public class MyFrame extends Frame {
add(new Button(...));
add(new Button(...));
}
You need to move the code that adds your Canvas into the Frame class in some method.
(WARNING: this is not complete Java code, I don't recall the names of the right methods offhand. Probably the init() method in the Applet, at least.
fn1. http://java.sun.com/developer/onlineTraining/awt/contents.html#simpleexample

Related

Paint hidden Java swing component

I've tried to paint component to PDF. I've got itextpdf 4.2 and everything works perfectly.
But this works only if I make visible the frame that I've tried to render.
The similar question that I've found is How to paint an invisible JFrame elsewhere? that has the same issue, but the solution wasn't provided in answer.
A little of code.
I've created a JFrame and insert main view that I want to render.
JFrame jframe = new ShowingFrame();
jframe.setPreferredSize(new Dimension(PDFHelper.getOriginalWidth().intValue(), PDFHelper.getOriginalHeight().intValue()));
jframe.setMinimumSize(new Dimension(PDFHelper.getOriginalWidth().intValue(), PDFHelper.getOriginalHeight().intValue()));
jframe.add(view);
jframe.setUndecorated(true);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setState(Frame.ICONIFIED);
jframe.setState(Frame.NORMAL);
//jframe.setVisible(true);
If I uncomment jframe.setVisible(true) than everything works.
But user will see this frame that I want to avoid.
So the question is: How to paint hidden control?
Inside Swing Component.java class all paint methods first check if the component is visible:
public void paint(Graphics g) {
if (isShowing()) {
// rest of the code...
}
}
I've tried to create inherit class ShowingFrame extends JFrame, that override isShowing and always return true. But this not helps to.
Swing (and the Java Graphics API) is optimized to stop rendering as soon as possible.
So the solution is to create a BufferedImage, get the Graphics instance from it and then call component.paint(g); with it.
Now you have a tab component. Try to get the content of the tab instead of rendering the tab itself. If that doesn't work, you can try to clone the tree of children, create a new JPanel, attach the children and render the result. But cloning can become tedious if the models don't behave well.
See this question for some code: Swing: Obtain Image of JFrame
Why do you want to paint something that is not visible? Your computer does not want to waste CPU cycles rendering graphics that can't be seen. In fact, there is a lot of computations done to see what parts of each window are visible and only paint the visible parts (the so called clip window).
If you want to paint something so you can use it later or save it you can always create a BufferedImage of the size you want and paint to that.
If I uncomment jframe.setVisible(true) than everything works. But user will see this frame that I want to avoid.
You can set the frame location so that it is not visible on the screen. Maybe something like:
frame.pack();
Dimension d = frame.getSize();
frame.setLocation(-d.witdh, 0);

Closing/Disposing JFrame that's been instantiated by a parent JFrame with an 'exit' button

I realize it's pointless to include a button that would do the exact same thing as the 'x' in the window, but I've already worked out the placement of my buttons on my GUI and found having the exit button made things much easier if nothing but a placeholder. And I like the practice.
Ok anyways moving on.
I have a parent JFrame (main class actually) that I'd like to keep running open the entire time the program is being run. This isn't my issue. My problem is when opening a child JFrame. I instantiate it in the main class (it's adding a panel component I created) and I just can't figure out how close said JFrame from within the Panel. Is there an easy way of doing so? I already have the WindowConstant set to Dispose on Close.
What I've done so far is created a method getExit() which returns a boolean value of true. I then have in the main class where the JFrame was instantiated an if/else if statement telling it to set the JFrame visible if exit is False, and dispose of it if true. Doesn't do anything. I'm guessing that's because either it's not bothering to check at all or I coded it poorly.
Any advice?
EDIT: Clarifying what my code is so far without posting it (600 lines of crap to get through). I have my main class Driver(). It's the fairly straight forward main JFrame 'form'.
Said class has several buttons to open up a new JFrame that performs a simple function. We'll name one of those classes (only have 3 total for the Secondary JFrames) Panel(int type) It extends JPanel.
I have a constructor set up depending that takes the int type and hides certain components in my Panel (tried to maximize the panel by combining similar functions). I have a button on the panel that is an exit button. But because the class itself is not a JFrame and does not instantiate itself, I can't dispose of it there. I have to find a way to do so in the main class.
That is my issue.
See Closing an Application. You can use the ExitAction for your JButton and it will be just like clicking the close button of the frame.

How to put JFrame into existing JPanel in Java Swing?

I have an open-source java swing application like this:
http://i47.tinypic.com/dff4f7.jpg
You can see in the screenshot, there is a JPanel divided into two area, left and right area. The left area has many text links. When I click the SLA Criteria link, it will pop-up the SLA Criteria window. The pop-up window is JFrame object.
Now, I'm trying to put the pop-up window into right area of the JPanel, so that means no pop-up window anymore, i.e. when I click the SLA Criteria link, its contents will be displayed at the right area of the JPanel. The existing content of the right area of JPanel will not be used anymore. The concept is just same like in the java api documentation page: http://docs.oracle.com/javase/6/docs/api. You click the link in the left frame, you'll get the content displayed at the right frame.
The example illustration is like this:
(note: it's made and edited using image editor, this is not a real screenshot of working application)
http://i48.tinypic.com/5vrxaa.jpg
So, I would like to know is there a way to put JFrame into JPanel?
I'm thinking of using JInternalFrame, is it possible? Or is there another way?
UPDATE:
Source code:
http://pastebin.com/tiqRbWP8 (VTreePanel.java, this is the panel with left & right area divisions)
http://pastebin.com/330z3yuT (CPanel.java, this is the superclass of VTreePanel and also subclass from JPanel)
http://pastebin.com/MkNsbtjh (AWindow.java, this is the pop-up window)
http://pastebin.com/2rsppQeE (CFrame.java, this is the superclass of AWindow and also subclass from JFrame)
Instead of trying to embed the frame, you want to embed the frame's content.
There is (at least) one issue I can see with this.
The menu bar is controlled by the frame's RootPane.
Create you're self a new JPanel. Set it's layout to BorderLayout.
Get the menu bar from the frame (using JFrame#getJMenuBar) and added to the north position of you new panel.
Get the frames ContentPane and add it to the center position of the panel.
There is undoubtedly countless other, application specific issues you will run into trying to do this...
No, you don't want to "put a JFrame into a JPanel" and your illustration above doesn't demonstrate this either. Instead it's showing a subordinate window on top of (not inside of) another window. If you absolutely need to display a new subordinate window, I'd recommend that you create and display a JDialog. The tutorials will explain how to do this, or if you get stuck post your code attempt and we'll help you work with this.
Edit 1
You state:
I need to convert from the pop-up window style into the jpanel content style. It's just like the java api documentation page style: docs.oracle.com/javase/6/docs/api When you click the text in left frame, it doesn't show any pop-up, right? The content is displayed at right frame directly. So that's basicly my goal. The source code is quite big. I will try to paste the source code if possible.
What you are looking for is to simply implement a MouseListener in a JList or JTable, and when responding to the click get the content based on the selection made. This has nothing to do with placing a JFrame in a JPanel and all to do with writing the correct program logic. Again, display it in a modal JDialog -- but that's all secondary to your writing the correct non-GUI logic. You're really barking up the wrong tree here. Forget about JFrames, forget about JPanels for the moment and instead concentrate on how you're going to extract the SLA Criteria data when it is clicked on.
Edit 2
I think I see what you're trying to do -- instead of JFrames and JDialogs, use JPanels and swap them using a CardLayout which would allow you to swap views.
I had skimming the source codes, I saw that the AWindow.java has internal panel (APanel.java) to hold the window's content, and it also has a public method to return the content panel object (getAPanel()). With this, I can use it for fetching the window's contents into other container.
Finally, I decided to use JTabbedPane in the right area of VTreePanel for displaying the pop-up window's contents.
You cannot put a Jframe into a JPanel. Instead you should try to create a separate panel that has functionalities like your JFrame and embed that into your JPanel.
Since you can put a JPanel into another JPanel but not a JFrame into another JPanel

JButton Sub-Class Not Displaying Button until Clicked/Transparency Error

I have run into yet another issue with my program. I have made several JButton sub-classes to do specifically what I need them to do. The problem is that the buttons don't show up until I either click where they are supposed to be or if I hover the mouse over them (when I had setRolloverEnabled() to true). I originally had them all set for setRolloverEnabled() to true. But I understood that when I did hover the mouse over top of them, it had an ugly blue outline of the button which I didn't like at all. So is there any way to make them visible without having to hover over them, or without having to click them?
I have a background on my JFrame (I sub-classed JPanel and overrode the paintComponent() method) allowing JFrame to maintain its role as a container). Also in Adobe Photoshop I have designed the buttons and on the outer edge it has some transparency, I saved the files as .png so the transparency would be kept, but when the buttons are placed in the frame, there is an ugly blue outline still where it should be transparent. Any help on that.
Any suggestions would be appreciated. Below is the code for one of my Button classes.
public class Button extends javax.swing.JButton {
//This Button class is not the AWT Button object.
//It is a custom class designed by me.
public Button(ImageIcon normal){
setRolloverEnabled(false);
setVisible(true);
setIcon(normal);
setSize(normal.getIconWidth(),normal.getIconHeight());
}
public Button(ImageIcon normal, ImageIcon rollover){
this(normal);
setRolloverIcon(rollover);
}
public Button(ImageIcon normal, ImageIcon rollover,ImageIcon selected){
this(normal,rollover);
setSelectedIcon(selected);
}
}
There is a good chance you are not creating your GUI in the Event Dispatch Thread. Swing painting is single threaded by design. If you try to draw your components (even if you dont do it on purpose), the results will be varied. There are well documented methods to properly instruct the jvm to paint your components, including SwingWorker and SwingUtilities.invokeLater(Runnable). Take a look at this tutorial to get more info.

JApplets and JDialog

I have a japplet which contains an "about" button, which when pressed creates a new JDialog. The idea is that when the user clicks on the about button in the applet they will get a popup window which displays information. This works perfectly fine when i test it from eclipse with the applet viewer, but when i test it in the html form, the button does nothing. The applet runs and works, but the button does not create a new window or respond in any way.
full disclosure: when i say JDialog i actually mean a class i created which extends JDialog and has a method public void paintComponent (Graphics g){ ~~~~ } ... and i also tried with that class extending JFrame instead and had the same problem, it worked in applet viewer but not in the browser.
now my question is, is my thinking wrong, am i going about creating a popup window for a japplet the wrong way, is there a different method i should probably try?
my code is very long, but if there's something you need to see from it to properly answer the question let me know.
Are you making sure to set the new popup window to visible? That could be your problem, or I've heard of problems with Google toolbar not allowing user-initiated popups to appear, but I think that problem was fixed (if you're running an old version of Google toolbar, it may still be an issue).
ok i figure out what the problem is, is an image problem, the DrawingBoard was using io and imageio to load some images.

Categories