How can I get rid of that gray box?
This is what I'm talking about:
i would really appreciate if you could help me out
Full code here: http://pastebin.com/nrpCTjvV
public final void initUI() {
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setBorder(new EmptyBorder(new Insets(90, 155, 40, 60)));
JButton NewGame = new JButton ("New Game!");
JButton Highscore = new JButton("Highscore");
JButton Credits = new JButton ("Credits");
JButton Website = new JButton ("Website");
JButton Exit = new JButton ("Exit");
panel.add(NewGame);
panel.add(Box.createRigidArea(new Dimension(0, 5)));
panel.add(Highscore);
panel.add(Box.createRigidArea(new Dimension(0, 5)));
panel.add(Credits);
panel.add(Box.createRigidArea(new Dimension(0, 5)));
panel.add(Website);
panel.add(Box.createRigidArea(new Dimension(0, 5)));
panel.add(Exit);
final ButtonGroup entreeGroup = new ButtonGroup();
JRadioButton radioButton;
panel.add(radioButton = new JRadioButton("Music1"));
radioButton.setActionCommand("Music1");
entreeGroup.add(radioButton);
panel.add(radioButton = new JRadioButton("Music2"));
radioButton.setActionCommand("Music2");
entreeGroup.add(radioButton);
panel.add(radioButton = new JRadioButton("No Music", true));
radioButton.setActionCommand("No Music");
entreeGroup.add(radioButton);
add(panel);
pack();
setTitle("Title");
JLabel background = new JLabel(new ImageIcon("background.png"));
add(background);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(false);
setSize(400, 400);
}
add(panel);
pack();
setTitle("Title");
JLabel background = new JLabel(new ImageIcon("background.png"));
add(background);
The default layout manager for a JFrame is a BorderLayout. When you add a component without specifying a constraint the component is added to the CENTER. You can't add multiple components to a single location.
Instead you need to use a different component as the background. Then you add your panel to this component. Check out Background Panel. Then code would be something like:
Background background = new BackgroundPanel(...);
background.add(panel);
add(background);
setResizable(false);
pack();
...
Related
I want to align Labels and a Panel (containing Buttons) to the left inside a vertical BoxLayout.
As long as I don't add the panel to the BoxLayout everything is aligned to the left perfectly, but adding it screws everything up.
import javax.swing.*;
import java.awt.*;
public class BoxLayoutDemo{
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JPanel five = new JPanel();
JButton plus = new JButton("+");
JButton minus = new JButton("-");
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
Font testFont = new Font("Arial", Font.BOLD, 20);
JLabel label1 = new JLabel("Label1");
label1.setFont(testFont);
JLabel label2 = new JLabel("Label2");
label2.setFont(testFont);
five.setLayout(new BoxLayout(five, BoxLayout.X_AXIS));
plus.setMinimumSize(new Dimension(30, 30));
plus.setMaximumSize(new Dimension(30, 30));
minus.setMinimumSize(new Dimension(30, 30));
minus.setMaximumSize(new Dimension(30, 30));
five.add(plus);
five.add(minus);
panel.add(label1);
panel.add(five);
panel.add(label2);
panel.setOpaque(true);
panel.setBackground(Color.red);
frame.setMinimumSize(new Dimension(200, 200));
frame.getContentPane().add(panel, BorderLayout.EAST);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
Your components need to have the same "x alignment":
label1.setAlignmentX(Component.LEFT_ALIGNMENT);
label2.setAlignmentX(Component.LEFT_ALIGNMENT);
five.setAlignmentX(Component.LEFT_ALIGNMENT);
Read the section from the Swing tutorial on Fixing Alignment Problems for more information.
You can use invisible components as fillers.
private static Box leftAlignedInHorizontalBox(Component component) {
Box box = Box.createHorizontalBox();
box.add(component);
box.add(Box.createHorizontalGlue());
return box;
}
and then:
panel.add(leftAlignedInHorizontalBox(label1));
I want to add labels and buttons above and below the border layout. How can I do that? Here is what I did:
import java.awt.*;
import javax.swing.*;
class homework{
public static void main(String[] args) {
JFrame frame= new JFrame("border layout");
frame.setVisible(true);
JLabel label=new JLabel("Container of BorderLayout");
JButton button1 = new JButton("NORTH");
JButton button2 = new JButton("SOUTH");
JButton button3 = new JButton("EAST");
JButton button5 = new JButton("CENTER");
JButton button4 = new JButton("WEST");
JPanel panel1=new JPanel();
JPanel panel2=new JPanel();
frame.add(panel2);
label.setLayout(new FlowLayout(0));
panel2.add(label);
panel1.setLayout(new BorderLayout());
panel1.add(button1,BorderLayout.NORTH);
panel1.add(button2,BorderLayout.SOUTH);
panel1.add(button3,BorderLayout.EAST);
panel1.add(button4,BorderLayout.WEST);
panel1.add(button5,BorderLayout.CENTER);
frame.add(panel1);
frame.pack();
}
}
Above and below of border layout, set new 2 containers (for example JPanel) and make them flow layout. enter image description here
JPanel panel = new JPanel();
frame.getContentPane().add(panel, BorderLayout.NORTH);
JRadioButton rdbtnNewRadioButton = new JRadioButton("New radio button");
panel.add(rdbtnNewRadioButton);
JRadioButton rdbtnNewRadioButton_1 = new JRadioButton("New radio button");
panel.add(rdbtnNewRadioButton_1);
JPanel panel_1 = new JPanel();
frame.getContentPane().add(panel_1, BorderLayout.SOUTH);
JLabel lblNewLabel = new JLabel("New label");
panel_1.add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("New label");
panel_1.add(lblNewLabel_1);
add something like that before frame.pack(); code.
Actually what do you mean by above and below? Do you mean north and south? If It is you should have something like this enter image description here
and you should write code this way
import java.awt.*;
class homework{
public static void main(String[] args) {
JFrame frame= new JFrame("border layout");
frame.setVisible(true);
JLabel label=new JLabel("Container of BorderLayout");
JButton button3 = new JButton("EAST");
JButton button5 = new JButton("CENTER");
JButton button4 = new JButton("WEST");
JPanel panel1=new JPanel();
JPanel panel2=new JPanel();
frame.getContentPane().add(panel2);
label.setLayout(new FlowLayout(0));
panel2.add(label);
panel1.setLayout(new BorderLayout());
panel1.add(button3,BorderLayout.EAST);
panel1.add(button4,BorderLayout.WEST);
panel1.add(button5,BorderLayout.CENTER);
frame.getContentPane().add(panel1);
JPanel panel = new JPanel();
panel1.add(panel, BorderLayout.NORTH);
JLabel lblNewLabel = new JLabel("New label");
panel.add(lblNewLabel);
JRadioButton rdbtnNewRadioButton = new JRadioButton("New radio button");
panel.add(rdbtnNewRadioButton);
JPanel panel_1 = new JPanel();
panel1.add(panel_1, BorderLayout.SOUTH);
JLabel lblNewLabel_1 = new JLabel("New label");
panel_1.add(lblNewLabel_1);
JRadioButton rdbtnNewRadioButton_1 = new JRadioButton("New radio button");
panel_1.add(rdbtnNewRadioButton_1);
frame.pack();
}
}
In two conditions, you should add two containers into your code and make them flow layout.
I currently have a JFrame to start fullscreen, inside this jframe i have a jpanel, this jpanel includes a vertical scrollpane. Now if i resize my jframe vertically it just kinda removes the bottom part of the jpanel. Is there any way to just shrink the jscrollpane.
im currently using flowlayout for the jframe,
Scrollbar appear automatically when the preferred size of the components added to the scroll pane area greater than the size of the scroll pane.
The FlowLayout will wrap components to a new row, but it always gives the preferred size as the size required to fit the components on a single row, so the preferred height will never change.
To solve this problem you can use the Wrap Layout which simple extend FlowLayout to recalculate the preferred size when wrapping occurs.
The JPanel consists of 3 other panels, a top panel, a scrollpane in the middle and a botpanel. The top and bot panel are just button and checkboxes and stuff
private void initPane() {
createFolderCompPanel();
createBotPanel();
createTopPanel();
createScrollPane();
createTotalPanel();
add(totalPanel);
}
private void createFolderCompPanel() {
//Create folderCompPanel
folderCompPanel = new JPanel();
folderCompPanel.setLayout(new BoxLayout(folderCompPanel, BoxLayout.Y_AXIS));
folderCompPanel.add(Box.createVerticalGlue());
}
private void createTotalPanel() {
//Create TotalPanel
totalPanel = new JPanel();
totalPanel.setLayout(new BoxLayout(totalPanel, BoxLayout.Y_AXIS));
totalPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
totalPanel.add(topPanel);
totalPanel.add(scrollPane);
totalPanel.add(botPanel);
}
private void createScrollPane() {
//Create ScrollPane
scrollPane = new JScrollPane();
scrollPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setViewportBorder(BorderFactory.createEtchedBorder());
scrollPane.getVerticalScrollBar().setUnitIncrement(6);
}
private void createBotPanel() {
//Create BotPanel
botPanel = new JPanel();
botPanel.setLayout(new BoxLayout(botPanel, BoxLayout.X_AXIS));
//AddButton
addButton = new JButton("Add");
addButton.setEnabled(false);
addButton.addActionListener(this);
//SaveButton
saveButton = new JButton("Save");
saveButton.setEnabled(false);
saveButton.addActionListener(this);
//CancelButton
cancelButton = new JButton("Cancel");
cancelButton.setEnabled(false);
cancelButton.addActionListener(this);
lblTotalLength = new JLabel("Total Length: " + totalLength);
botPanel.add(Box.createRigidArea(new Dimension(10, 0)));
botPanel.add(addButton);
botPanel.add(Box.createRigidArea(new Dimension(10, 0)));
botPanel.add(lblTotalLength);
botPanel.add(Box.createHorizontalGlue());
botPanel.add(saveButton);
botPanel.add(Box.createRigidArea(new Dimension(10, 0)));
botPanel.add(cancelButton);
botPanel.add(Box.createRigidArea(new Dimension(10, 0)));
}
private void createTopPanel() {
//Create TopPanel
topPanel = new JPanel();
topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.X_AXIS));
//create deletedisplay button
deleteDisplayButton = new JButton("Delete Display");
deleteDisplayButton.addActionListener(this);
deleteDisplayButton.setEnabled(false);
//create displaybox
displayBox = new JComboBox();
displayBox.addActionListener(this);
displayBox.addItem("<None>");
for (String s : connect.getAllDisplays()) {
displayBox.addItem(s);
}
displayBox.setMaximumSize(displayBox.getPreferredSize());
//create newdisplay button
newDisplayButton = new JButton("New Display");
newDisplayButton.addActionListener(this);
topPanel.add(Box.createRigidArea(new Dimension(10, 0)));
topPanel.add(displayBox);
topPanel.add(Box.createHorizontalGlue());
topPanel.add(newDisplayButton);
topPanel.add(Box.createRigidArea(new Dimension(5, 0)));
topPanel.add(deleteDisplayButton);
topPanel.add(Box.createRigidArea(new Dimension(10, 0)));
}
this is the panel i add to the jframe
public GuiConstructor(){
super(APPLICATION_NAME);
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
this.setMinimumSize(new Dimension(630, 600));
setLayout(new FlowLayout(FlowLayout.LEFT));
LoopControlWindow folderSearch = new LoopControlWindow(connect, this);
add(folderSearch);
pack();
setLocationRelativeTo(null);
setResizable(true);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
Here is my code:
final JTextArea textArea = new JTextArea();
textArea.setFont(new Font("MS UI Gothic", Font.PLAIN, 13));
textArea.setLineWrap(true);
textArea.setBounds(77, 310, 474, 136);
//contentPane.add(textArea); (edited...still the same problem persists..)
JScrollPane sbrText = new JScrollPane(textArea);
sbrText.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
contentPane.add(sbrText);
When ever I try with this,the text area is not visible..(I am using Eclipse's Window Builder plugin and layout as "Absolute Layout")..
Here I have done, the same thingy using Nested Layout, have a look at the code example :
import java.awt.*;
import javax.swing.*;
public class WelcomeExample
{
private JPanel headerPanel;
private JButton logoutButton;
private JPanel leavePanel;
private JRadioButton casualRButton;
private JRadioButton specialRButton;
private JRadioButton sickRButton;
private JRadioButton privilegeRButton;
private ButtonGroup radioButtonGroup;
private JTextField leaveDaysField;
private JButton checkLeaveButton;
private JTextArea notesArea;
private JScrollPane notesScroller;
private JButton applyLeaveButton;
private String headerText = "<html><body><h1><font " +
"color=\"red\">Welcome : </font><font color" +
"=\"blue\">Code Zero</font></h1></body></html>";
private void displayGUI()
{
JFrame frame = new JFrame("Welcome");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel contentPane = new JPanel();
contentPane.setLayout(new BorderLayout(5, 5));
contentPane.setBorder(
BorderFactory.createEmptyBorder(5, 5, 5, 5));
headerPanel = getHeaderPanel();
leavePanel = getLeavePanel();
contentPane.add(headerPanel, BorderLayout.PAGE_START);
contentPane.add(leavePanel, BorderLayout.CENTER);
contentPane.add(getApplyPanel(), BorderLayout.PAGE_END);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel getHeaderPanel()
{
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout(5, 5));
panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JLabel headerLabel = new JLabel(headerText, JLabel.CENTER);
JPanel buttonPanel = new JPanel();
logoutButton = new JButton("Logout");
buttonPanel.add(logoutButton);
panel.add(headerLabel, BorderLayout.CENTER);
panel.add(buttonPanel, BorderLayout.LINE_END);
panel.add(new JSeparator(
SwingConstants.HORIZONTAL), BorderLayout.PAGE_END);
return panel;
}
private JPanel getLeavePanel()
{
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout(5, 5));
panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JPanel leaveHeaderPanel = new JPanel();
leaveHeaderPanel.setLayout(new GridLayout(0, 1, 5, 5));
leaveHeaderPanel.setBorder(
BorderFactory.createTitledBorder("Choose a leave type : "));
JPanel leaveTypePanel = new JPanel();
leaveTypePanel.setLayout(new FlowLayout(
FlowLayout.LEFT, 5, 5));
casualRButton = new JRadioButton("Casual Leave");
specialRButton = new JRadioButton("Special Leave");
sickRButton = new JRadioButton("Sick Leave");
privilegeRButton = new JRadioButton("Privilege Leave");
radioButtonGroup = new ButtonGroup();
radioButtonGroup.add(casualRButton);
radioButtonGroup.add(specialRButton);
radioButtonGroup.add(sickRButton);
radioButtonGroup.add(privilegeRButton);
leaveTypePanel.add(casualRButton);
leaveTypePanel.add(specialRButton);
leaveTypePanel.add(sickRButton);
leaveTypePanel.add(privilegeRButton);
JPanel applyLeavePanel = new JPanel();
applyLeavePanel.setLayout(new FlowLayout(
FlowLayout.LEFT, 5, 5));
JLabel applyLeaveLabel = new JLabel(
"Apply for (No. of days) : ", JLabel.CENTER);
leaveDaysField = new JTextField(5);
checkLeaveButton = new JButton("Check Leave Availability");
applyLeavePanel.add(applyLeaveLabel);
applyLeavePanel.add(leaveDaysField);
applyLeavePanel.add(checkLeaveButton);
leaveHeaderPanel.add(leaveTypePanel);
leaveHeaderPanel.add(applyLeavePanel);
notesArea = new JTextArea(10, 10);
notesScroller = new JScrollPane();
notesScroller.setBorder(
BorderFactory.createTitledBorder(
"Leave Note (Max. 200 Characters) : "));
notesScroller.setViewportView(notesArea);
panel.add(leaveHeaderPanel, BorderLayout.PAGE_START);
panel.add(notesScroller, BorderLayout.CENTER);
return panel;
}
private JPanel getApplyPanel()
{
JPanel panel = new JPanel();
applyLeaveButton = new JButton("Apply");
panel.add(applyLeaveButton);
return panel;
}
public static void main(String[] args)
{
Runnable runnable = new Runnable()
{
#Override
public void run()
{
new WelcomeExample().displayGUI();
}
};
EventQueue.invokeLater(runnable);
}
}
OUTPUT :
I don't think you need to do contentPane.add(textArea);. It is this line that is causing the problem. Comment out this and your code should work fine.
See this answer, it might help you.
The following code runs fine at my place :
final JTextArea textArea = new JTextArea();
textArea.setFont(new Font("MS UI Gothic", Font.PLAIN, 13));
textArea.setLineWrap(true);
textArea.setBounds(77, 310, 474, 136);
JScrollPane sbrText = new JScrollPane(textArea);
sbrText.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
frame.getContentPane().add(sbrText);//contentPane.add(sbrText);
frame.setVisible(true);
If your code is not running fine then you must have some other error probably related to your contentpane.
Ok...I finally got it to work...I specified the same set bounds in the scroll pane instead of the text area.. Here is the code.!!! ^_^
final JTextArea textArea = new JTextArea();
textArea.setFont(new Font("MS UI Gothic", Font.PLAIN, 13));
textArea.setLineWrap(true);
//textArea.setBounds(77, 310, 474, 136);
JScrollPane scroll = new JScrollPane (textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scroll.setBounds(77, 310, 474, 136);
contentPane.add(scroll);
Here are the screenshots:
Previous: http://oi40.tinypic.com/11jyum0.jpg
Now: http://oi44.tinypic.com/2s9vdvt.jpg
I have container jpanel in which i used a boxlayout manager and what i do is i add another panels inside in which the added panel contains a label and textfield using flowlayout manager. everytime i add a panel inside it creates an annoying big space after another added panel. I want to reduce the spacing of the panels i have tried using setsize and setpreferredsize method to adjust it. Here is my code:
JPanel global = new JPanel();
global.setLayout(new BoxLayout(global, BoxLayout.Y_AXIS));
global.setPreferredSize(new Dimension(500,312));
global.setSize(500,312);
global.setBounds(8, 5, 500, 312);
global.setBorder(BorderFactory.createLineBorder(Color.black));
global.setBackground(Color.white);
//Elements of global
JLabel label1 = new JLabel("Global Settings");
label1.setAlignmentX(Component.CENTER_ALIGNMENT);
label1.setFont(new Font("tahoma", Font.BOLD, 17));
global.add(label1);
global.add(new JSeparator());
//Name Field
JPanel c = new JPanel();
c.setSize(100, 1);
c.setPreferredSize(new Dimension(100,1));
c.setLayout(new FlowLayout());
JLabel label = new JLabel("Display Name");
JTextField text = new JTextField(20);
text.setPreferredSize(new Dimension(20,25));
c.add(label);
c.add(text);
global.add(c);
//Hostname Field
JPanel c1 = new JPanel();
c1.setSize(100, 1);
c1.setPreferredSize(new Dimension(100,1));
c1.setLayout(new FlowLayout());
JLabel label2 = new JLabel("Host Name");
JTextField text1 = new JTextField(20);
text1.setPreferredSize(new Dimension(20,25));
c1.add(label2);
c1.add(text1);
global.add(c1);
BoxLayout is a pretty aggressive LayoutManager and doesn't always honour the preferred size of components within it. Instead, we must set the maximum size of BoxLayout components to prevent them from being stretched. Additionally, we need to add a Box via Box.createVerticalGlue() - this is special component that gets stretched (rather than the other components).
Here is the rewritten code:
JPanel global = new JPanel();
global.setLayout(new BoxLayout(global, BoxLayout.Y_AXIS));
global.setPreferredSize(new Dimension(500, 312));
global.setSize(500, 312);
global.setBounds(8, 5, 500, 312);
global.setBorder(BorderFactory.createLineBorder(Color.black));
global.setBackground(Color.white);
// Elements of global
JLabel label1 = new JLabel("Global Settings");
label1.setAlignmentX(Component.CENTER_ALIGNMENT);
label1.setFont(new Font("tahoma", Font.BOLD, 17));
global.add(label1);
JSeparator sep = new JSeparator();
sep.setMaximumSize(new Dimension((int) sep.getMaximumSize().getWidth(), 50));
global.add(sep);
// Name Field
JPanel c = new JPanel();
c.setMaximumSize(new Dimension((int) c.getMaximumSize().getWidth(), 50));
JLabel label = new JLabel("Display Name");
JTextField text = new JTextField(20);
text.setPreferredSize(new Dimension(20, 25));
c.add(label);
c.add(text);
global.add(c);
// Hostname Field
JPanel c1 = new JPanel();
c1.setMaximumSize(new Dimension((int) c1.getMaximumSize().getWidth(), 50));
JLabel label2 = new JLabel("Host Name");
JTextField text1 = new JTextField(20);
text1.setPreferredSize(new Dimension(20, 25));
c1.add(label2);
c1.add(text1);
global.add(c1);
global.add(Box.createVerticalGlue());