2

I look for a LayoutManager that has fixed height JButtons that expand in width to fit the size of their Container. Above the JButtons there should be a JLabel by itself on the line that says "Choose file:". This is intended to work as an accessory to a JFileChooser that lets the user choose recent files. I haven't been able to make it look quite right, I've tried using multiple JPanels and LayoutManagers such as BoxLayout. When using BoxLayout the JButtons only expand as far as they have to to contain their text; but I would like for all of the JButtons to be the same width so they don't look funny.

Note: I've also used other LayoutManagers such as Border and GridLayout but those mostly ignore my size settings and aren't sophisticated enough it would seem. I have to do this by hand, Netbeans etc are not an option.

Working example, but visually incorrect

import java.awt.Color;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class Chooser extends JPanel {


    public Chooser(){
        this.setLayout(new GridLayout(0,1));

        JPanel labelPanel = new JPanel();
        JLabel label = new JLabel("Choose a file:");
        labelPanel.add(label);
        labelPanel.setBackground(Color.red);

        JPanel buttonPanel = new JPanel();
        buttonPanel.add(new JButton("long pathname to a file goes here"));
        buttonPanel.setBackground(Color.blue);

        this.add(labelPanel);
        this.add(buttonPanel);
    }


    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Chooser c = new Chooser();
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setAccessory(c);
        fileChooser.showOpenDialog(null);
    }

}
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
KyleM
  • 4,445
  • 9
  • 46
  • 78
  • 2
    Why not show us your best attempt with a minimal but sufficient example, an [sscce](http://sscce.org)? – Hovercraft Full Of Eels Sep 22 '11 at 21:36
  • @Hovercraft Then I might as well copy paste any random example of Swing JPanel code since I've tried tons of various things. What use would an example of a working but not visually correct BoxLayout be? To prove that I tried? – KyleM Sep 22 '11 at 21:52
  • 1
    No, I believe you that you've tried, but if you post an SSCCE, we'll get an idea of the current structure of your code and problem and better understand your problem, and if we understand it better, we help you better. If we modify ***your*** code example, you'll understand our code better. And quite frankly, it will save us time and make it easier for us to help you since you will provide the initial framework for our code. But, up to you as it's your problem to solve. – Hovercraft Full Of Eels Sep 22 '11 at 21:59
  • 1
    No, it would give readers an idea of what you want, and it would give you the opportunity to get feedback on your approach. See [A Visual Guide to Layout Managers](http://download.oracle.com/javase/tutorial/uiswing/layout/visual.html) in general and [How to Use BoxLayout: Specifying Component Sizes](http://download.oracle.com/javase/tutorial/uiswing/layout/box.html#size) in particular. – trashgod Sep 22 '11 at 22:02
  • @KyleM `as well copy paste any random example of Swing` then I think that you are post your question on wrong forum – mKorbel Sep 22 '11 at 22:02
  • `JButton`? Why not a `JTextField(String text, int columns)`, which can scroll horizontally? – trashgod Sep 22 '11 at 22:07
  • @trashgod I was asking for advice on what approach to take, not for advice on my current approach. That's why I did not provide an example. – KyleM Sep 22 '11 at 22:11
  • @trashgod I simply need a clickable area that can register an event and that will "highlight" whatever area is selected. A JTextField with more than one column can do that? – KyleM Sep 22 '11 at 22:12
  • A JList or JTable should be able to do this. – Hovercraft Full Of Eels Sep 22 '11 at 22:13

1 Answers1

6

What about something where a GridLayout is nested in a BorderLayout (actually in a JScrollPane)...

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class Chooser extends JPanel {
   private static final String[] BUTTON_TEXTS = { "Hello", "Goodbye",
         "Hello Goodbye", "Adios", "This is a long String for WTF" };

   public Chooser() {
      this.setLayout(new BorderLayout());

      JPanel labelPanel = new JPanel();
      JLabel label = new JLabel("Choose a file:");
      labelPanel.add(label);
      labelPanel.setBackground(Color.red);

      JPanel buttonPanel = new JPanel(new GridLayout(0, 1));
      for (int i = 0; i < BUTTON_TEXTS.length; i++) {
         buttonPanel.add(new JButton(BUTTON_TEXTS[i]));
      }
      buttonPanel.add(Box.createVerticalGlue()); // to pad the bottom if needed
      buttonPanel.setBackground(Color.blue);

      this.add(labelPanel, BorderLayout.PAGE_START);
      this.add(new JScrollPane(buttonPanel), BorderLayout.CENTER);
   }

   public static void main(String[] args) {
      Chooser c = new Chooser();
      JFileChooser fileChooser = new JFileChooser();
      fileChooser.setAccessory(c);
      fileChooser.showOpenDialog(null);
   }

}

Example 2 with a simple JList that places its selection in the file chooser:

import java.awt.*;
import java.io.File;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class Chooser extends JPanel {
   private static final String[] BUTTON_TEXTS = { "Hello", "Goodbye",
         "Hello Goodbye", "Adios", "This is a long String for WTF", "Hello",
         "Goodbye", "Hello Goodbye", "Adios", "This is a long String for WTF" };

   public Chooser(final JFileChooser fileChooser) {
      setLayout(new BorderLayout());

      JPanel labelPanel = new JPanel();
      JLabel label = new JLabel("Choose a file:");
      labelPanel.add(label);
      labelPanel.setBackground(Color.red);

      final JList list = new JList(BUTTON_TEXTS);
      list.addListSelectionListener(new ListSelectionListener() {
         public void valueChanged(ListSelectionEvent e) {
            String selectedString = list.getSelectedValue().toString();
            fileChooser.setSelectedFile(new File(selectedString));
         }
      });

      add(labelPanel, BorderLayout.PAGE_START);
      add(new JScrollPane(list), BorderLayout.CENTER);
   }

   public static void main(String[] args) {
      JFileChooser fileChooser = new JFileChooser();
      Chooser c = new Chooser(fileChooser);
      fileChooser.setAccessory(c);
      fileChooser.showOpenDialog(null);
   }

}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Thanks so much. This is a great place to start. Personally I think the JList looks funny in the JFileChooser but I might add an icon to each JListItem (whatever they're called) and see how it looks. – KyleM Sep 22 '11 at 22:22
  • @KyleM: note edit to second program allowing the JList to place its selection in the JFileChooser's text field. – Hovercraft Full Of Eels Sep 22 '11 at 22:26
  • thanks, appreciate that. I did have that working already. In fact all my functionality works I just suck at layouts apparently. I'm still trying to figure out how JFileChooser internally lists its files (so I can mimic that) but I'm stuck on that thus far too, wading through source code. – KyleM Sep 22 '11 at 22:48
  • @Hovercraft Full Of Eels I have big problems with you (JList and Mouse) example, will be asked (later) as my question +1 – mKorbel Sep 23 '11 at 06:37
  • nevermind, was thinking of another question. Thanks again for your help. Marked as answer. – KyleM Oct 05 '11 at 21:32