How do I left align a JLabel inside a JPanel? - java

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);

Related

JPanel, problems with resizing

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.

using both JTabbedPane and JScrollPane

As you see the code, I would like to implement the 2nd tab with a text area, scrolling, and a button down there. This (JScrollPane scr = new JScrollPane(secondTab(panel2));) code was in the function, private static JTextArea secondTab(JPanel panel) before but I took it out of the function and put that back to MainFrame. Because the scroll and text area didn't show up. Now that I moved the code to mainframe the tex tarea and scroll are visible, but I'm struggling with making the button showing up in the 2nd tab. Do you guys have any idea?
public MainFrame(String username)
{
JTabbedPane tab = new JTabbedPane();
mainFrame.add(tab, BorderLayout.CENTER);
JPanel panel1 = new JPanel();
firstTab(panel1);
tab.add("babababa", panel1);
JPanel panel2 = new JPanel();
JScrollPane scr = new JScrollPane(secondTab(panel2));
JButton saveButton = new JButton("Save");
panel2.add(saveButton);
saveButton.setBounds(190, 280, 80, 40);
tab.add("hahaha", panel2.add(scr));
mainFrame.setBounds(200,200,500,400);
mainFrame.setVisible(true);
}
private static JTextArea secondTab(JPanel panel) {
panel.setLayout(null);
final JTextArea nameTextArea=new JTextArea();
nameTextArea.setBounds(10,10,440,270);
nameTextArea.setLineWrap(true);
return nameTextArea;
}
}
You should avoid use of null layout as this makes for very inflexible GUI's that while they might look good on one platform look terrible on most other platforms or screen resolutions and that are very difficult to update and maintain. Not only that, JScrollPanes do not work well when the viewport view (the component that you display inside of the JScrollPane) uses null layout. Not only that, if you set the bounds or even the preferred size of a JTextArea, it will not expand inside the JScrollPane and you won't see scrollbars.
Solution:
Avoid using null layouts like the plague.
Learn and use the layout managers: The Swing Layout Manager Tutorials
Never set a JTextArea's size or preferredSize. Instead consider setting its columns and rows.
For example:
import java.awt.BorderLayout;
import javax.swing.*;
public class MainFrame2 extends JPanel {
private JTabbedPane tabbedPane = new JTabbedPane();
private JTextArea textArea = new JTextArea(20, 40);
public MainFrame2() {
tabbedPane.add("Bahahahaha", new JPanel());
tabbedPane.add("TextArea Info", createTextAreaPane());
setLayout(new BorderLayout());
add(tabbedPane, BorderLayout.CENTER);
}
private JComponent createTextAreaPane() {
JPanel btnPanel = new JPanel();
btnPanel.add(new JButton("Save"));
JPanel textAreaPane = new JPanel(new BorderLayout());
textAreaPane.add(new JScrollPane(textArea), BorderLayout.CENTER);
textAreaPane.add(btnPanel, BorderLayout.SOUTH);
return textAreaPane;
}
private static void createAndShowGui() {
MainFrame2 mainPanel = new MainFrame2();
JFrame frame = new JFrame("Main Frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
It's better to build each tab panel in each method.
private JPanel buildFirstTab() {
// create new panel
// add components to the panel
// return the panel
}
private JPanel buildSecondTab() {
// create new panel
// create JScrollPane
// create JTextArea
// add text area to scrollpane by calling scrollpane.setViewPort method
// add scrollpane to the panel
// create button
// add button to the panel
// return the panel
}
public MainFrame(String username) {
JTabbedPane tab = new JTabbedPane();
mainFrame.add(tab, BorderLayout.CENTER);
tab.add("1st tab", buildFirstTab());
tab.add("2nd tab", buildSecondTab());
mainFrame.setBounds(200,200,500,400);
mainFrame.setVisible(true);
}
Adding to the previous answer, try to use the simple layout like BoxLayout (adds components on top of each other), or FlowLayout (adds components next to previous component), then learn more complex layout like GridBagLayout (so far, this is the best layout for me, most of the time).

Setting radio buttons to a background

I was trying to set Radio Button to a background in order to allow the user to select.
Here is the code ..
public class FirstWindow extends JFrame {
private JTextField search;
private JRadioButton author,title,both;
private ButtonGroup grp;
public FirstWindow() {
super("My App");
setLayout(new BorderLayout());
JLabel backGround = new JLabel(new ImageIcon("C:\\Users\\Kareem Abdo\\Desktop\\3.Jpg"));
backGround.setLayout(null);
add(backGround);
search = new JTextField("Search...");
search.setFont(new Font("Arial",Font.PLAIN,16));
search.setSize(150, 30);
search.setLocation(20, 20);
backGround.add(search);
author = new JRadioButton("Author",true);
author.setLocation(20, 25);
backGround.add(author);
title = new JRadioButton("Title",false);
title.setLocation(25, 25);
backGround.add(title);
both = new JRadioButton("Both",false);
both.setLocation(250, 250);
backGround.add(both);
grp = new ButtonGroup();
grp.add(author);
grp.add(title);
grp.add(both);
But the radio buttons doesn't appear on the screen!
JLabel (JLabel backGround = new JLabel) haven't implemented any LayoutManager, then you have to set the proper one, otherwise any JComponent added to JLabel isn't visible
maybe better could be start with Image (BufferedImage) painted in paintComponent to the JPanel (pre_implemented FlowLayout in API)

How to align two JButtons to be right aligned?

So currently my program shows only one of the buttons in the bottom right hand of the GUI. But I want to show both buttons in the bottom right hand corner. Any ideas how to set both buttons to the right corner? Here is my code so far:
import javax.swing.*;
import java.awt.*;
public class Other extends JFrame{
private static final long serialVersionUID = 1L;
public Other() {
super("Buttons");
final Container mainPanel = getContentPane();
mainPanel.setLayout(new BorderLayout());
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new BorderLayout());
JPanel inputPanel = new JPanel();
inputPanel.add(new JLabel("RANDOM TEXT HERE"));
inputPanel.add(new JLabel("RANDOM TEXT HERE"));
inputPanel.add(new JLabel("RANDOM TEXT HERE"));
JButton s = new JButton("first");
JButton l = new JButton("second");
buttonPanel.add(s,BorderLayout.LINE_END);
buttonPanel.add(l,BorderLayout.LINE_END); //<-- not working
mainPanel.add(inputPanel,BorderLayout.PAGE_START);
mainPanel.add(buttonPanel,BorderLayout.PAGE_END);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args){
Other o = new Other();
}
}
buttonPanel.setLayout(new FlowLayout(FlowLayout.TRAILING));
While the BorderLayout will only accept one component per layout area, FlowLayout will display as many as are added (within viewable bounds).
you can design GUI better and easy with Netbeans 7.1 .. you can align the swing components wherever you like and even make dependent on size of the frame ... you can get it here https://netbeans.org/downloads/index.html

Jpanel size after adding jlabel component

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.

Categories

Resources