0

So I'm trying to draw a sort of oval on a button and textarea. Yes, there is a reason why I can't just use a Panel or Frame.

So the button and textarea already has text on it and needs to stay as a button and textarea for the reason of the program.

So far, I only got one button to have this circle, but none of the other buttons show up with the circles. And the textareas aren't showing any circles at all.

My Code so Far for the buttons:

for (int i = 0; i < (label.length) / 2; i++) {
        butt = new JButton(label[i]); // label contains the text on button
        butt.setPreferredSize(new Dimension(83, 100));
        butt.add(smallPit); //smallpit is the circle graphics
        game.add(butt); // game is a panel
        // butt.setLayout(null);
    }
    for (int i = 6; i < label.length; i++) {
        butt = new JButton(label[i]);
        butt.setPreferredSize(new Dimension(83, 100));
        butt.add(smallPit);
        game.add(butt);
    }

Code for textarea

score2.setBounds(0, 50, 100, 210);
score1.setBounds(700, 50, 100, 210);
score2.add(scorePit);
score1.add(scorePit);

The outcome so far:

enter image description here

Xephos
  • 63
  • 1
  • 11
  • Some idea of why you are trying to achieve this might also allow use to provide different approaches you might try to achieve the same result – MadProgrammer Apr 14 '15 at 03:12
  • Sorry, I just realized my code was on my laptop and I was typing this question on my desktop. – Xephos Apr 14 '15 at 03:16
  • `score2.setBounds(0, 50, 100, 210);` -- no, never do this to a JTextArea as this will completely prevent it from scrolling if need be, and will allow text to happily bound beyond the borders of the area. Set its column and row properties instead. I fear that you might have other XY Problems involved here. – Hovercraft Full Of Eels Apr 14 '15 at 03:26
  • Your gut instinct may be to use a `null` layout and calling `setBounds(...)` on your components to place them with absolute positioning, but I'm going suggest that you not do this 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. Instead you will want to study and learn the layout managers and then nest JPanels, each using its own layout manager to create pleasing and complex GUI's that look good on all OS's. – Hovercraft Full Of Eels Apr 14 '15 at 03:27
  • Ah, it doesn't need to scroll. It only serves as a sort of label. – Xephos Apr 14 '15 at 03:38

1 Answers1

2

My guess is that this butt.add(smallPit); is causing your issues.

My guess is that smallPit extends from something like JComponent (something that extends from JComponent), the problem is a component can only reside within a single container, so each time you call butt.add(smallPit);, smallPit is removed from it's previous container before been added to the new one.

Instead, create an instance of smallPit (or what ever it is) and add it to each button which needs it.

Avoid using null layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify

You should also have a look at Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Do you have any idea about the JTextArea issue also? – Xephos Apr 14 '15 at 03:23
  • Same issue...but I'm not convinced that it's a good idea to "add" components directly to a `JTextArea`, but I'm not sure what it is you are trying to achieve so it's difficult to know what might be a better solution – MadProgrammer Apr 14 '15 at 03:24
  • I see. Basically, I just want to add a circle right over the text "Mancala A" and "Mancala B" in the textboxes. And yes, just using "add" doesn't work. Nothing shows up. – Xephos Apr 14 '15 at 03:26
  • 1
    It's probably because `JTextArea` doesn't have a layout manager. I'd consider using something like `JXLayer` or `JLayer` if you're using Java 7+ – MadProgrammer Apr 14 '15 at 03:27
  • I'm not aware of a component. :0 I'll take a look at it. Thanks! – Xephos Apr 14 '15 at 03:39