layout-manager

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

jScrollPane can't add component

蓝咒 提交于 2019-11-28 01:26:15
I have a jScrollPane and a button on a form. The button adds a component to the jScrollPane . I'm using a FlowLayout with a center alignment to arrange the components within the jScrollPane . The first component has no problems appearing and is aligned perfectly. When I then hit the button again, nothing seems too happen. When I follow the debugger it shows that everything happens precisely as before. The code that's being executed when the button is clicked: jScrollPane.getViewport().add(new Component()); This is how I setup the FlowLayout on the Viewport of the jScrollPane : jScrollPane

Position on Screen Right Bottom

北战南征 提交于 2019-11-28 01:12:14
I need to position JFrame on my screen. But I can't make them appear on the right side of the screen bottom. Please can someone explain me how to position them, if you can describe how to do it, it would be great. Here is the code so far. //Gets the screen size and positions the frame left bottom of the screen GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice defaultScreen = ge.getDefaultScreenDevice(); Rectangle rect = defaultScreen.getDefaultConfiguration().getBounds(); int x = (int)rect.getMinX(); int y = (int)rect.getMaxY()- frame.getHeight(); frame

Use of overriding getPreferredSize() instead of using setPreferredSize() for fixed size Components

本秂侑毒 提交于 2019-11-28 00:44:47
I read some posts here and I started why some people do @Override public Dimension getPreferredSize() { return new Dimension(500, 500); } instead of setPreferredSize(new Dimension(500, 500)); Isn't the second one better because it creates only one Dimension object whereas the first one possibly creates several (even if it's not that much wasted memory)? Or am I wrong? Is there a difference at all? A big difference is how the value can change over time, and so the one you choose should be dependent on what you're wanting to do with the code. If you simply call setPreferredSize(new Dimension(500

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

Centering a JLabel in a JPanel

送分小仙女□ 提交于 2019-11-27 21:18:20
I have a panel derived from JPanel . I have a custom control derived from JLabel . I am attempting to center this custom JLabel on my panel. The only way I know to do this that will work is to use the a null layout ( setLayout(null) ) and then calculate the custom JLabel's setLocation() point so that it's in the right spot. The custom JLabel is physically moved from one panel to this panel in this app and I believe the location previously set in setLocation is affecting things. However when I set it to (0,0) the component goes up into the upper left corner. BorderLayout doesn't work because

Which Swing layout(s) do you recommend? [closed]

丶灬走出姿态 提交于 2019-11-27 19:48:15
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 2 years ago . There are 8 layout managers in the Java library alone, and then there's a bunch of 3rd party products as well. The other day, I tried to use SpringLayout and... well, it ain't working out for me. See my other question. So... if you have a Swing app to design, and you want your layout just so , what are your

GridBagLayout is not working

蹲街弑〆低调 提交于 2019-11-27 19:35:15
问题 this.rootComponent.setLayout(new GridBagLayout()); GridBagConstraints gbc=new GridBagConstraints(); //gbc.gridwidth=2; gbc.gridx=0; gbc.gridy=0; gbc.gridwidth=8; gbc.anchor=GridBagConstraints.FIRST_LINE_START; this.rootComponent.add(new JLabel("Test label 1"),gbc); gbc.gridx=8; gbc.gridy=12; gbc.gridwidth=GridBagConstraints.REMAINDER; gbc.anchor=GridBagConstraints.FIRST_LINE_START; this.rootComponent.add(new JLabel("Test label"),gbc); Want to format it like this. grey part shows the jpanel

How to get GridBagLayout to respect minimum size of a button or panel?

℡╲_俬逩灬. 提交于 2019-11-27 17:52:23
问题 In the following GridBagLayout code, I'm expecting the specified minimum size of JButton btn2 to be respected when the JFrame is resized to be made smaller. But when I make the JFrame smaller, the btn2 gets smaller than its minimum size and then vanishes. Can someone point me in the right direction of what I'm doing wrong? Maybe I have to set the minimum size of the JPanel that contains the buttons? Any help is appreciated, thanks! JFrame frame = new JFrame(); frame.setSize(400,300); frame

Using a JPanel with a null layout

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-27 17:01:03
问题 So I have a class called CalendarPanel that extends JPanel . It uses a null layout. How would I use CalendarPanel as a regular component? When I put it in another JPanel and then add it to a window, it disappears. It is only visible when I add it directly to a window. EDIT: And yes, I realize using a JPanel with a null layout is bad practice. CalendarPanel is actually someone else's code, and I'm trying to use it for my purposes without having to refactor it. 回答1: It is only visible when I