I'm still trying to learn how layout managers work. I made a Frame with two JPanels.
The first one contains a textArea with a boxLayout.
The second one contains a flow layout with a button.
I set the preferredSize of each panel accordingly, packed them, but got unexpected results.
import java.awt.*;
import javax.swing.*;
public class LayoutMgrTest
{
public static void main(String[] args)
{
TableBasic frame = new TableBasic();
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
frame.setVisible(true);
frame.getContentPane().setLayout(new GridLayout(2,1));
JPanel controlPane = new JPanel();
JPanel buttonPane = new JPanel();
controlPane.setLayout(new BoxLayout(controlPane, BoxLayout.PAGE_AXIS));
controlPane.setPreferredSize(new Dimension(200, 200));
controlPane.add(new JScrollPane(new JTextArea()));
buttonPane.setLayout(new FlowLayout(FlowLayout.LEFT));
buttonPane.setPreferredSize(new Dimension(100,20));
buttonPane.add(new JButton("Button1"));
buttonPane.add(new JButton("Button2"));
frame.getContentPane().add(controlPane, BorderLayout.NORTH);
frame.getContentPane().add(buttonPane, BorderLayout.SOUTH);
frame.setSize(new Dimension(500,500));
frame.pack();
}
}
Whatever I do, if I use a grid Layout, it seems to always allocate half of the available space to each control. I have been told that:
The height of each row is dependent on the height of each component
added in each row.
The buttonpane's height is 20. It's allocating much more than that to it:
What's wrong with this code?
I would like to leave the two JPanels intact please. It's easy to simply add the textbox and the buttons directly to the frame, but I need to do it with JPanels (because I will be adding borders and other things).
That's the result of using GridLayout as layout manager. Change it to BorderLayout:
frame.getContentPane().setLayout(new BorderLayout());
For example, this code (I changed a little as possible from the original):
import java.awt.*;
import javax.swing.*;
public class LayoutMgrTest
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
//frame.setVisible(true);
//frame.getContentPane().setLayout(new BorderLayout());
JPanel controlPane = new JPanel();
JPanel buttonPane = new JPanel();
controlPane.setLayout(new BoxLayout(controlPane, BoxLayout.PAGE_AXIS));
controlPane.setPreferredSize(new Dimension(200, 200));
controlPane.add(new JScrollPane(new JTextArea()));
buttonPane.setLayout(new FlowLayout(FlowLayout.LEFT));
buttonPane.setPreferredSize(new Dimension(100,40));
buttonPane.add(new JButton("Button1"));
buttonPane.add(new JButton("Button2"));
frame.add(controlPane, BorderLayout.NORTH);
frame.add(buttonPane, BorderLayout.SOUTH);
//frame.setSize(new Dimension(500,500));
frame.pack();
frame.setVisible(true);
}
}
Generates this frame:
I set the preferredSize of each panel accordingly,
That is another problem. You should NOT set the preferred size. That is the job of the layout manager. Just add your components to the panels and let the layout manager do its job.
Most compnents have a default preferred size. For some you need to give it a little tip.
For example when using a text area you would give a "suggested" preferred size by using:
JTextArea textArea = new JTextArea(rows, columns);
If you use LayoutManager, you should not set a size on a component except the frame.
the size for the components is calculated from the different layout managers.
you find more infos at http://download.oracle.com/javase/tutorial/uiswing/layout/howLayoutWorks.html
in your code, you can add the panel with the textarea to BorderLayout.CENTER. this should solve your problem. the component in BorderLayout.CENTER takes the whole space, except the space needed for the components in NORTH, EAST, SOUTH and WEST.
Related
In most of the GUI programs, when the user resizes it, the components of the program, such as text fields, buttons, etc. tend to increase or decrease their size depending on the decisions of the user. I'm trying to implement this idea into my GUI program. I'm a little bit lost about how I can do it. By the way, I created my program without the usage/help of the Eclipse Swing or Netbeans' GUI.
import javax.swing.*;
import java.awt.*;
import java.io.IOException;
public class CodeReviewerFrame extends JFrame {
EditorAreaPanel display = new EditorAreaPanel();
// FileOptionsPanel fileOptionsPanel = new FileOptionsPanel( display );
JPanel p = new JPanel();
JPanel panel = new JPanel();
public CodeReviewerFrame(String title) throws IOException {
super(title);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setPreferredSize(new Dimension(1500, 1000));
setLayout(new BorderLayout());
ImageIcon img = new ImageIcon("icon.png");
setIconImage(img.getImage());
p.setLayout(new BorderLayout());
p.add(new HomeOptionsPanel(display), BorderLayout.LINE_START);
p.add(new NewCommentPanel(display), BorderLayout.CENTER);
p.add(new CommentOptionsPanel(display), BorderLayout.LINE_END);
add(p, BorderLayout.PAGE_START);
panel.setLayout(new BorderLayout());
panel.add(new FileExplorerPanel(), BorderLayout.LINE_START);
panel.add(new FileOptionsPanel(display), BorderLayout.CENTER);
panel.add(new CommentShowPanel(display), BorderLayout.LINE_END);
add(panel, BorderLayout.CENTER);
pack();
setResizable(true);
setVisible(true);
/**
* Everything Under This is experimental
*/
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = 0;
constraints.gridy = 0;
//add ( fileOptionsPanel, constraints );
}
}
There are components on each Panel that have been added, such as buttons in HomeOptionsPanel, huge JTextArea in the center of FileOptionsPanel plus four buttons up on the JTextArea, etc. Should I use new Layout type, or commands known as "repaint/revalidate," or implement changeListener? And should I only implement the code to the JFrame, or do it for each of the JPanels?
The behaviour of your UI upon window resizing depends (also) on the Layout Manager you are using.
Some Layout Managers (like BorderLayout) resize the components when the windows is resized, while others (like FlowLayout) don't.
It is not clear what LM you are using inside your panels, but most likely your issue stands in there.
I have been working on this for hours. I honestly cannot figure it out. I have JTextArea's inside a JSplitPane which is inside a JPanel with a JButton and all that is put in my JFrame. I am using Layout managers. I have tried using pack(). I have tried using preferred sizes. Without the JPanel my button does not display in the proper location or switch buttons in other Tabs. With the JPanel it cuts off all my text, stops the scroll function(yes I have tried setting the TextAreas to always have horizontal and vertical scroll bars...does not solve the problem where text just stops wrapping for no apparent reason).
public static void main(String[] args) throws IOException
{
JFrame frame = new JFrame();
Deck blackjack = new Deck(Deck.TYPE[0]);
JTextArea textBlackjackUnshuffled = new JTextArea();
JTextArea textBlackjackShuffle = new JTextArea();
JButton shuffleButtonBlackjack = new JButton(new ImageIcon(ImageIO.read(new File("res/shuffle.png"))));
JToolBar toolBarBlackjack = new JToolBar("Blackjack");
JSplitPane splitPaneBlackjack = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
JPanel panel = new JPanel();
JTabbedPane tabbedPaneBlackJack = new JTabbedPane();
JTabbedPane tabbedPaneCanasta = new JTabbedPane();
JTabbedPane tabbedPanePinochle = new JTabbedPane();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
textBlackjackUnshuffled.setColumns(10);
textBlackjackUnshuffled.setLineWrap(true);
textBlackjackUnshuffled.setWrapStyleWord(true);
textBlackjackUnshuffled.setEditable(false);
textBlackjackUnshuffled.setFont(new Font("DejaVu Sans", Font.PLAIN, 100));
textBlackjackUnshuffled.append(blackjack.toString());
textBlackjackShuffle.setColumns(10);
textBlackjackShuffle.setLineWrap(true);
textBlackjackShuffle.setWrapStyleWord(true);
textBlackjackShuffle.setEditable(false);
textBlackjackShuffle.setFont(new Font("DejaVu Sans", Font.PLAIN, 100));
textBlackjackShuffle.append(blackjack.toString());
shuffleButtonBlackjack.setBorderPainted(false);
shuffleButtonBlackjack.setFocusPainted(false);
shuffleButtonBlackjack.setContentAreaFilled(false);
splitPaneBlackjack.add(new JScrollPane(textBlackjackUnshuffled));
splitPaneBlackjack.add(new JScrollPane(textBlackjackShuffle));
panel.add(splitPaneBlackjack, BorderLayout.CENTER);
panel.add(shuffleButtonBlackjack, BorderLayout.PAGE_END);
tabbedPaneBlackJack.addTab("Blackjack", panel);
frame.add(tabbedPaneBlackJack);
frame.setSize(new Dimension(Toolkit.getDefaultToolkit().getScreenSize()));
frame.setVisible(true);
}
You're adding the JScrollPanes to the panel in BorderLayout positions, but have not set the layout manager of panel to BorderLayout. In this situation, panel will be using JPanel's default layout manager, FlowLayout, a manager which is not smart enough to respect the scroll pane's preferred sizes.
Your code needs:
panel.setLayout(new BorderLayout());
I have a typical IDE style window with a top JMenuBar and JToolBar,
a large center console and a bottom status bar.
Here are the main parts of the code:
mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
JPanel topPanel = new JPanel();
. (JMenuBar and JToolBar)
.
mainPanel.add(topPanel, BorderLayout.NORTH);
cardPanel = new JPanel();
cardPanel.setLayout(cardLayout);
.
.
Dimension minDimension = new Dimension(680, 400);
Dimension maxDimension = new Dimension(750, 800);
JPanel centerPanel = new JPanel();
centerPanel.setMinimumSize(minDimension);
centerPanel.setPreferredSize(minDimension);
centerPanel.setMaximumSize(maxDimension);
centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.Y_AXIS));
centerPanel.add(cardPanel);
mainPanel.add(centerPanel, BorderLayout.CENTER);
mainPanel.add(statusBar, BorderLayout.SOUTH);
I want to be able to control the sizing of the whole window,
with a minimum and maximum size.
I thought I could have the 'centerPanel' JPanel as a BoxLayout,
so I could have some control over it, but as it is, I can't
control the sizing of the window at all.
I tried to make the 'mainPanel' use a BoxLayout, instead of a BorderLayout, but there
where too many issues.
Is the main BorderLayout causing it to "ignore" the sizing?
I know that the parts, other than the center, have some sizing
control, that's why I tried to use a BoxLayout in the center.
Is it possible to keep the BorderLayout, and get it to work,
with some other modifications, or would I need to switch to some other Layout Manager?
Thanks!
I want to have a resizable panel, that always has the top green panel of a fixed depth. i.e. all changes in height should effect the yellow panel only.
My code below is almost OK, except the green panel varies in size a little.
How do I do this?
Panel.setLayout(new BoxLayout(Panel, BoxLayout.Y_AXIS));
Panel.setAlignmentX(Component.LEFT_ALIGNMENT);
JPanel TopPanel = new JPanel();
TopPanel.setPreferredSize(new Dimension(80,150));
TopPanel.setVisible(true);
TopPanel.setBackground(Color.GREEN);
JPanel MainPanel = new JPanel();
MainPanel.setPreferredSize(new Dimension(80,750));
MainPanel.setVisible(true);
MainPanel.setOpaque(true);
MainPanel.setBackground(Color.YELLOW);
Panel.add(TopPanel);
Panel.add(MainPanel);
Your question didn't restrict the solution to a BoxLayout, so I am going to suggest a different layout manager.
I would attack this with a BorderLayout and put the green panel in the PAGE_START location. Then put the yellow panel in the CENTER location without a preferredSize call.
http://docs.oracle.com/javase/tutorial/uiswing/layout/border.html
Here is an SSCCE example of the solution:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TestPad extends JFrame {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().setLayout(new BorderLayout());
JPanel green = new JPanel();
green.setPreferredSize(new Dimension(80, 150));
green.setBackground(Color.GREEN);
JPanel yellow = new JPanel();
yellow.setBackground(Color.YELLOW);
frame.getContentPane().add(green, BorderLayout.PAGE_START);
frame.getContentPane().add(yellow, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
If you make your Panel use BorderLayout instead of BoxLayout and put TopPanel in BorderLayout.NORTH and MainPanel in BorderLayout.CENTER, then they will both resize horizontally, but only the MainPanel will resize vertically.
See the BorderLayout documentation
I have a code in java.
package interfaces;
import javax.swing.JPanel;
public class TabbedPaneDemo extends JPanel {
public TabbedPaneDemo() {
JTabbedPane pane = new JTabbedPane();
JPanel dashboardPanel = new JPanel();
dashboardPanel.add(new JLabel("Dashboard"));
// Add Dashboard Tab
pane.addTab("Dashboard", dashboardPanel);
JPanel transactionPanel = new JPanel();
transactionPanel.add(new JLabel("Transactions"));
// Add Transactions Tab
pane.addTab("Transactions", transactionPanel);
JPanel accountPanel = new JPanel();
accountPanel.add(new JLabel("Account"));
// Add Account Tab
pane.addTab("Account", accountPanel);
this.setLayout(new BorderLayout());
this.setPreferredSize(new Dimension(400, 200));
this.add(pane, BorderLayout.CENTER);
}
public static void main(String[] args) {
JPanel panel = new TabbedPaneDemo();
panel.setOpaque(true);
// panel.setBounds(12, 12, 45, 98);
JFrame frame = new JFrame("JTabbedPane Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(panel);
frame.pack();
frame.setVisible(true);
}}
and the output it's !
I want to change in this code to be like this.
I want to use an absolute layout (null).
I want to add menu, buttons and labels with these tabs ...
How can I do??
You should NOT use absolute layout. There is not reason to use absolute layout.
Instead you should use layout managers.
Maybe create a panel and add buttons to the panel. Then add the panel to the NORTH of the content pane. The tabbed pane would be added to the CENTER.
Read the Swing tutorial on Using Layout Managers.