I try to set new red border to my "Ok" button, but instead of I get from this. How to make 1-st picture? (it's photoshop)
I try
button.setBorder(new LineBorder(Color.RED, 1));
You overrided the button's default border. You should set a compound border using the existing one and a new red one.
Border innerBorder = button.getBorder();
Border outerBorder = new LineBorder(Color.RED, 1);
button.setBorder(new CompoundBorder(outerBorder, innerBorder));
Here is a one liner combined with using the BorderFactory :-)
button.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(Color.RED, 1),
button.getBorder()));
Using BorderFactory - http://docs.oracle.com/javase/tutorial/uiswing/components/border.html#createapi
Related
I have a Java Swing form and JLabel like this:
What I need to do is inserting a gap in the beginning of the JLabel:
So it will not stuck to the border line.
Note 1 : I already used jLabName.setIconTextGap(35); but it did the below:
I need to insert the gap before the icon not after it!
Note 2 : The Border Setting And Type And other setting:
You can use compound borders for that.
For Example.
//get border of your component which is button as you say
Border border = myButton.getBorder();
//create a new empty border with name it margin
Border margin = new EmptyBorder(0,10,0,0); //top 0, left 10 , bottom 0, right 0
//now set compound border to your button(component), with margin
myButton.setBorder(new CompoundBorder(border, margin));
//NOTE: CompoundBorder accepts two borders as arguments, first one is inner border and last one is outer border
Fixed it with the following code:
a1.setBorder(new CompoundBorder(new EtchedBorder(), BorderFactory.createEmptyBorder(1, 8, 1, 1)));
I am a beginner. I am programming a Java game, and I am having trouble changing the size of the button buttonPlayAgain. The size of the button does not change using the code below. How can I change the size of the button?
Here is my code:
public hoppa(IModele modele) {
super(new GridLayout(1, 1));
setSize(VueGrille.FACT * modele.getGrille().getLongueur(), 1);
Dimension dim = new Dimension(1, 1);
labMines = new JLabel();
labMines.setPreferredSize(dim);
add(labMines);
buttonPlayAgain = new JButton("New Game");
//buttonPlayAgain.setSize(1, 1);
buttonPlayAgain.setPreferredSize(new Dimension(50,50));
add(buttonPlayAgain);
labTimer = new JLabel();
labTimer.setPreferredSize(dim);
add(labTimer);
initValues(modele);
}
It can be normal that your button is not resized while calling setPreferredSize. try setSize instead. See Java: Difference between the setPreferredSize() and setSize() methods in components
Use setSize() if your component's parent has no layout manager, and setPreferredSize() (see also setMinimumSize and setMaximumSize) if it does.
setSize() most likely won't do anything if the component's parent is using a layout manager
As the title says, I am simply trying to set the margins (provide some padding) on a TextArea with a LineBorder set. Without setting the Border, .setMargins works fine. Here is the specific chunk of code.
aboutArea = new JTextArea("program info etc.....");
Border border = BorderFactory.createLineBorder(Color.BLACK);
aboutArea.setSize(400, 200);
aboutArea.setBorder(border);
aboutArea.setEditable(false);
aboutArea.setFont(new Font("Verdana", Font.BOLD, 12));
add(aboutArea);
I have tried each of these:
aboutArea.setMargins(10,10,10,10);
.getBorders(aboutArea).set(10,10,10,10);
UIManager.put("aboutArea.margin", new Insets(10, 10, 10, 10));
but nothing affects the margins after I apply the border, the padding is always 0. Any ideas how to set the padding on the textArea with the border?
What if you try adding a CompoundBorder , won't this do, this will give you almost same thing
JTextArea tarea = new JTextArea("program info etc.");
Border border = BorderFactory.createLineBorder(Color.BLACK);
tarea.setBorder(BorderFactory.createCompoundBorder(border,
BorderFactory.createEmptyBorder(10, 10, 10, 10)));
I have an icon button that I want to use with a border. The default border looks too thick.
What's the easiest way to create a border with the same colors as the current look + feel?
Button myButton = createMyIconButton();
...
public Button createMyIconButton()
{
...
setBorder(BorderFactory.createLineBorder(???, 1));
}
The default border looks too thick.
Maybe you are looking for:
button.setMargin( new Insets(2, 2, 2, 2) );
I have got a window that should display the following:
JLablel "Have you used GUI before?" on the top, centered
two radioButtons "Yes" and "No" below it, somewhat in the center, a little bit towards the left
a JButton "NEXT" in the bottom-right corner
All three elements should have green font and darkGrey background.
The problem is that the window which is showing up, does not look like I would like it to.
And this is my code:
yesButton = new JRadioButton(yes);
//yesButton.setMnemonic(KeyEvent.VK_B); // doesn't work?
yesButton.setActionCommand(yes);
noButton = new JRadioButton(no);
// noButton.setMnemonic(KeyEvent.VK_C); // doesn't work?
noButton.setActionCommand(no);
ButtonGroup group = new ButtonGroup();
group.add(yesButton);
group.add(noButton);
nextButton = new JButton("NEXT");
nextButton.setActionCommand(next);
yesButton.addActionListener(this);
noButton.addActionListener(this);
nextButton.addActionListener(this);
JPanel radioPanel = new JPanel(new GridLayout(0, 1));
radioPanel.add(yesButton);
radioPanel.add(noButton);
add(radioPanel, BorderLayout.WEST);
// setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
// radioPanel.setBorder(new EmptyBorder(250, 250, 20, 20));
// there is no difference between the above two, right?
String q = "Have you used GUI before?";
JPanel area = new JPanel(new BorderLayout());
area.setBackground(Color.darkGray);
JLabel textLabel2 = new JLabel("<html><div style=\"text-align: center;\">"
+ q + "</html>", SwingConstants.CENTER);
textLabel2.setForeground(Color.green);
Font font2 = new Font("SansSerif", Font.PLAIN, 30);
textLabel2.setFont(font2);
//textLabel2.setBorder(new EmptyBorder(0, 0, 250, 0)); //top, left, bottom, right
area.add(textLabel2, BorderLayout.NORTH);
area.add(nextButton, BorderLayout.EAST);
add(area, BorderLayout.CENTER);
I feel I'm nearly there, thanks for any help!
--EDIT--
A screenshot:
You need to use nested panels.
for the BorderLayout.NORTH you can add the JLabel directly. You will need to set the horizontal text alignment to center.
for the radio buttons you can create a JPanel with a FlowLayout and then add the buttons to the panel and add the panel to the CENTER.
for the button you add the button to a panel using a FlowLayout that is right aligned, then add the panel to the SOUTH.
There are other choices. You could also use a Vertical BoxLayout as the layout of the main panel and then add child panels to it.
You won't be able to get much control with just a BorderLayout. Try something else like MigLayout or one of the other many many layout managers Java has (GridBag, Box, etc).
In MigLayout it would look something like:
area.setLayout(new MigLayout("fill"));
area.add(textLabel2, "wrap");
area.add(radioPanel, "wrap");
area.add(nextButton, "tag right");