Reputation: 51
I have this method to print and set the material properties of a Solid Object in a class called MaterialProperties, which has printMaterial & setMaterial methods.
public void Btn3_callback ( ) throws Exception {
Model model = session.GetCurrentModel();
if (model == null) {
mesg = "No Model Selected!!";
//TextField.Area("No Model Selected!!");
System.exit(0);
}
else {
Solid solid= (Solid) model;
String newMaterial="copper";//user input for new Material name
printMaterial(solid);//printMaterial print the Material properties of the Solid object
setMaterial(solid,newMaterial);//setMaterial sets the Material properties of the Solid object to the material entered
}
}
I need to get the user input for newMaterial instead of hard coding it. What I need to do is to display all the Material types avaialable, so that the user can just select the material required. So I tried to do this using JFrame. Here's the code I have:
public class MaterialWindow {
JFrame frame = new JFrame("Material Selection");
public MaterialWindow(){
// Directory path here
String path = "W:\\materials";
JFrame frame = new JFrame("Material Selection");
JPanel panel = new JPanel(new GridLayout(0, 4));
ButtonGroup bg = new ButtonGroup();
String files;
File folder = new File(path);
File[] listOfFiles = folder.listFiles();
JRadioButton button;
for (int i = 0; i < listOfFiles.length; i++)
{
if (listOfFiles[i].isFile())
{
files = listOfFiles[i].getName();
if (files.endsWith(".mtl") || files.endsWith(".MTL"))
{
button = new JRadioButton(files);
panel.add(first,BorderLayout.CENTER);
panel.revalidate();
bg.add(button);
first.addActionListener(new MyAction());
}
}
}
frame.add(panel, BorderLayout.NORTH);
frame.getContentPane().add(new JScrollPane(panel), BorderLayout.CENTER);
frame.setSize(1000, 400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public class MyAction implements ActionListener{
public void actionPerformed(ActionEvent e){
String newMaterial =e.getActionCommand();
String[] split = newMaterial.split("\\.");
newMaterial = split[0];
newMaterial.trim();
//set the newMaterial for btn3_callback OR call the setMaterial method of MaterialPropeties class
frame.dispose();
}
}
}
Now the problem is how can I use the newMaterial string selected from the radio button to newMaterial in my Btn3_callback() function? When I create a getString() method for newMaterial in the class MyAction and use that it Btn3_callback it always returns null;
Is there any way I can do this? Or any different way I can implement the idea? Thanks
Upvotes: 2
Views: 998
Reputation: 168825
Use a JOptionPane
instead of the frame. Put a list (JList
..or JComboBox
) of options in the option pane and query the component once returned (the pane is closed), for the selected object.
The GUI should be created and altered on the EDT (batteries not included).
import java.io.File;
import javax.swing.*;
public class QuickTest {
public static void main(String[] args) throws Exception {
File[] files = new File(System.getProperty("user.home")).listFiles();
JFrame f = new JFrame("Faux J-Link");
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JEditorPane jep = new JEditorPane();
f.add(new JScrollPane(jep));
f.setSize(600,400);
f.setLocationByPlatform(true);
f.setVisible(true);
JComboBox choices = new JComboBox(files);
int result = JOptionPane.showConfirmDialog(f, choices);
if (result==JOptionPane.OK_OPTION) {
System.out.println("OK");
File file = files[choices.getSelectedIndex()];
jep.setPage(file.toURI().toURL());
}
}
}
Upvotes: 4