Change dimensions of JButton - java

I have problem with this. The button is taking up the entire JFrame. I've tried changing the dimensions of the JFrame and the JButton but with no changed. It's completely hiding a JTable underneath. Could someone please tell me what I'm doing wrong.
JFrame FRAME = new JFrame();
JButton BUTTON = new JButton("OK");
FRAME.add(new JScrollPane(TableName));
BUTTON.setPreferredSize(new Dimension(20,30));
FRAME.add(BUTTON);
FRAME.setSize(700, 600);
FRAME.setVisible(true);
FRAME.setLocationRelativeTo(null);

The default layout manager for a a JFrame is a BorderLayout. You are attempting to add two component the CENTER which is not allowed. Only the last one added will be displayed.
You need to specify constraints when adding components to a BorderLayout. Your code should be something like:
frame.add(new JScrollPane(TableName), BorderLayout.CENTER);
button.setPreferredSize(new Dimension(20,30));
frame.add(button, BorderLayout.PAGE_START);
Also, variables names should NOT be upper cased. Follow Java convention.
Read the section from the Swing tutorial on How to Use BorderLayout for more information and working examples. The tutorial code will also show you how to better structure your program so you follow Swing coding conventions.

Don't add the button straight to the frame, create a JPanel first, add the button to the panel then add the panel to the frame.

JFrame frame = new JFrame();
// frame.setBounds(x axis, y axis, weight, height)
frame.setBounds(10,10,304,214);
frame.getContentPane().setLayout(null);
JButton button = new JButton("Button");
button.setBounds(98, 75, 126, 39);
frame.getContentPane().add(button);
frame.setVisible(true);

Related

How to fix GUI components "leaving" their panels or taking up more space they they are supposed to?

I am trying to make a GUI for a game. I am very new to Java, especially GUI. The code below is a snippet which is supposed to make a JFrame with nested panels for organization. It works until I add buttons to the button panel. They end up on the boardBckg panel. If I manage to place them on the correct panel the JTextField disappears or it takes up the entire screen. I have been working on this part of the code for the past two days and I could really use GUI tips.
private void makeWindow()
{
boardPanel = new JPanel();
boardBckg = new JPanel();
menuPanel = new JPanel();
save = new JButton("Save");
save.setSize(Buttons);
load = new JButton("Load");
load.setSize(Buttons);
replay = new JButton ("Replay");
replay.setSize(Buttons);
words = new JTextField();
frame = new JFrame(title);
boardPanel.setSize(PANEL);
boardPanel.setMaximumSize(MAX);
boardPanel.setMinimumSize(MIN);
boardPanel.setLayout(new GridLayout(m,n));
boardBckg.setSize(1000, 1000);
boardBckg.setBackground(Color.cyan);
boardBckg.add(boardPanel, BorderLayout.CENTER);
frame.setSize(1500, 1000);
frame.setResizable(false);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BoxLayout vertical = new BoxLayout(menuPanel, BoxLayout.Y_AXIS);
menuPanel.setSize(500, 1000);
menuPanel.setBackground(Color.blue);
menuPanel.setLayout(vertical);
frame.add(boardBckg);
frame.add(menuPanel);
JPanel iGiveUp = new JPanel();
iGiveUp.setBackground(Color.black);
JPanel buttons = new JPanel();
buttons.setBackground(Color.darkGray);
buttons.add(save);
buttons.add(load);
buttons.add(replay);
menuPanel.add(iGiveUp);
menuPanel.add(buttons);
iGiveUp.add(words);
boardBckg.add(boardPanel, BorderLayout.CENTER);
The default layout of a JPanel is the FlowLayout. You can't just specify a BorderLayout constraint when you add the component to the panel.
frame.add(boardBckg);
frame.add(menuPanel);
The default layout for (the content pane of) the frame is a BorderLayout. If you don't specify a constraint, then the component is added to the BorderLayout.CENTER. Problem is only one component can be added to the CENTER so you only see the last comoponent added.
frame.setVisible(true);
Component should be added to the frame BEFORE the frame is packed and made visible. So the above statement should be the last statement in your constructor.
I have no ideas what your desired layout is but you need to start with something simple and take advantage of the default BorderLayout of the frame.
So your basic logic might be something like:
JPanel menuPanel = new JPanel()
menuPanel.setLayout(new BoxLayout(menuPanel, BoxLayout.Y_AXIS));
menuPanel.add(...);
menuPanel.add(...);
JPanel center = new JPanel();
center.setLayout(...);
center.setBackground( Color.BLUE );
center.add(...);
frame.add(menuPanel, BorderLayout.LINE_START);
frame.add(center, BorderLayout.CENTER);
frame.pack();
frame.setVisible( true );
The main point is to break the panels down logically and add them to the frame one at a time. So first get the menu and its child components added to the frame is the correct position. Then you can add the CENTER panel and its child components.

Put a JLabel over two JButtons

Okay so before you ask, yes I'm not using any Layout Manager. No, that doesn't make this bad design (as I've seen people in here saying because someone simply didn't use one). The thing is i want the label to always (and I mean always) show over the two buttons (over the gap left by them which makes it impossible to put it as an Icon or a text on the JButton).
JFrame frame = new JFrame("ColorTap");
private void init() {
JButton jb1 = new JButton(""), jb2 = new JButton("-");
JLabel label = new JLabel("TEXT HERE");
label.setForeground(Color.white);
label.setFont(new Font("Arial Bold",Font.ITALIC,30));
label.setBounds(60,249,200,100);
frame.setLayout(null);
jb1.setBounds(0, 0, 300,298);
jb2.setBounds(0, 302, 300, 300);
jb1.setBackground(Color.black);
jb2.setBackground(Color.black);
jb1.setBorderPainted(false);
jb2.setBorderPainted(false);
frame.add(label);
frame.add(jb1);
frame.add(jb2);
frame.setResizable(false);
frame.setSize(300, 628);
frame.setLocation(550, 50);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
After this what's stranger to me is that the button on the bottom stays under the label and not the one on the top... HELP! Thanks
Swing is optimized to paint components in two dimensions. That is it assumes components will never overlap. Overlapping JButtons cause a problem because of the rollover effects which only cause the buttons to be painted, not the label, so the button is painted over the top of the label.
So you need to tell Swing that components do overlap so Swing can make sure components are painted in the proper ZOrder:
JPanel panel = new JPanel()
{
#Override
public boolean isOptimizedDrawingEnabled()
{
return false;
}
};
Now you can set the layout manager of the panel and add your components to the panel and they will be painted properly.
See: How to put a JButton with an image on top of another JButton with an image? for a working example that DOES use layout managers.

JButton added to JPanel doesn't show

I can't seem to add a JButton to a JPanel.
I have a PropWindow (JFrame) that has a PropView (JPanel) in it. the PropView-JPanel seems to be added correctly because I can draw shapes on it with paint().
But when I use this to try adding a button it just won't show up att all :/
JButton testButton;
public PropView(int width, int height) {
super(true);
setLayout(null);
setSize(width, height);
//TestButton
testButton = new JButton("Test");
testButton.setLocation(10,10);
testButton.setSize(100, 50);
testButton.setVisible(true);
add(testButton);
setFocusable(true);
setVisible(true);
}
The JFrame and the JPanel are both 250x600 px.
I can't tell from the code snippet you posted but just in case: make sure you call pack () on the frame after you have added the panel or any other components.
Also, it's usually discouraged to extend a JPanel or JFrame, unless you have a good reason to do it, just a heads up.
Here you have a short tutorial about displaying frames:
And some sample code in it that might help:
//1. Create the frame.
JFrame frame = new JFrame("FrameDemo");
//2. Optional: What happens when the frame closes?
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//3. Create components and put them in the frame.
//...create emptyLabel...
frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);
//4. Size the frame.
frame.pack();
//5. Show it.
frame.setVisible(true);
Make sure you added PropPanel to PropWindow using myPropWindow.getContentPane().add(myPropPanel), not just myPropWindow.add(myPropPanel).

Positioning of JButton in Java

This is very basic code but i just need some help to understand..
i have a button and i want to place it on the EAST side of the Panel.. any suggestions..
public class ButtonText {
public static void main(String[] args) {
Frame frame=new Frame("Button Frame");
Button button = new Button("Submit");
frame.setLayout(new FlowLayout());
frame.add(button, BorderLayout.EAST);
frame.setSize(200,100);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
which looks like this http://imgur.com/0GYso
any help would be greatly appreciated thank you!
To lay your user interface out with a border layout manager, you need to set your frame's layout to an instance of BorderLayout. A default FlowLayout sets components out from left to right and onto next lines if necessary.
frame.setLayout(new BorderLayout());
frame.add(button, BorderLayout.EAST);
You can use a Layout that positions the elements in the frame. Take a look here. You are using a FlowLayout, but you need to understand each layout and the rules positioning elements.
FlowLayout cant use BorderLayout constraints...
Try using BorderLayoutinstead of FlowLayout and it should work just fine.
Flowlayout doesn't let you do that, you should set the
frame.setLayout(new BorderLayout());
frame.add(button, BorderLayout.EAST);
or
frame.setLayout(new BorderLayout());
frame.add(button, BorderLayout.LINE_START);
to make it work,
The Flowlayout only positions things like a horizontal stack.
you may want to try something like this
Frame frame=new Frame("Button Frame");
Button button = new Button("Submit");
HorizontalPanel hp = new HorizontalPanel();
hp.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
hp.setWidth(200);
hp.add(button);
frame.add(hp);
frame.setSize(200,100);
frame.setVisible(true);

Java Textfield focus

Hello I have a problem with the focus
mytext= new JTextField();
mytext.requestFocus(true);
gc.fill =GridBagConstraints.HORIZONTAL ;
gc.gridx =3; gc.gridy=4;
gbl.setConstraints(mytext,gc);
jContentPane.add(mytext);
I tried
mytext.requestFocus();
too
and how can I auto-select the text in the textfield so the text is marked?
From the Swing Tutorial
If you want to ensure that a particular component gains the focus the first time a window is activated, you can call the requestFocusInWindow method on the component after the component has been realized, but before the frame is displayed. The following sample code shows how this operation can be done:
//...Where initialization occurs...
JFrame frame = new JFrame("Test");
JPanel panel = new JPanel(new BorderLayout());
//...Create a variety of components here...
//Create the component that will have the initial focus.
JButton button = new JButton("I am first");
panel.add(button);
frame.getContentPane().add(panel); //Add it to the panel
frame.pack(); //Realize the components.
//This button will have the initial focus.
button.requestFocusInWindow();
frame.setVisible(true); //Display the window.
As for selecting all the text you should use...
mytext.selectAll();
As for getting focus, maybe you should try the requestFocus function after everything has been added to jContentPane.

Categories

Resources