How can I set size of a button? - java

I put my buttons in a JPane with GridLayout. Then I put JPanel into another JPanel with BoxLayout.Y_AXIS. I want buttons in the GridLayout to be square. I use tmp.setSize(30,30) and it does not work. I also try to use new GridLayout(X, Y, 4, 4) but I cannot figure out what X and Y are. So, what is the correct way to do this stuff?
ADDED:
I still cannot solve the problem. Here is the code of what I am trying to do:
import javax.swing.*;
import java.awt.*;
public class PanelModel {
public static void main(String[] args) {
JFrame frame = new JFrame("Colored Trails");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
JPanel firstPanel = new JPanel();
firstPanel.setLayout(new GridLayout(4, 4));
JButton btn;
for (int i=1; i<=4; i++) {
for (int j=1; j<=4; j++) {
btn = new JButton();
btn.setPreferredSize(new Dimension(100, 100));
firstPanel.add(btn);
}
}
JPanel secondPanel = new JPanel();
secondPanel.setLayout(new GridLayout(5, 13));
for (int i=1; i<=5; i++) {
for (int j=1; j<=13; j++) {
btn = new JButton();
btn.setPreferredSize(new Dimension(40, 40));
secondPanel.add(btn);
}
}
mainPanel.add(firstPanel);
mainPanel.add(secondPanel);
frame.add(mainPanel);
frame.setSize(400,600);
frame.setVisible(true);
}
}
The problem is that Java tries to make width of the firstPanel and secondPanel equal! Moreover, Java tries to to fill all height of the window. How can I remove this behavior?

The following bit of code does what you ask for. Just make sure that you assign enough space so that the text on the button becomes visible
JFrame frame = new JFrame("test");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new GridLayout(4,4,4,4));
for(int i=0 ; i<16 ; i++){
JButton btn = new JButton(String.valueOf(i));
btn.setPreferredSize(new Dimension(40, 40));
panel.add(btn);
}
frame.setContentPane(panel);
frame.pack();
frame.setVisible(true);
The X and Y (two first parameters of the GridLayout constructor) specify the number of rows and columns in the grid (respectively). You may leave one of them as 0 if you want that value to be unbounded.
Edit
I've modified the provided code and I believe it now conforms to what is desired:
JFrame frame = new JFrame("Colored Trails");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
JPanel firstPanel = new JPanel();
firstPanel.setLayout(new GridLayout(4, 4));
firstPanel.setMaximumSize(new Dimension(400, 400));
JButton btn;
for (int i=1; i<=4; i++) {
for (int j=1; j<=4; j++) {
btn = new JButton();
btn.setPreferredSize(new Dimension(100, 100));
firstPanel.add(btn);
}
}
JPanel secondPanel = new JPanel();
secondPanel.setLayout(new GridLayout(5, 13));
secondPanel.setMaximumSize(new Dimension(520, 200));
for (int i=1; i<=5; i++) {
for (int j=1; j<=13; j++) {
btn = new JButton();
btn.setPreferredSize(new Dimension(40, 40));
secondPanel.add(btn);
}
}
mainPanel.add(firstPanel);
mainPanel.add(secondPanel);
frame.setContentPane(mainPanel);
frame.setSize(520,600);
frame.setMinimumSize(new Dimension(520,600));
frame.setVisible(true);
Basically I now set the preferred size of the panels and a minimum size for the frame.

Try with setPreferredSize instead of setSize.
UPDATE: GridLayout take up all space in its container, and BoxLayout seams to take up all the width in its container, so I added some glue-panels that are invisible and just take up space when the user stretches the window. I have just done this horizontally, and not vertically, but you could implement that in the same way if you want it.
Since GridLayout make all cells in the same size, it doesn't matter if they have a specified size. You have to specify a size for its container instead, as I have done.
import javax.swing.*;
import java.awt.*;
public class PanelModel {
public static void main(String[] args) {
JFrame frame = new JFrame("Colored Trails");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
JPanel firstPanel = new JPanel(new GridLayout(4, 4));
firstPanel.setPreferredSize(new Dimension(4*100, 4*100));
for (int i=1; i<=4; i++) {
for (int j=1; j<=4; j++) {
firstPanel.add(new JButton());
}
}
JPanel firstGluePanel = new JPanel(new BorderLayout());
firstGluePanel.add(firstPanel, BorderLayout.WEST);
firstGluePanel.add(Box.createHorizontalGlue(), BorderLayout.CENTER);
firstGluePanel.add(Box.createVerticalGlue(), BorderLayout.SOUTH);
JPanel secondPanel = new JPanel(new GridLayout(13, 5));
secondPanel.setPreferredSize(new Dimension(5*40, 13*40));
for (int i=1; i<=5; i++) {
for (int j=1; j<=13; j++) {
secondPanel.add(new JButton());
}
}
JPanel secondGluePanel = new JPanel(new BorderLayout());
secondGluePanel.add(secondPanel, BorderLayout.WEST);
secondGluePanel.add(Box.createHorizontalGlue(), BorderLayout.CENTER);
secondGluePanel.add(Box.createVerticalGlue(), BorderLayout.SOUTH);
mainPanel.add(firstGluePanel);
mainPanel.add(secondGluePanel);
frame.getContentPane().add(mainPanel);
//frame.setSize(400,600);
frame.pack();
frame.setVisible(true);
}
}

GridLayout is often not the best choice for buttons, although it might be for your application. A good reference is the tutorial on using Layout Managers. If you look at the GridLayout example, you'll see the buttons look a little silly -- way too big.
A better idea might be to use a FlowLayout for your buttons, or if you know exactly what you want, perhaps a GroupLayout. (Sun/Oracle recommend that GroupLayout or GridBag layout are better than GridLayout when hand-coding.)

This is how I did it.
JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("SAP Multiple Entries");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new GridLayout(10,10,10,10));
frame.setLayout(new FlowLayout());
frame.setSize(512, 512);
JButton button = new JButton("Select File");
button.setPreferredSize(new Dimension(256, 256));
panel.add(button);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
JFileChooser fileChooser = new JFileChooser();
int returnValue = fileChooser.showOpenDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
keep = selectedFile.getAbsolutePath();
// System.out.println(keep);
//out.println(file.flag);
if(file.flag==true) {
JOptionPane.showMessageDialog(null, "It is done! \nLocation: " + file.path , "Success Message", JOptionPane.INFORMATION_MESSAGE);
}
else{
JOptionPane.showMessageDialog(null, "failure", "not okay", JOptionPane.INFORMATION_MESSAGE);
}
}
}
});
frame.add(button);
frame.pack();
frame.setVisible(true);

Related

How do I make a button use up all of the space within a BorderLayout leaving no white space?

I've got a program which creates an 8x8 grid filled with random colours (already done this bit) and I'm supposed to add a button to the bottom which should fill the entire bottom part which will be used to reset the colours. I've already got the grid and the button, however, my button doesn't fill up the entire space at the bottom and only about half of it. How do I make it fill the entire space up?
My code:
public void createGUI()
{
guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
guiFrame.setTitle("Label demo");
JPanel mainPanel = new JPanel();
JPanel gridPanel = new JPanel();
JPanel buttonPanel = new JPanel();
JButton refreshButton = new JButton("Press me to refresh labels");
mainPanel.setLayout(new BorderLayout());
guiFrame.getContentPane().add(mainPanel);
gridPanel.setLayout(new GridLayout(8, 8));
for (int i = 0; i < arrayLabels.length; i++)
{
arrayLabels[i] = new ColorLabel(80, 80, new Color(rand.nextInt()), 0, new Color(rand.nextInt()));
}
for (int i = 0; i < 8*8; i++)
{
gridPanel.add(arrayLabels[i]);
}
guiFrame.getContentPane().add(gridPanel);
buttonPanel.add(refreshButton);
guiFrame.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
guiFrame.setVisible(true);
}
And the output of my program currently is this: https://imgur.com/a/fw1Bx92
Also set the JPanel layout of refresh button! I make an example below you can examine it:
public static void createGUI()
{
JFrame guiFrame = new JFrame("a");
guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
guiFrame.setTitle("Label demo");
JPanel mainPanel = new JPanel();
JPanel gridPanel = new JPanel();
JPanel buttonPanel = new JPanel(new BorderLayout());
JButton refreshButton = new JButton("Press me to refresh labels");
mainPanel.setLayout(new BorderLayout());
guiFrame.getContentPane().add(mainPanel);
gridPanel.setLayout(new GridLayout(8, 8));
JLabel arrayLabels[] = new JLabel[64];
for (int i = 0; i < 64; i++)
{
arrayLabels[i] = new JLabel("a");
}
for (int i = 0; i < 8*8; i++)
{
gridPanel.add(arrayLabels[i]);
}
guiFrame.getContentPane().setLayout(new BorderLayout());
guiFrame.getContentPane().add(gridPanel,BorderLayout.CENTER);
buttonPanel.add(refreshButton,BorderLayout.CENTER);
guiFrame.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
guiFrame.setVisible(true);
}

Adding a JScrollPane to an existing JPanel

I am trying to add a JScrollPane (createTeamScrollPane) to a JPanel (createTeamPanel) that I have. I have a frame, with a BorderLayout with the NORTH portion being used by a JPanel called tabMenu, and then the CENTER portion I would like my 'createTeamPanel' to have this scrolling ability as it will have more content than what I can fit on the screen at once. I am then adding both panels to the frame. Currently the code as is runs but the window appears blank. Once resizing the window, I then see the 3 buttons in the NORTH portion of my frame (why is this happening?) and when I click on 'Create Team' it brings up the list of JLabels and JButtons I expect but I don't see any scrolling bars?
public static void main (String args[]) {
JFrame frame = new JFrame();
frame.setTitle("v0.01");
frame.setSize(800, 800);
//frame.setLayout(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
JPanel tabMenu = new JPanel();
JPanel createTeamPanel = new JPanel();
createTeamPanel.setLayout(new BoxLayout(createTeamPanel, BoxLayout.Y_AXIS));
createTeamPanel.setSize(800, 700);
createTeamPanel.setVisible(showCreateTeamPanel);
createTeamPanel.setBackground(Color.gray);
JScrollPane createTeamScrollPane = new JScrollPane(createTeamPanel);
createTeamScrollPane.setBounds(50, 50, 200, 500);
createTeamScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
createTeamScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
createTeamScrollPane.setPreferredSize(new Dimension(500,500));
//createTeamPanel.add(createTeamScrollPane);
List<Player> teamList = MockTeams.initTeam();
int xcoord = 100;
int ycoord = 50;
for(Player player : teamList) {
JLabel label = new JLabel(player.getName());
label.setBounds(xcoord, ycoord, Constants.buttonWidth, Constants.buttonHeight);
JButton addToTeamBtn = new JButton("Add to team");
addToTeamBtn.setBounds(xcoord + 100, ycoord, Constants.buttonWidth, Constants.buttonHeight);
addToTeamBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
myTeam.add(player);
addToTeamBtn.setEnabled(false);
}
});
createTeamPanel.add(label);
//createTeamFrame.add(label);
createTeamPanel.add(addToTeamBtn);
//createTeamFrame.add(addToTeamBtn);
ycoord += 50;
}
JButton createTeamBtn = new JButton("Create Team");
createTeamBtn.setBounds(0,0,150,20);
createTeamBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Hide/Show Create team panel
if (!showCreateTeamPanel) {
showCreateTeamPanel = true;
createTeamPanel.setVisible(showCreateTeamPanel);
} else {
showCreateTeamPanel = false;
createTeamPanel.setVisible(showCreateTeamPanel);
}
}
});
JButton manageTeamBtn = new JButton("Team Statistics");
manageTeamBtn.setBounds(100,150,150,40);
JButton resetBtn = new JButton("Reset Season");
resetBtn.setBounds(100,200,150,40);
tabMenu.add(createTeamBtn);
tabMenu.add(manageTeamBtn);
tabMenu.add(resetBtn);
mainPanel.add(tabMenu, BorderLayout.NORTH);
mainPanel.add(createTeamPanel, BorderLayout.CENTER);
frame.add(mainPanel);
}
Expected result is to see a scrolling ability on the createTeamPanel but it is not there.
Fixed: I was able to add the JScrollPane to the mainPanel with:
mainPanel.add(createTeamScrollPane, BorderLayout.CENTER);

JPanel inside JPanel in JAVA

public static void main(String[] args) {
JTextField text = new JTextField();
JFrame frame = new JFrame();
frame.setLayout(new BorderLayout());
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(5, 4));
JPanel panel2 = new JPanel();
panel2.setLayout(new FlowLayout());
JLabel imgLabel1 = new JLabel(new ImageIcon("C:\\Users\\Arthur\\Downloads\\abs.jpg"));
JLabel imgLabel2 = new JLabel(new ImageIcon("C:\\Users\\Arthur\\Downloads\\abss.jpg"));
imgLabel1.setPreferredSize(new Dimension(100,100));
imgLabel2.setPreferredSize(new Dimension(100,100));
panel2.add(imgLabel1);
panel2.add(imgLabel2);
for(int i=0; i<20; i++){
panel.add(panel2);
}
frame.add(text, BorderLayout.NORTH);
frame.add(panel, BorderLayout.CENTER);
frame.setPreferredSize(new Dimension(1280,700));
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
I want to make a memory game, I need to put two images in each cell of the JPanel of 4x5. For this I created a JPanel 1x2 with two images inside and put it in the JPanel of 4x5. But the result is:
Result:
So, if understand correctly, you're problem is, you're not seeing 20 new panels, only one.
The problem is, a component can only reside in a single container, once, so doing something like...
for (int i = 0; i < 20; i++) {
panel.add(panel2);
}
is the equivalent of doing something like...
panel.add(panel2);
You actually need to create a new instance of the component on each iteration of the loop
What I would suggest you do is create a "wrapper" or "card" panel which can contain the two images. In my testing I just used coloured panels, but you get the idea...
public class WrapperPane extends JPanel {
public WrapperPane() {
setLayout(new FlowLayout());
add(makePanel(Color.RED));
add(makePanel(Color.GREEN));
// This is just for demonstration purposes
setBorder(new LineBorder(Color.DARK_GRAY));
}
protected JPanel makePanel(Color background) {
JPanel panel = new JPanel();
panel.setBackground(background);
panel.setPreferredSize(new Dimension(100, 100));
return panel;
}
}
The you'd just have to do something like...
JTextField text = new JTextField();
JFrame frame = new JFrame();
frame.setLayout(new BorderLayout());
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(5, 4));
for (int i = 0; i < 20; i++) {
panel.add(new WrapperPane());
}
frame.add(text, BorderLayout.NORTH);
frame.add(panel, BorderLayout.CENTER);
// Don't do this, just let the content make it's own
// calculations
//frame.setPreferredSize(new Dimension(1280, 700));
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
And you'd end up with something like...

Keep BoxLayout From Expanding Children

I want to stack some JComponents vertically inside a JPanel so they stack at the top and any extra space is at the bottom. I'm using a BoxLayout. The components will each contain a JTextArea that should allow the text to wrap if necessary. So, basically, I want the height of each of these components to be the minimum necessary for displaying the (possibly wrapped) text.
Here's a contained code example of what I'm doing:
import javax.swing.*;
import java.awt.*;
public class TextAreaTester {
public static void main(String[] args){
new TextAreaTester();
}
public TextAreaTester(){
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel,BoxLayout.PAGE_AXIS));
panel.setPreferredSize(new Dimension(100,400));
for(int i = 0; i<3; i++){
JPanel item = new JPanel(new BorderLayout());
JTextArea textarea = new JTextArea("this is a line of text I want to wrap if necessary");
textarea.setWrapStyleWord(true);
textarea.setLineWrap(true);
textarea.setMaximumSize( textarea.getPreferredSize() );
item.add(textarea,BorderLayout.NORTH);
panel.add(item);
}
panel.add(Box.createGlue());
frame.add(panel);
frame.setVisible(true);
frame.pack();
}
}
The child JPanels are expanding to fill the vertical space. I tried using glue because I thought that's what glue was for, but it seems to do nothing at all. Any help?
Note: I have found questions that look almost identical, but none with answers I can apply.
One solution: nest JPanels with the outer JPanel using Borderlayout and adding the BoxLayout using JPanel to this one BorderLayout.NORTH, also known as BorderLayout.PAGE_START:
Edit for Kleopatra:
import javax.swing.*;
import java.awt.*;
public class TextAreaTester {
public static void main(String[] args) {
new TextAreaTester();
}
public TextAreaTester() {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
// panel.setPreferredSize(new Dimension(100,400));
for (int i = 0; i < 3; i++) {
JPanel item = new JPanel(new BorderLayout());
// item.setLayout(new BoxLayout(item,BoxLayout.LINE_AXIS));
JTextArea textarea = new JTextArea(
"this is a line of text I want to wrap if necessary", 3, 35);
textarea.setWrapStyleWord(true);
textarea.setLineWrap(true);
// textarea.setMaximumSize(textarea.getPreferredSize());
// item.setMaximumSize( item.getPreferredSize() );
item.add(new JScrollPane(textarea), BorderLayout.NORTH);
panel.add(item);
}
panel.add(Box.createGlue());
JPanel mainPanel = new JPanel(new BorderLayout()) {
private final int prefW = 100;
private final int prefH = 400;
#Override
public Dimension getPreferredSize() {
return new Dimension(prefW, prefH);
}
};
// mainPanel.setPreferredSize(new Dimension(100, 400));
mainPanel.add(panel, BorderLayout.PAGE_START);
frame.add(mainPanel);
frame.setVisible(true);
// frame.getContentPane().add(jp);
frame.pack();
}
}
Alternatively, you can use Box.Filler. Just replace your call to panel.add(Box.createGlue()) with
panel.add(new Box.Filler(new Dimension(0, 0),
new Dimension(0, Short.MAX_VALUE),
new Dimension(0, Short.MAX_VALUE)));
If you want to achieve the same for a horizontal layout, just use Short.MAX_VALUE for width instead of height in the Dimension call.

JScrollPane for multiple labels

I am working on a program that has a flow layout, inside are a set of labels and because there are so many they do not all display. Is there anyway to add a scroll pane to scroll through all of these labels horizontally?
JPanel mainpanel = new JPanel();
mainpanel.setLayout(new BoxLayout(mainpanel, BoxLayout.X_AXIS));
pane.add(mainpanel, BorderLayout.NORTH);
JPanel rightpanel = new JPanel();
rightpanel.setLayout(new FlowLayout());
for (int i = 0; i < 100; i++)
{
rightpanel.add(new JLabel("Label " + i));
}
mainpanel.add(new JLabel("Left label"));
mainpanel.add(new JScrollPane(rightpanel));
I'd suggest ot use JList or JTable with one Column or Row (depends or direction), Object in the JList or JTable is JLabel/JComponent by default
Not sure what your question really is, since you already know you need to use a JScrollPane. How about:
public class ScrollLabels
{
public static void main(String[] args)
{
JFrame frame = new JFrame("Labels");
JPanel mainpanel = new JPanel();
mainpanel.setLayout(new BoxLayout(mainpanel, BoxLayout.X_AXIS));
frame.add(mainpanel);
JPanel rightpanel = new JPanel();
rightpanel.setLayout(new FlowLayout());
for (int i = 0; i < 100; i++)
{
rightpanel.add(new JLabel("Label " + i));
}
mainpanel.add(new JLabel("Left label"));
mainpanel.add(new JScrollPane(rightpanel));
frame.setSize(500, 100);
frame.setVisible(true);
}
}

Categories