I have created a JOptionPane that pulls up a custom JPanel that takes in 2 JTextField's & A JComboBox. Upon hitting Save I would like to have the 3 values stored into global variables but have little experience with JOptionPane and making this work I have the following method that instantiates it:
public void add() {
JOptionPane.showOptionDialog(null,
getPanel(),
"Add A Shipment ",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.INFORMATION_MESSAGE, null,
new String[]{"Add Shipment", "Cancel"},"default");
}
and the method that creates the custom popup
#SuppressWarnings("unchecked")
private JPanel getPanel() {
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout (8,8));
//NORTH PANEL
JPanel name = new JPanel();
name.setLayout(new BorderLayout(8,8));
name.add(new JLabel("Title: "), BorderLayout.WEST);
JTextField titleIn = new JTextField();
titleIn.setPreferredSize(new Dimension(150, 20));
name.add(titleIn, BorderLayout.EAST);
//CENTER PANEL
JPanel trackID = new JPanel();
trackID.setLayout(new BorderLayout(8,8));
trackID.add(new JLabel("Tracking #: "), BorderLayout.WEST);
JTextField trackIn = new JTextField();
trackIn.setPreferredSize(new Dimension(150, 20));
trackID.add(trackIn, BorderLayout.EAST);
//BOTTOM PANEL
JPanel ship = new JPanel();
ship.setLayout(new BorderLayout(8,8));
String[] services = { "USPS", "UPS", "FedEx", "DHL" };
#SuppressWarnings("rawtypes")
JComboBox service = new JComboBox(services);
service.setSelectedIndex(0);
ship.add(service);
panel.add(name, BorderLayout.NORTH);
panel.add(trackID, BorderLayout.CENTER);
panel.add(ship, BorderLayout.SOUTH);
return panel;
}
Any easy way of assigning these as variables with the given code?
Thanks
You need to create a class that extends JPanel, store the controls (the two JTextFields and the JComboBox) as fields of this class, pass an instance of this class to JOptionPane.showOptionDialog() and retrieve the values from the fields after the call.
Related
import javax.swing.*;
import java.awt.*;
public class SQlUI {
public static void main(String[] args){
SQlUI user=new SQlUI();
user.go();
}
public void go(){
//Creating a Frame
JFrame frame=new JFrame();
//Creating three Panels
JPanel panel0=new JPanel();
JPanel panel1=new JPanel();
JPanel panel2=new JPanel();
//Creating three Buttons
JButton button0=new JButton("INSERT");
JButton button1=new JButton("UPDATE");
JButton button2=new JButton("DELETE");
//Adding panel0 to the frame which contains three butoon objects
frame.getContentPane().add(BorderLayout.SOUTH,panel0);
panel0.add(button0);
panel0.add(button1);
panel0.add(button2);
//Creating four textbox
JTextField textbox0 = new JTextField(120);
JTextField textbox1 = new JTextField(120);
JTextField textbox2 = new JTextField(120);
JTextField textbox3 = new JTextField(120);
//Adding panel1 to the frame which contains four textbox objects
frame.getContentPane().add(BorderLayout.EAST,panel1);
//Using BoxLayout managaer for panel1 objects
panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS));
panel1.add(textbox0);
panel1.add(textbox1);
panel1.add(textbox2);
panel1.add(textbox3);
//Adding panel2 to the frame which contains four label objects
frame.getContentPane().add(BorderLayout.WEST,panel2);
//Using BoxLayout managaer for panel1 objects
panel2.setLayout(new BoxLayout(panel2, BoxLayout.Y_AXIS));
//Creating four labels
JLabel label0=new JLabel("Name");
label0.setSize(50,50);
label0.setVisible(true);
JLabel label1=new JLabel("ID");
label1.setSize(50,50);
label1.setVisible(true);
JLabel label2=new JLabel("AGE");
label2.setSize(50,50);
label2.setVisible(true);
JLabel label3=new JLabel("ADDRESS");
label3.setSize(50,50);
label3.setVisible(true);
//Adding labels to panel2
panel2.add(label0);
panel2.add(label1);
panel2.add(label2);
panel2.add(label3);
//Setting frame size and visiblity
frame.setSize(500,500);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Details about the GUI- I am trying to create a GUI which have four textbox where user can enter his details and there are three buttons available on GUI by which user can send the data or store the data. The GUI also contains labels which tells the user which textbox is for which type of data.
Now the allignment of the objects are not coming the way i wanted it. All the labels are coming at one place and the size of box is very big. Tried to find a solution on stalkoverflow but got nothing.
i want something like this----
Name- textbox0
ID- textbox1
AGE- textbox2
ADDRESS- textbox3
Insert Update Delete
but getting something like----
textbox0
textbox1
Name
ID
AGE
Address textbox2
textbox3
Insert Update Delete
This is not the best looking thing you can do, but it's simple and a good start for you.
public class SQlUI {
public static void main(String[] args) {
SQlUI user = new SQlUI();
user.go();
}
public void go() {
JFrame frame = new JFrame();
JPanel buttonsPanel = new JPanel();
JButton insert = new JButton("INSERT");
JButton update = new JButton("UPDATE");
JButton delete = new JButton("DELETE");
buttonsPanel.add(insert);
buttonsPanel.add(update);
buttonsPanel.add(delete);
frame.getContentPane().add(buttonsPanel, BorderLayout.SOUTH);
JPanel centerPanel = new JPanel();
centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.Y_AXIS));
JPanel namePanel = new JPanel();
JLabel nameLabel = new JLabel("Name");
JTextField nameField = new JTextField(120);
namePanel.add(nameLabel);
namePanel.add(nameField);
JPanel idPanel = new JPanel();
JLabel idLabel = new JLabel("ID");
JTextField idField = new JTextField(120);
idPanel.add(idLabel);
idPanel.add(idField);
centerPanel.add(namePanel);
centerPanel.add(idPanel);
frame.getContentPane().add(centerPanel, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Notice that I call frame.getContentPane().add(buttonsPanel, BorderLayout.SOUTH); with the panel as the first argument and the position second, you did it the other way around.
Not sure why you need text fields of length 120.
Call frame.pack() instead of setting the size yourself.
See what indentation does to the code.
You can complete the rest of the panels by yourself.
I suggest reading up on different layouts here. It'll give you more of an idea how you can lay your items out. You can then experiment to find out which one you actually want.
That said I find it far easier to design a GUI graphically, and IntelliJ has a pretty good GUI builder that I've often used. You'll then be able to play around with layout managers and JPanels to get the effect you actually want.
I've been at this for a good while now, but I cannot seem to get a hand of it. I'm trying to produce a JPanel that has a JTextArea above and two JLabels below, but my JLabel ends up on the left side of my JTextArea and I cannot make the other appear.
Here's my code (sorry for the display stuff- just filler really):
public JPanel contentPane() {
JPanel something = new JPanel();
String information = "Please";
info = new JTextArea(information, 4, 30);
info.setEditable(false);
info.setLineWrap(true);
info.setWrapStyleWord(true);
JPanel one = new JPanel(new BorderLayout());
one.setBackground(Color.WHITE);
one.setLocation(10, 10);
one.setSize(50, 50);
one.add(info, BorderLayout.CENTER);
something.add(one, BorderLayout.NORTH);
JPanel two = new JPanel(new BorderLayout());
two.setBackground(null);
two.setLocation(220, 10);
two.setSize(50, 50);
two.add(new JLabel("Please work"), BorderLayout.EAST);
two.add(new JLabel("Oh gosh, please"), BorderLayout.WEST);
something.add(two, BorderLayout.SOUTH);
something.setOpaque(true);
return something;
}
public static void GUI() {
JFrame frame = new JFrame("You Guessed It!");
DisplayStudent panel = new DisplayStudent();
frame.setContentPane(panel.contentPane());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 150);
frame.setVisible(true);
}
Please and thank you to anyone who takes the time to help.
When you create you something, you don't specify any layout manager, but later on you attempt to add one to something using BorderLayout constants -- which will not work, since the default layout manager for a JPanel is FlowLayout.
Try this instead;
JPanel something = new JPanel(new BorderLayout());
I can do this:
I have a JFrame with 2 JPanel objects. In the panel on the left, I want to put the components of Java Swing useful for the construction of an user interface. The panel on the right is empty.
On run-time, my user should to be able to copy with drag and drop drag from one panel to another the selected component. But the copied component on the right panel, can be moved or deleted.
I have created the panel, but I don't know if is better inserire the component as image, label or true component.. and how to make this possible..
This is my panel for the customization.. I have to insert the component in the JTabbedPane in the PanelSx..
public class Customize extends JFrame {
private JPanel panelSx, panelCx, panelMobile;
private JButton buttonSave;
private TabbedPaneComponents tpc;
public Customize(){
Container c = getContentPane();
c.setLayout(new BorderLayout());
setResizable(true);
setTitle("Design Preview");
setSize(800, 650);
setLocation(250,50);
panelSx = new JPanel();
panelSx.setPreferredSize(new Dimension(300, 200));
panelSx.setOpaque(true);
panelSx.setBackground(Color.RED.darker());
panelCx = new JPanel();
panelCx.setPreferredSize(new Dimension(200, 200));
panelCx.setOpaque(true);
panelCx.setBackground(Color.BLUE);
// display panel
panelMobile = new JPanel();
panelMobile.setPreferredSize(new Dimension(300,500));
panelMobile.setOpaque(true);
panelMobile.setBackground(Color.PINK.darker());
panelMobile.setFocusable(false);
buttonSave = new JButton("Save");
panelCx.add(panelMobile);
c.add(panelSx, BorderLayout.WEST);
c.add(panelCx, BorderLayout.CENTER);
tpc = new TabbedPaneComponents();
panelSx.add(tpc);
}
}
public class TabbedPaneComponents extends JTabbedPane{
private JPanel panel1,panel2,panel3;
public TabbedPaneComponents(){
panel1 = new JPanel();
panel1.setPreferredSize(new Dimension(200,300));
addTab("Form Widgets", panel1);
panel1 = new JPanel();
panel1.setPreferredSize(new Dimension(200,300));
addTab("Text Field", panel2);
panel1 = new JPanel();
panel1.setPreferredSize(new Dimension(200,300));
addTab("Other", panel3);
}
}
I would have to suggest using a JToolBar because of it's built in drag'n'drop features. I would put the component inside of the toolbar.
below is my user class for guiFrames. I want a class that creates several JFrames with methods and pass those methods into cardLayout. The reason for this is each JFrame will be have different buttons displayed depending on what the user has selected.
So, I thought I would create methods for the individual panels and depending on the parameter passed have different buttons displayed. I need the panels to be displayed in a cardLayout. But I am unable to pass the pass the methods into the cardLayout.add(); because it says the method type is invalid. So I tried to make the method return a Component but its not working out. Help please.
import javax.swing.*;
import java.awt.*;
public class guiFrames extends JFrame{
public guiFrames(){
}
public Component inputFrame(){
JFrame inputFrame = new JFrame("Input");
JPanel inputPnl = new JPanel();
inputPnl.setLayout(new GridLayout(3,2));
JLabel loginLbl = new JLabel("Login");
inputPnl.add(loginLbl);
JTextField loginTxt = new JTextField();
inputPnl.add(loginTxt);
JLabel pwLbl = new JLabel("Password");
inputPnl.add(pwLbl);
JTextField pwTxt = new JTextField();
inputPnl.add(pwTxt);
JPanel buttonPnl = new JPanel();
buttonPnl.setLayout(new FlowLayout(FlowLayout.LEFT, 1,5));
JButton submit = new JButton("Submit");
buttonPnl.add(submit);
JButton output = new JButton("Output");
buttonPnl.add(output);
JPanel container = new JPanel();
container.setLayout(new BorderLayout());
container.add(inputPnl, BorderLayout.CENTER);
container.add(buttonPnl, BorderLayout.SOUTH);
inputFrame.add(container);
inputFrame.pack();
inputFrame.setVisible(true);
inputFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
return;
}
public void cardView(){
JFrame cardFrame = new JFrame();
JPanel cardGUI = new JPanel();
CardLayout cards = new CardLayout();
cardGUI.setLayout(cards);
cardGUI.add(inputFrame(), "first");
cardFrame.add(cardGUI, BorderLayout.CENTER);
cardFrame.pack();
cardFrame.setVisible(true);
cardFrame.setDefaultCloseOperation(cardFrame.EXIT_ON_CLOSE);
}
}
At the end of inputFrame() you aren't returning anything. You need to return the inputFrame, like this:
return inputFrame;
Hope that helps.
What would be the easiest way for creating a dialog:
in one window I'm giving data for envelope addressing, also set font type from list of sizes
when clicked OK, in the same window or in next window I get preview of how the envelope would look like with the given names, and used selected font size
It should look similarly to:
alt text http://img15.imageshack.us/img15/7355/lab10aa.gif
Should I use Jdialog? Or will JOptionPane will be enough? The next step will be to choose color of font and background so I must keep that in mind.
This should get you going.
class TestDialog extends JDialog {
private JButton okButton = new JButton(new AbstractAction("ok") {
public void actionPerformed(ActionEvent e) {
System.err.println("User clicked ok");
// SHOW THE PREVIEW...
setVisible(false);
}
});
private JButton cancelButton = new JButton(new AbstractAction("cancel") {
public void actionPerformed(ActionEvent e) {
System.err.println("User clicked cancel");
setVisible(false);
}
});
private JTextField nameField = new JTextField(20);
private JTextField surnameField = new JTextField();
private JTextField addr1Field = new JTextField();
private JTextField addr2Field = new JTextField();
private JComboBox sizes = new JComboBox(new String[] { "small", "large" });
public TestDialog(JFrame frame, boolean modal, String myMessage) {
super(frame, "Envelope addressing", modal);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
getContentPane().add(mainPanel);
JPanel addrPanel = new JPanel(new GridLayout(0, 1));
addrPanel.setBorder(BorderFactory.createTitledBorder("Receiver"));
addrPanel.add(new JLabel("Name"));
addrPanel.add(nameField);
addrPanel.add(new JLabel("Surname"));
addrPanel.add(surnameField);
addrPanel.add(new JLabel("Address 1"));
addrPanel.add(addr1Field);
addrPanel.add(new JLabel("Address 2"));
addrPanel.add(addr2Field);
mainPanel.add(addrPanel);
mainPanel.add(new JLabel(" "));
mainPanel.add(sizes);
JPanel buttons = new JPanel(new FlowLayout());
buttons.add(okButton);
buttons.add(cancelButton);
mainPanel.add(buttons);
pack();
setLocationRelativeTo(frame);
setVisible(true);
}
public String getAddr1() {
return addr1Field.getText();
}
// ...
}
Result:
If you need to use JOptionPane :
import java.awt.*;
import javax.swing.*;
public class Main extends JFrame {
private static JTextField nameField = new JTextField(20);
private static JTextField surnameField = new JTextField();
private static JTextField addr1Field = new JTextField();
private static JTextField addr2Field = new JTextField();
private static JComboBox sizes = new JComboBox(new String[] { "small", "medium", "large", "extra-large" });
public Main(){
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
getContentPane().add(mainPanel);
JPanel addrPanel = new JPanel(new GridLayout(0, 1));
addrPanel.setBorder(BorderFactory.createTitledBorder("Receiver"));
addrPanel.add(new JLabel("Name"));
addrPanel.add(nameField);
addrPanel.add(new JLabel("Surname"));
addrPanel.add(surnameField);
addrPanel.add(new JLabel("Address 1"));
addrPanel.add(addr1Field);
addrPanel.add(new JLabel("Address 2"));
addrPanel.add(addr2Field);
mainPanel.add(addrPanel);
mainPanel.add(new JLabel(" "));
mainPanel.add(sizes);
String[] buttons = { "OK", "Cancel"};
int c = JOptionPane.showOptionDialog(
null,
mainPanel,
"My Panel",
JOptionPane.DEFAULT_OPTION,
JOptionPane.PLAIN_MESSAGE,
null,
buttons,
buttons[0]
);
if(c ==0){
new Envelope(nameField.getText(), surnameField.getText(), addr1Field.getText()
, addr2Field.getText(), sizes.getSelectedIndex());
}
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
public static void main(String[] args) {
new Main();
}
}
You will need to use JDialog. No point messing about with JOptoinPane - it's not meant for gathering more than a simple string. Additionally, use either MigLayout, TableLayout, or JGoodies forms - it will help you get a nice layout that's easy to code.
If it is allowed to use a GUI builder I would recommend you IntelliJ IDEA's
You can create something like that in about 5 - 10 mins.
If that's not possible ( maybe you want to practice-learn-or-something-else ) I would use a JFrame instead ) with CardLayout
Shouldn't be that hard to do.
You can use JOptionPane. You can add any Swing component to it.
Create a panel with all the components you need except for the buttons and then add the panel to the option pane. The only problem with this approach is that focus will be on the buttons by default. To solve this problem see the solution presented by Dialog Focus.