I have a central panel. This is my parent panel. I am adding 3 panels to the parent panel.
The panels are going to be stacked vertically. Like a title panel, then a middle panel, then a bottom panel. I just want to focus on my title panel. When I create a jlabel using text. The label shows and the panel borders stretches the entire width of the parent panel, which is what I want.
private JPanel titlePanel() {
String text = "<html><b><big><font color=#5C8C5C>Help Dialog</font></big></b></html>";
JLabel textLabel = new JLabel(text, JLabel.CENTER);
JPanel p = new JPanel();
p.setLayout(new BoxLayout(p, BoxLayout.LINE_AXIS));
p.add(textLabel);
p.setBorder(BorderFactory.createLineBorder(Color.black));
return p;
}
I am actually wanting to use a icon as the label and not html text. So make the changes to the code.
private JPanel titlePanel() {
Registry appReg = Registry.getRegistry(this);
ImageIcon ediLabelIcon = appReg.getImageIcon("ToolLabel.ICON");
JLabel textLabel = new JLabel(ediLabelIcon, JLabel.CENTER);
JPanel p = new JPanel();
p.setLayout(new BoxLayout(p, BoxLayout.LINE_AXIS));
p.add(textLabel);
p.setBorder(BorderFactory.createLineBorder(Color.black));
return p;
}
Now the label shows, but the border of the panel is only as wide as the label and not stretched out the width of the parent panel.
I am trying to figure out to extend the panel border the width of the parent panel and not just as wide as the label. This is the code for the parent panel.
private void createDialog() {
Component titlePanel = titlePanel();
Component verbiagePanel = verbiagePanel();
Component closeButtonPanel = closeButton();
setTitle("HELP Dialog");
centerPanel = new JPanel();
centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.PAGE_AXIS));
centerPanel.setPreferredSize(new Dimension(600, 300));
centerPanel.add(titlePanel);
centerPanel.add(Box.createRigidArea(new Dimension(0, 10)));
centerPanel.add(verbiagePanel);
centerPanel.add(Box.createHorizontalGlue());
centerPanel.add(Box.createRigidArea(new Dimension(0, 10)));
centerPanel.add(closeButtonPanel);
getContentPane().add(centerPanel);
this.pack();
}
Using HTML in JLabel text switched the mechanism which calculate preferred size fot JLabel.
Now I can't explain it in detail, but if you change creating title label to
JLabel textLabel = new JLabel("<html></html>", ediLabelIcon, JLabel.CENTER);
your label will be stretched out to parent panel width.
Or you may choose another layout manager such as GridBagLayout. With GridBagLayout you can force stretch any component to its parent width.
Related
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 want to create a JTabbedPane, but I want to create the headers manually, I created a JPanel and JLabel where I put my Icon and background color, but I don't know how to add it to the tabbed pane.
The tabbed pane has 4 panels:
JPanel header = new JPanel();
header.setSize(100, 50);
header.setBackground(Color.red);
JLabel icon_Label = new JLabel(Icon);
icon_Label.setText("header 1");
jTabbedPane1.getComponentAt(1).*************?!
i find it
JPanel panel = new JPanel();
panel.setBackground(Color.BLUE);
JLabel title = new JLabel("OK");
title.setForeground(Color.RED);
title.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/icons8-boƮte-pleine-64.png")));
title.setText("GESTION A");
panel.add(title);
jTabbedPane1.setTabComponentAt(2, panel);
but there is space in the left and right of the header !
I am trying to build a status bar for my login dialog box but the label doesn't align to the left of the status panel. Here is my code.
public class LoginDialog extends JDialog {
private static final long serialVersionUID = 1L;
protected JLabel lblTopSpace = null;
protected JPanel loginPanel = null;
protected JPanel statusPanel = null;
public LoginDialog(String title) {
super((Dialog)null);
this.setTitle(title);
Initialize();
}
protected void Initialize() {
lblTopSpace = new JLabel("Login into Bookyard");
lblTopSpace.setForeground(this.getBackground());
loginPanel = new LoginPanel();
statusPanel = new JPanel();
statusPanel.setBorder(new BevelBorder(BevelBorder.LOWERED));
statusPanel.setSize(this.getWidth(), 50);
JLabel lblStatus = new JLabel("Status");
lblStatus.setFont(new Font("Verdana", Font.PLAIN, 12));
lblStatus.setHorizontalAlignment(SwingConstants.LEFT);
statusPanel.add(lblStatus);
this.setLayout(new BorderLayout());
Container container = this.getContentPane();
container.add(lblTopSpace, BorderLayout.NORTH);
container.add(loginPanel, BorderLayout.CENTER);
container.add(statusPanel, BorderLayout.SOUTH);
this.pack();
}
}
Here is what it looks like presently.
What am I missing?
Your label is inside a panel that is inside the contentpane of the dialog. So the label is managed with the layout of its parent panel. But you don't set any particular layout for it, then it's a FlowLayout, and your label is then centered in it with a size the minimal one required to let text appear. Then the label is left aligned in its own area, but this one is centered in the panel.
Either change the layout of the panel to let the label extends in it (add a BorderLayout and set the label in north, center or south of it), or remove the panel that seems not useful (and let the label extends in the south of the contentpane.
statusPanel.setLayout(new BorderLayout());
statusPanel.add(lblStatus,BorderLayout.SOUTH);
or
container.add(lblStatus,BorderLayout.SOUTH);
The code below adds the label to the left south of the panel, and when I use set location with the label, the position does not change. Is there a way to make the label be in the center south of the panel without the need for an extra panel?
EDIT: the JFrame has a BorderLayout and adds the panel to CENTER
JPanel pnl = new JPanel();
pnl.setPreferredSize(new Dimension(500,500));
JLabel lbl = new JLabel("label");
pnl.setLayout(new BorderLayout());
pnl.add(lbl, BorderLayout.SOUTH);
It seem you need to set text align of label to center panel?
If so, try this:
JPanel pnl = new JPanel();
pnl.setPreferredSize(new Dimension(500, 500));
JLabel lbl = new JLabel("label", SwingConstants.CENTER); //Set text align
pnl.setLayout(new BorderLayout());
pnl.add(lbl, BorderLayout.SOUTH);
lbl.setBackground(Color.red);
lbl.setOpaque(true); //Test background
getContentPane().add(pnl);
Result:
I'm a newbie.
Trying to make auto resize border. I made border on my frame with 2 panels. I added panels with border into first panel.
I want border which retreated from all edges. In this border panel I also added text panel and button.
When I expand the window, or resize it panel with border is resizing too. But there is not indents from edges when I am using BorderLayout.
public class App {
private JFrame frame;
private JPanel panel;
private JPanel panel_1;
private JTextField textField;
private JButton addBtn;
public static void main(String args[]) {
App app = new App();
app.initialize();
app.frame.pack();
app.frame.setVisible(true);
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
frame.getContentPane().add(panel, BorderLayout.NORTH);
panel.setLayout(new BorderLayout(0, 0));
panel_1 = new JPanel();
panel_1.setPreferredSize(new Dimension(784, 40));
panel_1.setBorder(new LineBorder(new Color(0, 0, 0)));
panel.add(panel_1, BorderLayout.CENTER);
textField = new JTextField();
textField.setPreferredSize(new Dimension(6, 24));
panel_1.add(textField);
textField.setColumns(50);
addBtn = new JButton("Add");
addBtn.setPreferredSize(new Dimension(70, 24));
panel_1.add(addBtn);
}
}
This is with BorderLayout - http://snag.gy/S43C2.jpg.
Also I tried with FlowLayout in panel - http://snag.gy/ndjDG.jpg
Can you help me please?
The problem is that because you set the border on a panel that you add into BorderLayout.NORTH. When you resize the window, BorderLayout.NORTH section will only resize horizontally, that's why the border will not be resized correctly.
public static void main(String args[]) {
JavaApplication11 app = new JavaApplication11();
app.initialize();
app.frame.pack();
app.frame.setVisible(true);
}
private JFrame frame;
private JPanel panel;
private JTextField textField;
private JButton addBtn;
private void initialize() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
frame.add(panel);
Border border = new CompoundBorder(new EmptyBorder(5, 10, 15, 20), new LineBorder(Color.BLACK));
panel.setBorder(border);
textField = new JTextField(50);
panel.add(textField);
addBtn = new JButton("Add");
panel.add(addBtn);
}
I'm assuming that this is the GUI you're trying to create.
To create this GUI, you need to use multiple JPanels with more than one Swing layout manager.
Here's the hierarchy of Swing components I would use.
JFrame - border layout
JPanel - main panel, border layout
JPanel - text, button panel, border layout, border north
JTextField - border center
JButton - border east
JScrollPane - border center
JTable
JPanel - button panel, flow layout, border south
JButton (3)
You get the spacing by setting an empty border on the JPanels and JScrollPane. The empty border can be as wide as you wish.