Issue
I want to add buttons and their number is not certain. While they adding, their location must set dynamically. I am using Netbeans IDE's designer therefore frames layout is GroupLayout. How can I do this in GroupLayout? Thanks for help.
package addButton;
import javax.swing.JButton;
/**
*
* @author 12043
*/
public class MyGui extends javax.swing.JFrame {
/**
* Creates new form MyGui
*/
public MyGui() {
initComponents();
addButtons(3);
}
private void addButtons(int numberOfButtons) {
for (int i = 0; i < numberOfButtons; i++) {
JButton newButton = new JButton("NewButton" + i);
newButton.setSize(newButton.getPreferredSize());
newButton.setLocation(10, i*30);
newButton.setVisible(true);
add(newButton);
}
}
//netbeans code after here
The frame is looking like:
The buttons in the gui will looks like in android launcher. Their locations will sort by rows and colums. But since number of buttons is not certain I must do this automatically.
Solution
The buttons in the gui will look like in android launcher..
It looks something like this..
But of course, a desktop computer does not understand 'swiping' to the next screen, so we have keyboards & mice to change the view and scroll panes for long lists.
Here is the source code that created the above GUI. You might have to change the paths and file type at the top, but it will search for executable files in the top most level of directories indicated in the path string.
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.io.*;
import java.util.Vector;
import javax.swing.filechooser.FileSystemView;
public class ExecutableList {
private JComponent ui = null;
String exePathString = "C:\\Windows";
String exeFileType = "exe";
ExecutableList() {
initUI();
}
public final void initUI() {
if (ui != null) {
return;
}
ui = new JPanel(new BorderLayout(4, 4));
ui.setBorder(new EmptyBorder(4, 4, 4, 4));
File[] fAll0 = new File(exePathString).listFiles();
Vector<File> v = new Vector<>();
for (File f0 : fAll0) {
if (f0.isDirectory()) {
FilenameFilter fNF = new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.toLowerCase().endsWith("exe");
}
};
File[] fExe0 = f0.listFiles(fNF);
System.out.println("fExe0: ");
if (fExe0 != null
) {
//&& fExe0.length<100) {
System.out.println("fExe0: " + fExe0.length);
for (File f1 : fExe0) {
v.add(f1);
}
}
}
}
System.out.println("v.size(): " + v.size());
DefaultListModel dlm = new DefaultListModel();
for (File f : v) {
dlm.addElement(f);
}
JList list = new JList(dlm);
list.setCellRenderer(new File2CellRenderer());
list.setVisibleRowCount(v.size()/5);
list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
list.setFixedCellHeight(20);
list.setFixedCellWidth(30);
ui.add(new JScrollPane(
list, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER));
}
public JComponent getUI() {
return ui;
}
public static void main(String[] args) {
Runnable r = () -> {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception useDefault) {
}
ExecutableList o = new ExecutableList();
JFrame f = new JFrame(o.getClass().getSimpleName());
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);
f.setContentPane(o.getUI());
f.pack();
Dimension d = f.getSize();
d = new Dimension(d.width, d.height/3);
f.setMinimumSize(d);
f.setSize(d);
f.setVisible(true);
};
SwingUtilities.invokeLater(r);
}
}
/** A cell renderer for a File. */
class File2CellRenderer implements ListCellRenderer {
private FileSystemView fileSystemView;
private JLabel label;
File2CellRenderer() {
label = new JLabel();
label.setOpaque(true);
fileSystemView = FileSystemView.getFileSystemView();
}
@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
File file = (File)value;
label.setIcon(fileSystemView.getSystemIcon(file));
//label.setText(fileSystemView.getSystemDisplayName(file));
label.setToolTipText(file.getName());
if (isSelected) {
label.setBackground(Color.RED);
label.setForeground(Color.YELLOW);
} else {
label.setBackground(Color.WHITE);
label.setForeground(Color.BLACK);
}
return label;
}
}
Answered By - Andrew Thompson
Answer Checked By - Terry (JavaFixing Volunteer)