2

So, my JFrame is not turning out the way I want it to, because of the FlowLayout() but I don't know what else to use to fix this. It just makes my JButton fill the entire JFrame. Is there a way I can get FlowLayout() to apply my custom sizes and location for the JFrame components, or is there an alternative that could easily replace it?

Here is my code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MTGSAMPServerReference extends JFrame implements ActionListener {

    public static Toolkit tk = Toolkit.getDefaultToolkit();
    static int ScrnWidth = ((int) tk.getScreenSize().getWidth());
    static int ScrnHeight = ((int) tk.getScreenSize().getHeight());

    private static final long serialVersionUID = 1L;
    private static JList list1;
    private static JButton next;

    public MTGSAMPServerReference() {
        // set flow layout for the frame
        this.getContentPane().setLayout(new FlowLayout());
        Object[] data1 = { "Value 1", "Value 2", "Value 3", "Value 4", "Value 5" };
        list1 = new JList<Object>(data1);
        next = new JButton("Next");
        next.addActionListener(this);
        // add list to frame
        add(list1);
        add(next);
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("Next")) {
            int index = list1.getSelectedIndex();
            System.out.println("Index Selected: " + index);
            String s = (String) list1.getSelectedValue();
            System.out.println("Value Selected: " + s);
        }
    }
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame f = new MTGSAMPServerReference();
        //Display the window.
        f.pack();
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        f.setSize(1200, 800);
        f.setLocationRelativeTo(null);
        list1.setSize(250, 250);
        list1.setLocation(0, 0);
        next.setSize(75, 25);
        next.setLocation(251, 276);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
        createAndShowGUI();
        }
        });
    }
}

Any ideas?

Thanks in advance!

EDIT: Here is a picture of the problem.

This is the problem.

EDIT: Here is what I want it to look like (approximately):

This is about what it should look like.

knorberg
  • 462
  • 5
  • 19
  • *"It just makes my JButton fill the entire JFrame"* Not here it doesn't. Provide ASCII art of the GUI as it should appear in smallest size and (if resizable) with extra width/height. Also, please include the imports at the top of source code, and fix any compiler warnings before posting. I have a version of your code that does that. Do you want me to edit it into the question? – Andrew Thompson Jul 22 '13 at 20:34
  • Or just post the image, or a link to it. – tbodt Jul 22 '13 at 20:35
  • @AndrewThompson Do what now? Should I just take a screenshot of what it looks like? – knorberg Jul 22 '13 at 20:35
  • *"Do what now?"* I have a version of your code that includes imports and fixes the warning! *"Should I just take a screenshot of what it looks like?"* I don't think that will help much, since my results differ from yours. – Andrew Thompson Jul 22 '13 at 20:37
  • Don't use custom sizes and locations. The point of layout managers is that you don't need them (and they won't work, since the layout manager sets the component bounds) – kiheru Jul 22 '13 at 20:38
  • @kiheru As is my question, "what should I use instead of `FlowLayout()`?" – knorberg Jul 22 '13 at 20:40
  • That is sort of difficult to say, as long as we do not know what you want the layout to be. (And really don't use `null`. That breaks the moment you want to change anything, or the user resizes the window) – kiheru Jul 22 '13 at 20:40
  • In case you missed it earlier.. ***Provide ASCII art of the GUI as it should appear in smallest size and (if resizable) with extra width/height.*** – Andrew Thompson Jul 22 '13 at 20:42
  • @kiheru I want the components to be bound to the sizes and locations that I specify, not automatically centered. – knorberg Jul 22 '13 at 20:42
  • @AndrewThompson As I said earlier, I do not understand exactly what it is that you are asking me to do. – knorberg Jul 22 '13 at 20:43
  • I figure it will be easier to edit your post myself, so I did that. As for anything else, you'll have to be more specific about *what* you don't understand. I do not intend to explain every word! – Andrew Thompson Jul 22 '13 at 20:45
  • 1
    Forget exact placement. Give a rough outline where and how you want to the components to be, and we can help you do it using a layout manager. That does not mean centered, unless you want it to. – kiheru Jul 22 '13 at 20:45
  • @kiheru I want it to place the JList somewhere near the middle of the Y and about 250 in on the X. And the JButton at the bottom right hand corner of the JList. I will add a picture of what I want it to look like in an edit. – knorberg Jul 22 '13 at 20:50
  • *"..the JButton at the bottom right hand corner of the JList."* That just looks ..weird. It would be more pleasing to the eye to have the button centered below the list. It is also much easier. Can you go with the button being *below* the list? – Andrew Thompson Jul 22 '13 at 21:07
  • @AndrewThompson Sure, that could work. – knorberg Jul 22 '13 at 21:07
  • @AndrewThompson How would I set it up like that? – knorberg Jul 22 '13 at 21:08
  • That looks suspiciously like you intend to fill the rest of the window with several other components. The layout can be created, but I think you are still too stuck in the idea of placing the components on a fixed sized canvas with no thought on resizing. It's fine to have component groups with their own layout managers, though. – kiheru Jul 22 '13 at 21:14
  • @kiheru I am OCD and have to have things positioned a certain way. Haha. Anyways, is there a way to do this? – knorberg Jul 22 '13 at 21:15

2 Answers2

3

This example puts the button below the list, and adds a border to the list using a scroll pane. 'White space' in the GUI is provided partly by spacing defined in the constructor of the layout (e.g. the space between list and button), and partly by using an EmptyBorder.

The control panel (which uses a BorderLayout) is then placed inside the other FlowLayout.

GUI

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

public class MTGSAMPServerReference extends JFrame implements ActionListener {

    public static Toolkit tk = Toolkit.getDefaultToolkit();
    static int ScrnWidth = ((int) tk.getScreenSize().getWidth());
    static int ScrnHeight = ((int) tk.getScreenSize().getHeight());

    private static final long serialVersionUID = 1L;
    private static JList list1;
    private static JButton next;

    public MTGSAMPServerReference() {
        // set flow layout for the frame
        this.getContentPane().setLayout(new FlowLayout(FlowLayout.LEADING));
        Object[] data1 = { "Value 1", "Value 2", "Value 3", "Value 4", "Value 5" };

        JPanel controls = new JPanel( new BorderLayout(5,5) );

        list1 = new JList<Object>(data1);
        list1.setVisibleRowCount(5);
        next = new JButton("Next");
        next.addActionListener(this);
        // add list to frame
        controls.add(new JScrollPane(list1));
        controls.add(next, BorderLayout.PAGE_END);
        // adjust numbers as needed.
        controls.setBorder(new EmptyBorder(25,25,0,0));

        add(controls);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("Next")) {
            int index = list1.getSelectedIndex();
            System.out.println("Index Selected: " + index);
            String s = (String) list1.getSelectedValue();
            System.out.println("Value Selected: " + s);
        }
    }

    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame f = new MTGSAMPServerReference();
        //Display the window.
        f.pack();
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        f.setSize(1200, 800);
        f.setLocationRelativeTo(null);
        list1.setSize(250, 250);
        list1.setLocation(0, 0);
        next.setSize(75, 25);
        next.setLocation(251, 276);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
        createAndShowGUI();
        }
        });
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • So, this allows the customization of the `FlowLayout()`, correct? I see it can move the components/ – knorberg Jul 22 '13 at 21:18
  • 2
    Run it, then play with the numbers in the `BorderLayout` & `EmptyBorder` constructors. ..then read the JavaDocs for each constructor. – Andrew Thompson Jul 22 '13 at 21:20
  • And sorry for not understanding sometimes, I am still trying to learn and perfect my skills. – knorberg Jul 22 '13 at 21:23
  • For some reason, I am getting an error with the line `JPanel controls = new JPanel( new BorderLayout(5,5) );` any ideas why? – knorberg Jul 22 '13 at 21:38
  • A compiler error should: 1) Be asked on a separate question 2) Include the code needed for us to reproduce it (an SSCCE) 3) Include the exact, copy/pasted text of the message. – Andrew Thompson Jul 22 '13 at 21:42
-3

If you want complete control over where the components are placed, and have setBounds work, you need to remove the layout manager. This can be done by setting the layout manager to null. That's not a good idea, though, and you are better off only using a layout manager and not using setBounds at all, because you are not tied down to a particular window size and default component sizes, and components that look good with a specific size on one look and feel might not look nice on another.

tbodt
  • 16,609
  • 6
  • 58
  • 83
  • I want complete control, but don't want to do anything that is not a good practice. I am learning and want to know the best way to do this. – knorberg Jul 22 '13 at 20:41
  • Those two demands conflict with each other. You should use a layout manager when starting out. It is guaranteed to place your components nicely. – tbodt Jul 22 '13 at 20:42
  • 3
    An automatic -1 for even mentioning `setBounds`. That is only for use by experts and even then, only in extremely rare circumstances. – Andrew Thompson Jul 22 '13 at 20:43
  • I have told him not to use `setBounds`!, I never use it myself even! I just know it is a bad idea. – tbodt Jul 22 '13 at 20:44
  • Just to finish this off.. My usual comment to people who ask questions involving `null` layouts is.. *"Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or combinations of them, along with layout padding & borders for white space."* -- See my answer for an example of that. If you want to avoid future down-votes, stop mentioning `null` layouts in answers. – Andrew Thompson Jul 22 '13 at 21:27