jpanel

MouseListener/KeyListener not working (JPanel)

纵然是瞬间 提交于 2019-11-28 01:43:48
问题 I'm doing a little project that involves the mouse and key listeners in JPanel. Unfortunately, none of the methods are called when I use the mouse/keyboard. I have worked with JPanels/JFrame/JApplet and JComponents before. The code snippets are shown below: import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import java.io.File; import java.io.PrintWriter; import java.io.IOException; import java.io.FileNotFoundException; import java.awt.image

Make an added JPanel visible inside a parent JPanel

折月煮酒 提交于 2019-11-28 01:30:25
How to make an added JPanel visible inside a parent JPanel ? I am using Netbeans for designing my UI. I have a MainFrame.java , which contains two panels; namely headerPanel and bodyPanel . In headerPanel I have put three buttons,let it be button1 , button2 and button3 . Also I have created three separate files extending JPanel , name it panel1 , panel2 and panel3 . Then I added all my three panels inside bodypanel in MainFrame.java in constructor. bodyPanel.add(panel1); bodyPanel.add(panel2); bodyPanel.add(panel3); I want that on clicking the respective buttons only respective panels should

Update graph with JFreeChart and slider

与世无争的帅哥 提交于 2019-11-28 01:19:27
I have a time history for arrays describing the pressure along a pipe. So I have an array of pressure values along the length of the pipe for each delta t. I want to plot the pressures along the length of the pipe with JFreeChart and chose which delta t to plot with a slider, so that whenever the user moves the slider the graphic is updates with values from a different delta t. I'm also resetting the tile to be the pressure at the last portion of the pipe. What happens is that the title is updates, meaning the data is being properly updates, but the curve remains the same. I have read every

Incorrect behavior of JPanel#paintChildren(Graphics) when a JMenu is present?

爱⌒轻易说出口 提交于 2019-11-28 00:23:11
What I want to do: Create a JPanel 's subclass to draw a simple overlay on top of contained components. Why don't I use JLayeredPane ? See JComponent#isOptimizedDrawingEnabled() . When a JMenu is present in a JFrame , adding a JPanel with an overridden paintChildren(Graphics) method, an incorrect coordinate starting point is provided in the passed Graphics object, as observed with this code sample: import java.awt.Color; import java.awt.FontMetrics; import java.awt.Graphics; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import

Java JPanel inside JScrollPane?

杀马特。学长 韩版系。学妹 提交于 2019-11-27 23:34:10
I have a JFrame, in this JFrame I have a JPanel that I draw on, this Panel can be any size and so I placed it into a JScrollpane to let me scroll when the panel is larger than the window screen size. Unfortunately does not work as I expected: Making the JFrame window smaller than the JPanel size does not show scroll bars The JScrollPane size now seems locked to the size of the JPanel I have added to it, where as before it resized to the bounds of it's JFrame window (it still kinda does this but only vertically now?!) The JPanel seems to assume the size of the JScrollpane regardless of what I

add controls vertically instead of horizontally using flow layout

拟墨画扇 提交于 2019-11-27 23:24:43
I am adding checkboxes on JPanel in FlowLayout the checkboxes are being added horizontally. I want to add checkboxes vertically on the Panel. What is the possible solution? Sunil luitel I hope what you are trying to achieve is like this. For this please use Box layout. package com.kcing.kailas.sample.client; import javax.swing.BoxLayout; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.SwingUtilities; import javax.swing.UIManager; import javax.swing.WindowConstants; public class Testing extends JFrame { private JPanel jContentPane = null;

Java items appear only after the window is resize

醉酒当歌 提交于 2019-11-27 22:53:00
I have 2 JPanels in a frame. The first panel contains java items like buttons etc. The two buttons I added appears but the JSpinner appear just after I resize the window. I guess this will happen also with other items I will add. How could I solve this problem? import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.util.ArrayList; import java.util.List; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JSpinner; import javax.swing.SpinnerListModel;

Obtaining focus on a JPanel

ⅰ亾dé卋堺 提交于 2019-11-27 22:48:38
I have a JPanel inside a JFrame . I have registered a KeyListener , based on which I want to update the JPanel . The problem I am having is that I cannot get the focus on the JPanel and therefore my KeyListener won't work. I already know that the KeyListener is functional because I registered it with the JFrame and it worked fine. My code goes something like this at the moment: myFrame.setFocusable(false); myPanel.setFocusable(true); myPanel.addKeyListener(myKL); myFrame.add(myPanel); Has anyone encountered a problem like this before? Is there something I am missing in regards to this? P.S.: I

Convert a JPanel to an image in a JScrollPane

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-27 22:28:41
I want to convert an JPanel to an image. I used the following method: public BufferedImage createImage(){ int w = getWidth(); int h = getHeight(); BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); Graphics2D g = bi.createGraphics(); paint(g); return bi; } But the problem is that the JPanel is contained within a JScrollPane. So when I convert the jpanel to an image, the image contains only the parts visible in the jpanel and the parts that are hidden inside the scrollpane aren't contained in the image. Are there any solutions to get the full content of the JPanel into an

Export JPanel Graphics to .png or .gif or .jpg

霸气de小男生 提交于 2019-11-27 20:45:16
问题 I'm trying to develop some sort of paint using Java. I have a JComponent that is located inside of a JPanel. I already can draw lines and rectangles into that JComponent. Now, how can I export this drawings as an image (png, gif, jpg)? I tried this: BufferedImage b = new BufferedImage(1700,1100,BufferedImage.TYPE_INT_RGB); this.print(getGraphics()); try{ImageIO.write(b,"png",new File("test.png"));}catch (Exception e) {} But that only creates a .png file all black. Help!!! RESOLVED!!!