jpanel

jpanel not displaying well with jframe set to gridbaglayout

淺唱寂寞╮ 提交于 2019-12-05 17:25:11
the program below is to position a jpanel at the top left conner of jframe with gridbaglayout but instead a very small box is displayed in center of jframe. when I set the layout of jframe to null, the jpanel displays fine. can someone tell me why the jpanel is compressed to the center of frame with gridbaglayout? i really need to use gridbag. please help import java.awt.*; import javax.swing.*; //swing package public class Major { //defining the constructor public Major() { JFrame maFrame = new JFrame("The main screen"); //creating main Jframe JPanel headPanel = new JPanel(); //creating the

JScrollPane doesn't work for my JPanel

不打扰是莪最后的温柔 提交于 2019-12-05 15:15:27
first of all I must say that I have checked these questions and didn't find my answer : 1 , 2 , 3 , 4 , 5 , 6 , 7 and many other questions like so also I have checked these tutorials and examples: 1 , 9 , 10 , 11 and many other sites. but I couldn't fix my problem. and this is the simple kind of my code: public class Question extends JFrame { public Question() { Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); setLayout(new BorderLayout()); setSize(d.width, d.height); setResizable(false); setDefaultCloseOperation(EXIT_ON_CLOSE); JPanel panel = new JPanel(); panel.setPreferredSize(new

How to make the background gradient of a JPanel

不羁岁月 提交于 2019-12-05 14:42:21
I want to know how to make background gradient which is in another JPanel. Many articles found in internet,but all of them had demostrated how to overide the paintComponent() of the JPanel not how to do for a jPanel which is inside it. I use Netbeans IDE. I created a new JPanel class and could overide its paintComponent(). I have another jpanel on it (dragged & dropped on to the parent JPanel). I want to make its background gradient. Here is how I tried for parent. It worked. How can I overide this for child jpanel ? public void paintComponent(Graphics g) { Graphics2D g2d = (Graphics2D) g;

Java Swing transparent JPanels problem

故事扮演 提交于 2019-12-05 12:47:01
I'm having A JLayeredPane where I add 3 JPanels. I make the JPanels transparent (no background is set and setOpaque(false)). I draw lines on the JPanels and only the line on the last JPanel which has been added is visible. The lines of the other JPanels are not visible through that top JPanel (even if I have added different zIndexes when adding them). Anybody who knows a solution for this? Why aren't they transparent? I have created a little test program (3 classes). (TestJPanel and TestJPanel1 draw a line but on a different position but I only see the line of the last added JPanel. I don't

How to implement MouseWheelListener for JPanel without breaking its default implementation?

淺唱寂寞╮ 提交于 2019-12-05 11:20:52
Simply; I have a JPanel inside a JScrollPane ; As expected; JScrollPane by default listens to MouseWheelEvent so that scrolling is working well for when the wheel is rotating and while the cursor is hovering over the JPanel . But After that; I just updated JPanel so that it implements MouseWheelListener , and I added this mouse wheel listener for the JPanel itself. @Override public void mouseWheelMoved(MouseWheelEvent e) { if (e.isControlDown()) { if (e.getWheelRotation() < 0) { System.out.println("mouse wheel Up"); } else { System.out.println("mouse wheel Down"); } } } The JPanel responds to

JScrollPane does not appear when using it on a JPanel

旧城冷巷雨未停 提交于 2019-12-05 11:15:22
I have been trying for hours to find a way to solve the issue, but I had no luck with that. Here is a sample code: import java.awt.BorderLayout; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.SwingUtilities; public class Example extends JFrame { private static final long serialVersionUID = 1L; public Example() { JPanel contentPane = (JPanel) getContentPane(); contentPane.setLayout(new BorderLayout()); JPanel panTop = new JPanel(new BorderLayout()); //JPanel panBottom = new JPanel

How to auto scroll to bottom in Java Swing

社会主义新天地 提交于 2019-12-05 10:54:31
I have a simple JPanel with a JScrollPane (with vertical scrollbar as needed) on it. Things get added to (or removed from) the JPanel and when it goes beyond the bottom of the panel, I want the JScrollPane to scroll down to the bottom automatically as needed or scroll up if some components go away from the panel. How shall I do this? I am guessing I need some kind of listener which gets called whenever the JPanel height changes? Or is there something as simple as JScrollPanel.setAutoScroll(true) ? camickr When you add/remove components for a panel you should invoke revalidate() on the panel to

Get shown component in JScrollPane

南楼画角 提交于 2019-12-05 10:13:57
I have a JScrollPane containing a JPanel. I fill this JPanel with many buttons. Is there any possibility to get the currently shown buttons? I know I can access the children of a JPanel via jpanel.getComponents() but those are all components in this pane; I want only the ones that are currently on screen. I assume that this container is already visible on the screen, then I suggest 1) to extract JViewPort from JScrollPane , 2) addChangeListener to JViewPort 3) each visible JComponent(s) returns Rectangle 4) and Rectangle#intersects returns Boolean value if is JComponent(s) visible or not in

How do I disable a JPanel so that it can't change size in Java

谁说我不能喝 提交于 2019-12-05 09:02:00
I have several JPanels in a layout and whenever I change the size of the JFrame manually with my mouse, it stretches the JPanels and makes it look messy. Is there a way to disable this? JPanel is not resizable by the user. If you are referring to JFrame, you can use yourFrame.setResizable(false); It's all about the LayoutManager you are using. Some LayoutManagers like BorderLayout always give extra space to children added in the center position. However, north, south, east, and west only allocated the preferred size to them. My favorite LayoutManager is TableLayout which is extremely easy to

How do you draw shapes on a JPanel, which is within another JPanel?

寵の児 提交于 2019-12-05 07:12:48
问题 I'm currently trying to draw shapes on a JPanel , which is within another JPanel , within a JFrame . I've searched Google and Youtube and found out how to draw shapes within a JFrame that has one panel, but have found nothing which can help me with what I'm doing. (maybe I'm not seeing something). Code I've seen so far: public class GameScreen { public void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.red); g.drawRect(100, 10, 30, 40); } public static void main