I have no idea what am doing wrong anymore, heres how it all looks
Notice the last 3 panes are incorrect in terms of alignment yet they use the same code as the first two. Heres the code.
import javax.swing.BorderFactory;
public class View_RegisterMember extends JDialog {
private static final long serialVersionUID = -4610105372782292470L;
private JDialog dialog;
// Panels
private JPanel pdPanel;
private JPanel addressPanel;
private JPanel cdpanel;
private JPanel memDetails;
private Border blackline;
private JLabel lblTitle;
private JLabel lblFirstName;
private JLabel lblSurname;
private JLabel lblStreetNo;
private JLabel lblStreetName;
private JLabel lblCity;
private JLabel lblPostcode;
private JLabel lblPhoneNo;
private JLabel lblEmail;
private JLabel lblMembershipType;
private JLabel lblPrice;
private JLabel lblpriceData;
private JLabel lblDob;
// textfields
private JTextField textphoneData;
private JTextField textFieldEmailData;
#SuppressWarnings("rawtypes")
private JComboBox comboBoxTitle;
private JTextField textFieldfName;
private JTextField textFieldlName;
private JTextField textFieldNumber;
private JTextField textFieldStreetName;
private JTextField textFieldCity;
private JTextField textFieldPostCode;
private JTextField textFieldDOB;
private JComboBox<String> comboBoxMemType;
private JButton btnSave;
private JPanel bdPanel;
private JLabel lblAccountName;
private JTextField textFieldAccName;
private JLabel labelSortCode;
private JTextField textFieldSortCode;
private JLabel lblAccNo;
private JTextField textFieldAccNo;
public View_RegisterMember(final JFrame frame) {
dialog = this;
blackline = BorderFactory.createLineBorder(Color.black);
dialog.setTitle("Member Registration");
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent arg0) {
frame.setEnabled(true);
}
});
GridBagLayout gridBagLayout = new GridBagLayout();
createBagLayouts(gridBagLayout);
getContentPane().setLayout(gridBagLayout);
createPersonalDetailsPane();
createAddressDetailsPane();
createContactDetailsPane();
createBankDetailsPane();
createMemberTypeDetailsPane();
// createAppointmentsPane();
// frame.setSize(new Dimension(335, 650));
btnSave = new JButton("Register");
btnSave.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
btnSave.setFont(new Font("Dialog", Font.PLAIN, 18));
GridBagConstraints gbc_btnNewButton = new GridBagConstraints();
gbc_btnNewButton.fill = GridBagConstraints.HORIZONTAL;
gbc_btnNewButton.insets = new Insets(0, 0, 5, 5);
gbc_btnNewButton.gridx = 1;
gbc_btnNewButton.gridy = 10;
getContentPane().add(btnSave, gbc_btnNewButton);
dialog.setResizable(true);
dialog.pack();
dialog.validate();
dialog.repaint();
}
#SuppressWarnings({ "unchecked", "rawtypes" })
private void createPersonalDetailsPane() {
pdPanel = new JPanel();
GridBagConstraints gbc_dpPanel = new GridBagConstraints();
gbc_dpPanel.fill = GridBagConstraints.HORIZONTAL;
gbc_dpPanel.insets = new Insets(0, 0, 5, 5);
gbc_dpPanel.gridx = 1;
gbc_dpPanel.gridy = 1;
getContentPane().add(pdPanel, gbc_dpPanel);
GridBagLayout gbl_panel = new GridBagLayout();
gbl_panel.columnWeights = new double[]{0.0, 1.0};
pdPanel.setBorder(new TitledBorder(blackline, "Personal Details",
TitledBorder.LEADING, TitledBorder.ABOVE_TOP, null,
SystemColor.inactiveCaptionText));
pdPanel.setLayout(gbl_panel);
lblTitle = new JLabel("Title:");
//lblTitle.setHorizontalAlignment(SwingConstants.LEFT);
GridBagConstraints gbc_lblTitle = new GridBagConstraints();
gbc_lblTitle.anchor = GridBagConstraints.WEST;
gbc_lblTitle.insets = new Insets(0, 0, 5, 5);
gbc_lblTitle.gridx = 0;
gbc_lblTitle.gridy = 0;
pdPanel.add(lblTitle, gbc_lblTitle);
lblTitle.setFont(new Font("Dialog", Font.PLAIN, 18));
try {
comboBoxTitle = new JComboBox(Controller_Registration.GetTitles());
} catch (Exception_Database e) {
utilities.DisplayError(e.getMessage(), dialog);
}
comboBoxTitle.setFont(new Font("Dialog", Font.PLAIN, 18));
GridBagConstraints gbc_comboBoxTitle = new GridBagConstraints();
gbc_comboBoxTitle.anchor = GridBagConstraints.EAST;
//gbc_comboBoxTitle.fill = GridBagConstraints.BOTH;
gbc_comboBoxTitle.insets = new Insets(0, 0, 5, 0);
gbc_comboBoxTitle.gridx = 1;
gbc_comboBoxTitle.gridy = 0;
// Minus the one to match up the SQL ids with the index
comboBoxTitle.setSelectedIndex(1);
pdPanel.add(comboBoxTitle, gbc_comboBoxTitle);
lblFirstName = new JLabel("Name:");
//lblFirstName.setHorizontalAlignment(SwingConstants.LEFT);
GridBagConstraints gbc_lblFirstName = new GridBagConstraints();
gbc_lblFirstName.anchor = GridBagConstraints.WEST;
gbc_lblFirstName.insets = new Insets(0, 0, 5, 5);
gbc_lblFirstName.gridx = 0;
gbc_lblFirstName.gridy = 1;
pdPanel.add(lblFirstName, gbc_lblFirstName);
lblFirstName.setFont(new Font("Dialog", Font.PLAIN, 18));
textFieldfName = new JTextField();
textFieldfName.setFont(new Font("Dialog", Font.PLAIN, 18));
GridBagConstraints gbc_textFieldfName = new GridBagConstraints();
gbc_textFieldfName.anchor = GridBagConstraints.EAST;
gbc_textFieldfName.insets = new Insets(0, 0, 5, 0);
gbc_textFieldfName.gridx = 1;
gbc_textFieldfName.gridy = 1;
pdPanel.add(textFieldfName, gbc_textFieldfName);
textFieldfName.setColumns(10);
lblSurname = new JLabel("Surname:");
//lblSurname.setHorizontalAlignment(SwingConstants.LEFT);
GridBagConstraints gbc_lblSurname = new GridBagConstraints();
gbc_lblSurname.anchor = GridBagConstraints.WEST;
gbc_lblSurname.insets = new Insets(0, 0, 5, 5);
gbc_lblSurname.gridx = 0;
gbc_lblSurname.gridy = 2;
pdPanel.add(lblSurname, gbc_lblSurname);
lblSurname.setFont(new Font("Dialog", Font.PLAIN, 18));
textFieldlName = new JTextField();
textFieldlName.setFont(new Font("Dialog", Font.PLAIN, 18));
GridBagConstraints gbc_textFieldlName = new GridBagConstraints();
gbc_textFieldlName.anchor = GridBagConstraints.EAST;
gbc_textFieldlName.insets = new Insets(0, 0, 5, 0);
gbc_textFieldlName.gridx = 1;
gbc_textFieldlName.gridy = 2;
pdPanel.add(textFieldlName, gbc_textFieldlName);
textFieldlName.setColumns(10);
lblDob = new JLabel("D.O.B:");
//lblDob.setHorizontalAlignment(SwingConstants.LEFT);
lblDob.setFont(new Font("Dialog", Font.PLAIN, 18));
GridBagConstraints gbc_lblDob = new GridBagConstraints();
gbc_lblDob.anchor = GridBagConstraints.WEST;
gbc_lblDob.insets = new Insets(0, 0, 5, 5);
gbc_lblDob.gridx = 0;
gbc_lblDob.gridy = 3;
pdPanel.add(lblDob, gbc_lblDob);
textFieldDOB = new JTextField();
textFieldDOB.setFont(new Font("Dialog", Font.PLAIN, 18));
GridBagConstraints gbc_textFieldDOB = new GridBagConstraints();
gbc_textFieldDOB.anchor = GridBagConstraints.EAST;
gbc_textFieldDOB.gridx = 1;
gbc_textFieldDOB.gridy = 3;
pdPanel.add(textFieldDOB, gbc_textFieldDOB);
textFieldDOB.setColumns(10);
}
private void createAddressDetailsPane() {
addressPanel = new JPanel();
GridBagConstraints gbc_addressPanel = new GridBagConstraints();
gbc_addressPanel.fill = GridBagConstraints.HORIZONTAL;
gbc_addressPanel.insets = new Insets(0, 0, 5, 5);
gbc_addressPanel.gridx = 1;
gbc_addressPanel.gridy = 3;
getContentPane().add(addressPanel, gbc_addressPanel);
GridBagLayout gbl_panel1 = new GridBagLayout();
gbl_panel1.columnWeights = new double[] { 0.0, 1.0 };
addressPanel.setBorder(new TitledBorder(blackline, "Address",
TitledBorder.LEADING, TitledBorder.ABOVE_TOP, null,
SystemColor.inactiveCaptionText));
createBagLayouts(gbl_panel1);
addressPanel.setLayout(gbl_panel1);
lblStreetNo = new JLabel("Street No:");
//lblStreetNo.setVerticalAlignment(SwingConstants.TOP);
//lblStreetNo.setHorizontalAlignment(SwingConstants.LEFT);
lblStreetNo.setFont(new Font("Dialog", Font.PLAIN, 18));
GridBagConstraints gbc_lblStreetNo = new GridBagConstraints();
gbc_lblStreetNo.anchor = GridBagConstraints.WEST;
gbc_lblStreetNo.gridx = 0;
gbc_lblStreetNo.insets = new Insets(0, 0, 5, 5);
gbc_lblStreetNo.gridy = 0;
addressPanel.add(lblStreetNo, gbc_lblStreetNo);
textFieldNumber = new JTextField();
textFieldNumber.setFont(new Font("Dialog", Font.PLAIN, 18));
GridBagConstraints gbc_textFieldNumber = new GridBagConstraints();
gbc_textFieldNumber.anchor = GridBagConstraints.EAST;
gbc_textFieldNumber.insets = new Insets(0, 0, 5, 0);
gbc_textFieldNumber.gridx = 1;
gbc_textFieldNumber.gridy = 0;
addressPanel.add(textFieldNumber, gbc_textFieldNumber);
textFieldNumber.setColumns(10);
lblStreetName = new JLabel("Street Name:");
//lblStreetName.setHorizontalAlignment(SwingConstants.CENTER);
lblStreetName.setFont(new Font("Dialog", Font.PLAIN, 18));
GridBagConstraints gbc_lblStreetName = new GridBagConstraints();
gbc_lblStreetName.anchor = GridBagConstraints.WEST;
gbc_lblStreetName.insets = new Insets(0, 0, 5, 5);
gbc_lblStreetName.gridx = 0;
gbc_lblStreetName.gridy = 1;
addressPanel.add(lblStreetName, gbc_lblStreetName);
textFieldStreetName = new JTextField();
textFieldStreetName.setFont(new Font("Dialog", Font.PLAIN, 18));
textFieldStreetName.setColumns(10);
GridBagConstraints gbc_textFieldStreetName = new GridBagConstraints();
gbc_textFieldStreetName.anchor = GridBagConstraints.EAST;
gbc_textFieldStreetName.insets = new Insets(0, 0, 5, 0);
gbc_textFieldStreetName.gridx = 1;
gbc_textFieldStreetName.gridy = 1;
addressPanel.add(textFieldStreetName, gbc_textFieldStreetName);
lblCity = new JLabel("City:");
//lblCity.setHorizontalAlignment(SwingConstants.LEFT);
lblCity.setFont(new Font("Dialog", Font.PLAIN, 18));
GridBagConstraints gbc_lblCity = new GridBagConstraints();
gbc_lblCity.anchor = GridBagConstraints.WEST;
gbc_lblCity.insets = new Insets(0, 0, 5, 5);
gbc_lblCity.gridx = 0;
gbc_lblCity.gridy = 2;
addressPanel.add(lblCity, gbc_lblCity);
textFieldCity = new JTextField();
textFieldCity.setFont(new Font("Dialog", Font.PLAIN, 18));
textFieldCity.setColumns(10);
GridBagConstraints gbc_textFieldCity = new GridBagConstraints();
gbc_textFieldCity.anchor = GridBagConstraints.EAST;
gbc_textFieldCity.insets = new Insets(0, 0, 5, 0);
gbc_textFieldCity.gridx = 1;
gbc_textFieldCity.gridy = 2;
addressPanel.add(textFieldCity, gbc_textFieldCity);
lblPostcode = new JLabel("Postcode:");
lblPostcode.setFont(new Font("Dialog", Font.PLAIN, 18));
GridBagConstraints gbc_lblPostcode = new GridBagConstraints();
gbc_lblPostcode.anchor = GridBagConstraints.WEST;
gbc_lblPostcode.insets = new Insets(0, 0, 5, 5);
gbc_lblPostcode.gridx = 0;
gbc_lblPostcode.gridy = 3;
addressPanel.add(lblPostcode, gbc_lblPostcode);
textFieldPostCode = new JTextField();
textFieldPostCode.setFont(new Font("Dialog", Font.PLAIN, 18));
textFieldPostCode.setColumns(10);
GridBagConstraints gbc_textFieldPostCode = new GridBagConstraints();
gbc_textFieldPostCode.anchor = GridBagConstraints.EAST;
gbc_textFieldPostCode.gridx = 1;
gbc_textFieldPostCode.gridy = 3;
addressPanel.add(textFieldPostCode, gbc_textFieldPostCode);
}
private void createContactDetailsPane() {
cdpanel = new JPanel();
GridBagConstraints gbc_cdpanel = new GridBagConstraints();
gbc_cdpanel.fill = GridBagConstraints.HORIZONTAL;
gbc_cdpanel.insets = new Insets(0, 0, 5, 5);
gbc_cdpanel.gridx = 1;
gbc_cdpanel.gridy = 5;
getContentPane().add(cdpanel, gbc_cdpanel);
GridBagLayout gbl_cdpanel = new GridBagLayout();
gbl_cdpanel.columnWeights = new double[] { 0.0, 1.0 };
cdpanel.setBorder(new TitledBorder(blackline, "Contact Details",
TitledBorder.LEADING, TitledBorder.ABOVE_TOP, null,
SystemColor.inactiveCaptionText));
createBagLayouts(gbl_cdpanel);
cdpanel.setLayout(gbl_cdpanel);
lblPhoneNo = new JLabel("Phone No:");
//lblPhoneNo.setHorizontalAlignment(SwingConstants.LEFT);
lblPhoneNo.setFont(new Font("Dialog", Font.PLAIN, 18));
GridBagConstraints gbc_lblPhoneNo = new GridBagConstraints();
gbc_lblPhoneNo.anchor = GridBagConstraints.WEST;
gbc_lblPhoneNo.insets = new Insets(0, 0, 5, 5);
gbc_lblPhoneNo.gridx = 0;
gbc_lblPhoneNo.gridy = 0;
cdpanel.add(lblPhoneNo, gbc_lblPhoneNo);
textphoneData = new JTextField();
textphoneData.setFont(new Font("Dialog", Font.PLAIN, 18));
GridBagConstraints gbc_textFieldEmailData = new GridBagConstraints();
gbc_textFieldEmailData.anchor = GridBagConstraints.EAST;
gbc_textFieldEmailData.insets = new Insets(0, 0, 5, 0);
gbc_textFieldEmailData.gridx = 1;
gbc_textFieldEmailData.gridy = 0;
cdpanel.add(textphoneData, gbc_textFieldEmailData);
textphoneData.setColumns(10);
lblEmail = new JLabel("Email:");
//lblEmail.setHorizontalAlignment(SwingConstants.LEFT);
lblEmail.setFont(new Font("Dialog", Font.PLAIN, 18));
GridBagConstraints gbc_lblEmail = new GridBagConstraints();
gbc_lblEmail.anchor = GridBagConstraints.WEST;
gbc_lblEmail.insets = new Insets(0, 0, 5, 5);
gbc_lblEmail.gridx = 0;
gbc_lblEmail.gridy = 1;
cdpanel.add(lblEmail, gbc_lblEmail);
textFieldEmailData = new JTextField();
textFieldEmailData.setFont(new Font("Dialog", Font.PLAIN, 18));
GridBagConstraints gbc_textFieldEmailData1 = new GridBagConstraints();
gbc_textFieldEmailData1.anchor = GridBagConstraints.EAST;
gbc_textFieldEmailData1.gridx = 1;
gbc_textFieldEmailData1.gridy = 1;
cdpanel.add(textFieldEmailData, gbc_textFieldEmailData1);
textFieldEmailData.setColumns(10);
}
#SuppressWarnings({ "unchecked", "rawtypes" })
private void createBankDetailsPane() {
bdPanel = new JPanel();
GridBagConstraints gbc_bdPanel = new GridBagConstraints();
gbc_bdPanel.fill = GridBagConstraints.HORIZONTAL;
gbc_bdPanel.insets = new Insets(0, 0, 5, 5);
gbc_bdPanel.gridx = 1;
gbc_bdPanel.gridy = 7;
getContentPane().add(bdPanel, gbc_bdPanel);
GridBagLayout gbl_bdPanel = new GridBagLayout();
gbl_bdPanel.columnWeights = new double[] { 0.0, 1.0 };
bdPanel.setBorder(new TitledBorder(blackline, "Payment Details",
TitledBorder.LEADING, TitledBorder.ABOVE_TOP, null,
SystemColor.inactiveCaptionText));
createBagLayouts(gbl_bdPanel);
bdPanel.setLayout(gbl_bdPanel);
lblAccountName = new JLabel("Acc Name:");
lblAccountName.setFont(new Font("Dialog", Font.PLAIN, 18));
GridBagConstraints gbc_lblAccountName = new GridBagConstraints();
gbc_lblAccountName.anchor = GridBagConstraints.WEST;
gbc_lblAccountName.insets = new Insets(0, 0, 5, 5);
gbc_lblAccountName.gridx = 0;
gbc_lblAccountName.gridy = 0;
bdPanel.add(lblAccountName, gbc_lblAccountName);
textFieldAccName = new JTextField();
textFieldAccName.setFont(new Font("Dialog", Font.PLAIN, 18));
GridBagConstraints gbc_textFieldAccName = new GridBagConstraints();
gbc_textFieldAccName.anchor = GridBagConstraints.EAST;
gbc_textFieldAccName.insets = new Insets(0, 0, 5, 0);
gbc_textFieldAccName.gridx = 1;
gbc_textFieldAccName.gridy = 0;
bdPanel.add(textFieldAccName, gbc_textFieldAccName);
textFieldAccName.setColumns(10);
labelSortCode = new JLabel("Sortcode:");
labelSortCode.setFont(new Font("Dialog", Font.PLAIN, 18));
GridBagConstraints gbc_labelSortCode = new GridBagConstraints();
gbc_labelSortCode.anchor = GridBagConstraints.WEST;
gbc_labelSortCode.insets = new Insets(0, 0, 5, 5);
gbc_labelSortCode.gridx = 0;
gbc_labelSortCode.gridy = 1;
bdPanel.add(labelSortCode, gbc_labelSortCode);
textFieldSortCode = new JTextField();
textFieldSortCode.setFont(new Font("Dialog", Font.PLAIN, 18));
GridBagConstraints gbc_textFieldSortCode = new GridBagConstraints();
gbc_textFieldSortCode.anchor = GridBagConstraints.EAST;
gbc_textFieldSortCode.insets = new Insets(0, 0, 5, 0);
gbc_textFieldSortCode.gridx = 1;
gbc_textFieldSortCode.gridy = 1;
bdPanel.add(textFieldSortCode, gbc_textFieldSortCode);
textFieldSortCode.setColumns(10);
lblAccNo = new JLabel("Acc No:");
lblAccNo.setFont(new Font("Dialog", Font.PLAIN, 18));
GridBagConstraints gbc_lblAccNo = new GridBagConstraints();
gbc_lblAccNo.anchor = GridBagConstraints.WEST;
gbc_lblAccNo.insets = new Insets(0, 0, 5, 5);
gbc_lblAccNo.gridx = 0;
gbc_lblAccNo.gridy = 2;
bdPanel.add(lblAccNo, gbc_lblAccNo);
textFieldAccNo = new JTextField();
textFieldAccNo.setFont(new Font("Dialog", Font.PLAIN, 18));
textFieldAccNo.setColumns(10);
GridBagConstraints gbc_textFieldAccNo = new GridBagConstraints();
gbc_textFieldAccNo.anchor = GridBagConstraints.EAST;
gbc_textFieldAccNo.gridx = 1;
gbc_textFieldAccNo.gridy = 2;
bdPanel.add(textFieldAccNo, gbc_textFieldAccNo);
}
private void createMemberTypeDetailsPane() {
memDetails = new JPanel();
GridBagConstraints gbc_memDetails = new GridBagConstraints();
gbc_memDetails.fill = GridBagConstraints.HORIZONTAL;
//gbc_memDetails.anchor = GridBagConstraints.NORTH;
gbc_memDetails.insets = new Insets(0, 0, 5, 5);
gbc_memDetails.gridx = 1;
gbc_memDetails.gridy = 9;
getContentPane().add(memDetails, gbc_memDetails);
GridBagLayout gbl_memDetails = new GridBagLayout();
gbl_memDetails.columnWeights = new double[] { 0.0, 1.0 };
memDetails.setBorder(new TitledBorder(blackline, "Membership Type",
TitledBorder.LEADING, TitledBorder.ABOVE_TOP, null,
SystemColor.inactiveCaptionText));
createBagLayouts(gbl_memDetails);
memDetails.setLayout(gbl_memDetails);
lblMembershipType = new JLabel("Type:");
lblMembershipType.setFont(new Font("Dialog", Font.PLAIN, 18));
GridBagConstraints gbc_lblMembershipType = new GridBagConstraints();
gbc_lblMembershipType.insets = new Insets(0, 0, 5, 5);
gbc_lblMembershipType.anchor = GridBagConstraints.WEST;
gbc_lblMembershipType.gridx = 0;
gbc_lblMembershipType.gridy = 0;
memDetails.add(lblMembershipType, gbc_lblMembershipType);
comboBoxMemType = new JComboBox(Controller_Member.getMemberTypes(null));
comboBoxMemType.setFont(new Font("Dialog", Font.PLAIN, 18));
GridBagConstraints gbc_comboBoxMemType = new GridBagConstraints();
gbc_comboBoxMemType.anchor = GridBagConstraints.EAST;
gbc_comboBoxMemType.insets = new Insets(0, 0, 5, 0);
gbc_comboBoxMemType.gridx = 1;
gbc_comboBoxMemType.gridy = 0;
memDetails.add(comboBoxMemType, gbc_comboBoxMemType);
lblPrice = new JLabel("Price:");
lblPrice.setFont(new Font("Dialog", Font.PLAIN, 18));
GridBagConstraints gbc_lblPrice = new GridBagConstraints();
gbc_lblPrice.anchor = GridBagConstraints.WEST;
gbc_lblPrice.insets = new Insets(0, 0, 0, 5);
gbc_lblPrice.gridx = 0;
gbc_lblPrice.gridy = 1;
memDetails.add(lblPrice, gbc_lblPrice);
lblpriceData = new JLabel("");
lblpriceData.setFont(new Font("Dialog", Font.PLAIN, 18));
GridBagConstraints gbc_lblpriceData = new GridBagConstraints();
gbc_lblpriceData.anchor = GridBagConstraints.EAST;
gbc_lblpriceData.gridx = 1;
gbc_lblpriceData.gridy = 1;
memDetails.add(lblpriceData, gbc_lblpriceData);
DecimalFormat ob = new DecimalFormat("0.00");
String price = "£"
+ ob.format(Controller_Member.getMembershipTypes()[0]
.GetPrice());
lblpriceData.setText(price);
comboBoxMemType.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
double price = Controller_Member.updatePrice(comboBoxMemType
.getSelectedIndex());
DecimalFormat ob = new DecimalFormat("0.00");
String priceLabel = "£" + ob.format(price);
lblpriceData.setText(priceLabel);
}
});
}
private void createBagLayouts(GridBagLayout gridBagLayout) {
gridBagLayout.columnWidths = new int[] { 0, 0, 0, 0, 0, 0, 0 };
gridBagLayout.rowHeights = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0 };
gridBagLayout.columnWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, Double.MIN_VALUE };
gridBagLayout.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE };
}
}
Have I missed something so simple?
Remove all the calls to the method private void createBagLayouts(GridBagLayout gridBagLayout) except the first one (inside the dialog constructor).
These calls are overwriting your columnWeights settings in your panes and messing things up.
Note that the first pane doesn't have a call to createBagLayouts method and that's the reason why it works. The only reason why the call worked on the second pane is because it is the largest pane and therefore the one determining the entire dialog width.
Related
public class RoteLearnerTabbedPanePage extends JFrame{
private static final long serialVersionUID = 1L;
private JTabbedPane tabbedPane;
private JPanel panel1;
private JPanel transientPanel;
private JPanel nonTransientPanel;
ReadXml rx=new ReadXml();
Set<String> primaryKeys = rx.getPrimaryKeysOfGuiAttributes();
public RoteLearnerTabbedPanePage() throws IOException, SAXException, ErrorCondition
{
tabbedPane=new JTabbedPane();
FlowLayout fl=new FlowLayout();
Document doc=rx.readXmlFile();
Hashtable<String,String> mainclasnames=rx.getMainClassNames(doc, doc.getChildNodes());
Set<XmlClass> guiAttrVals=rx.getGuiAttributeValues(doc, doc.getChildNodes());
int j=-1;
Iterator<Entry<String, String>> mainclass_it = mainclasnames.entrySet().iterator();
while(mainclass_it.hasNext())
{
Entry<String, String> classDet = mainclass_it.next();
String classname = classDet.getValue();
final Hashtable<JLabel,JTextField> uiParams=new Hashtable<JLabel,JTextField>();
JPanel buttons=new JPanel();
JButton buttonLearn = new JButton("Learn");
panel1= new JPanel();
transientPanel=new JPanel(new GridBagLayout());
nonTransientPanel=new JPanel(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
Iterator<XmlClass> guiItr=guiAttrVals.iterator();
while(guiItr.hasNext())
{
XmlClass xmlclasss=guiItr.next();
if(classname.equals(xmlclasss.getName()))
{
Set<XmlAttributes> xmlAttrs=xmlclasss.getAttributes();
Iterator<XmlAttributes> xmlattrItr=xmlAttrs.iterator();
while(xmlattrItr.hasNext())
{
XmlAttributes xa=xmlattrItr.next();
String attrname=xa.getName();
String isTransient=xa.isTransientAttr();
if(isTransient.equals("false"))
{
constraints.anchor = GridBagConstraints.NORTH;
constraints.insets = new Insets(10, 10, 10, 10);
JLabel name = new JLabel(attrname);
//name.setBounds( 10, 15, 150, 20 );
JTextField value = new JTextField(20);
//value.setBounds( 10, 15, 150, 20 );
constraints.gridx = 0;
constraints.gridy = ++j;
if(primaryKeys.contains(name.getText()))
{
name.setForeground(Color.RED);
}
else
{
name.setForeground(Color.BLUE);
}
nonTransientPanel.add(name,constraints);
nonTransientPanel.add(value);
uiParams.put(name, value);
nonTransientPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Non Transient Attributes"));
nonTransientPanel.setLayout(fl);
panel1.add(nonTransientPanel);
}
else
{
constraints.anchor = GridBagConstraints.NORTH;
constraints.insets = new Insets(10, 10, 10, 10);
JLabel name = new JLabel(attrname);
//name.setBounds( 10, 15, 150, 20 );
JTextField value = new JTextField(20);
//value.setBounds( 10, 35, 150, 20 );
constraints.gridx = 0;
constraints.gridy = ++j;
if(primaryKeys.contains(name.getText()))
{
name.setForeground(Color.RED);
}
else
{
name.setForeground(Color.BLUE);
}
transientPanel.add(name,constraints);
transientPanel.add(value);
uiParams.put(name, value);
transientPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Transient Attributes"));
transientPanel.setLayout(fl);
panel1.add(transientPanel);
}
}
constraints.gridx = 0;
constraints.gridy = ++j;
constraints.gridwidth = 2;
constraints.anchor = GridBagConstraints.SOUTH;
panel1.setSize(400, 400);
panel1.setLayout(fl);
tabbedPane.addTab(classname, panel1);
}
}
buttons.add(buttonLearn, constraints);
panel1.add(buttons);
}
setTitle( "Rote Learner Form" );
setSize(500, 500);
JPanel topPanel = new JPanel();
topPanel.setLayout(new BorderLayout());
getContentPane().add( topPanel );
// Create the tab pages
topPanel.add( tabbedPane, BorderLayout.CENTER );
}
this is how I am using JTabbedPane to display two forms.
How do I indent the JLabels so as to make it look like a standard form?
Could anyone answer this? urgently needed.. thanks in advance..
I have used JTabbedPane and an outer Jpanel inside which there are two JPanels TransietnPanel and nonGTransientPanel. This outer panel is added to JTabedPane.
This is the output I am getting
This is what I want to make it look like.
--------------code after adding GroupLayout--------------------
while(mainclass_it.hasNext())
{
Entry<String, String> classDet = mainclass_it.next();
String classname = classDet.getValue();
final Hashtable<JLabel,JTextField> uiParams=new Hashtable<JLabel,JTextField>();
JPanel buttons=new JPanel();
JButton buttonLearn = new JButton("Learn");
panel1= new JPanel();
//GroupLayout layout = new GroupLayout(panel1);
//panel1.setLayout(layout);
transientPanel=new JPanel(new GridBagLayout());
nonTransientPanel=new JPanel(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
Iterator<XmlClass> guiItr=guiAttrVals.iterator();
while(guiItr.hasNext())
{
XmlClass xmlclasss=guiItr.next();
if(classname.equals(xmlclasss.getName()))
{
GroupLayout layout1 = new GroupLayout(nonTransientPanel);
layout1 = new GroupLayout(transientPanel);
Set<XmlAttributes> xmlAttrs=xmlclasss.getAttributes();
Iterator<XmlAttributes> xmlattrItr=xmlAttrs.iterator();
while(xmlattrItr.hasNext())
{
XmlAttributes xa=xmlattrItr.next();
String attrname=xa.getName();
String isTransient=xa.isTransientAttr();
if(isTransient.equals("false"))
{
constraints.anchor = GridBagConstraints.NORTH;
constraints.insets = new Insets(10, 10, 10, 10);
JLabel name = new JLabel(attrname);
//name.setBounds( 10, 15, 150, 20 );
JTextField value = new JTextField(20);
//value.setBounds( 10, 15, 150, 20 );
constraints.gridx = 0;
constraints.gridy = ++j;
if(primaryKeys.contains(name.getText()))
{
name.setForeground(Color.RED);
}
else
{
name.setForeground(Color.BLUE);
}
nonTransientPanel.setLayout(layout1);
nonTransientPanel.add(name,constraints);
nonTransientPanel.add(value);
uiParams.put(name, value);
nonTransientPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Non Transient Attributes"));
nonTransientPanel.setLayout(fl);
panel1.add(nonTransientPanel);
}
else
{
constraints.anchor = GridBagConstraints.NORTH;
constraints.insets = new Insets(10, 10, 10, 10);
JLabel name = new JLabel(attrname);
//name.setBounds( 10, 15, 150, 20 );
JTextField value = new JTextField(20);
//value.setBounds( 10, 35, 150, 20 );
constraints.gridx = 0;
constraints.gridy = ++j;
if(primaryKeys.contains(name.getText()))
{
name.setForeground(Color.RED);
}
else
{
name.setForeground(Color.BLUE);
}
transientPanel.setLayout(layout1);
transientPanel.add(name,constraints);
transientPanel.add(value);
uiParams.put(name, value);
transientPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Transient Attributes"));
transientPanel.setLayout(fl);
panel1.add(transientPanel);
}
}
constraints.gridx = 0;
constraints.gridy = ++j;
constraints.gridwidth = 2;
constraints.anchor = GridBagConstraints.SOUTH;
panel1.setSize(400, 400);
panel1.setLayout(fl);
tabbedPane.addTab(classname, panel1);
}
}
buttons.add(buttonLearn, constraints);
panel1.add(buttons);
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I'm completely lost, being new to Java I can't seem to find what I need to do.
Please, do not refer me to another person's question, those don't help because I don't know how it will fix my problem.
Here is my code:
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.text.StyledDocument;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Locale;
import javax.swing.text.Element;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import json.JsonObject;
public class dicebot extends JFrame implements ActionListener {
static final long serialVersionUID = 1L;
private JPanel contentPane;
public static String APIKey = null;
public static JComboBox<String>cmbCurrency;
public static JButton btnLow;
public static JButton btnFloat;
public static JButton btnHigh;
public static JButton btnClearLog;
public static JButton btnDonate;
public static JTextPane textPane;
public static JCheckBox scrollCheck;
public static JCheckBox scrollDisable;
public static JTextField txtRollAmnt;
public static JTextField txtUserName;
public static JTextField txtStartBid;
public static JTextField txtMultiplier;
public static JTextField txtMinRemaining;
public static JPasswordField txtPassword;
public static JTextField txtOdds;
public static JTextField txtMaxBet;
public static JTextArea txtInfo;
public static JCheckBox RollAmntCheck;
public static JLabel lblBalTag;
public static JLabel userTag;
public static JLabel passTag;
public static void main(String[] args) {
Locale.setDefault(Locale.US);
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
dicebot frame = new dicebot();
frame.setVisible(true);
Dicebotcode d = new Dicebotcode();
d.LoadSettings();
d = null;
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public dicebot() {
setTitle("Dice Bot");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.WEST);
GridBagLayout gbl_panel = new GridBagLayout();
gbl_panel.columnWidths = new int[]{0, 0};
gbl_panel.rowHeights = new int[]{0, 0};
gbl_panel.columnWeights = new double[]{0.0, 1.0};
gbl_panel.rowWeights = new double[]{0.0, Double.MIN_VALUE};
panel.setLayout(gbl_panel);
//Every new Label however needs every part that says "user" or on the Password: "pass" changed to something unique.
userTag = new JLabel("Username:");
GridBagConstraints gbc_userTag = new GridBagConstraints();
gbc_userTag.insets = new Insets(0, 0, 0, 5);
gbc_userTag.anchor = GridBagConstraints.EAST;
gbc_userTag.gridx = 0;//Here are your x + y coords
gbc_userTag.gridy = 1;//Adding to x moves left, adding to y moves down
panel.add(userTag, gbc_userTag);
//Every new textfield needs only the * part to change for it to be valid. (gbc_* =)
//textField = new JTextField();
txtUserName = new JTextField();
GridBagConstraints grdUserName = new GridBagConstraints();
grdUserName.fill = GridBagConstraints.HORIZONTAL;
grdUserName.gridx = 1;
grdUserName.gridy = 1;
txtUserName.setColumns(10);
panel.add(txtUserName, grdUserName);
//panel.add(textField,txtUserName);
//textField.setColumns(10);
JLabel balTag = new JLabel("Current Balance:");
GridBagConstraints gbc_balTag = new GridBagConstraints();
gbc_balTag.insets = new Insets(0, 0, 0, 5);
gbc_balTag.anchor = GridBagConstraints.EAST;
gbc_balTag.gridx = 0;
gbc_balTag.gridy = 0;
panel.add(balTag, gbc_balTag);
lblBalTag = new JLabel("[________________]");
lblBalTag.setToolTipText("Balance as of the last call to the peerbet site.");
GridBagConstraints gbc_lblBalTag = new GridBagConstraints();
gbc_lblBalTag.insets = new Insets(0, 0, 0, 5);
gbc_lblBalTag.anchor = GridBagConstraints.EAST;
gbc_lblBalTag.gridx = 1;
gbc_lblBalTag.gridy = 0;
panel.add(lblBalTag, gbc_lblBalTag);
JLabel startTag = new JLabel("Starting Bid:");
GridBagConstraints gbc_startTag = new GridBagConstraints();
gbc_startTag.insets = new Insets(0, 0, 0, 5);
gbc_startTag.anchor = GridBagConstraints.EAST;
gbc_startTag.gridx = 0;
gbc_startTag.gridy = 3;
panel.add(startTag, gbc_startTag);
txtStartBid = new JTextField();
GridBagConstraints grdStartBid = new GridBagConstraints();
grdStartBid.fill = GridBagConstraints.HORIZONTAL;
grdStartBid.gridx = 1;
grdStartBid.gridy = 3;
txtStartBid.setText("0.00000010");
txtStartBid.setEnabled(false);
panel.add(txtStartBid, grdStartBid);
JLabel multTag = new JLabel("Multiplier:");
GridBagConstraints gbc_multTag = new GridBagConstraints();
gbc_multTag.insets = new Insets(0, 0, 0, 5);
gbc_multTag.anchor = GridBagConstraints.EAST;
gbc_multTag.gridx = 0;
gbc_multTag.gridy = 4;
panel.add(multTag, gbc_multTag);
txtMultiplier = new JTextField();
GridBagConstraints grdMultiplier = new GridBagConstraints();
grdMultiplier.fill = GridBagConstraints.HORIZONTAL;
grdMultiplier.gridx = 1;
grdMultiplier.gridy = 4;
txtMultiplier.setColumns(10);
txtMultiplier.setText("2");
txtMultiplier.setEnabled(false);
panel.add(txtMultiplier, grdMultiplier);
JLabel minTag = new JLabel("Min Remaining:");
GridBagConstraints gbc_minTag = new GridBagConstraints();
gbc_minTag.insets = new Insets(0, 0, 0, 5);
gbc_minTag.anchor = GridBagConstraints.EAST;
gbc_minTag.gridx = 0;
gbc_minTag.gridy = 5;
panel.add(minTag, gbc_minTag);
txtMinRemaining = new JTextField();
GridBagConstraints grdMinRemaining = new GridBagConstraints();
grdMinRemaining.fill = GridBagConstraints.HORIZONTAL;
grdMinRemaining.gridx = 1;
grdMinRemaining.gridy = 5;
txtMinRemaining.setColumns(10);
txtMinRemaining.setText("0");
txtMinRemaining.setEnabled(false);
panel.add(txtMinRemaining, grdMinRemaining);
txtPassword = new JPasswordField();
GridBagConstraints grdPassword = new GridBagConstraints();
grdPassword.fill = GridBagConstraints.HORIZONTAL;
grdPassword.gridx = 1;
grdPassword.gridy = 2;
txtPassword.setEchoChar('*');
txtPassword.setColumns(10);
panel.add(txtPassword, grdPassword);
passTag = new JLabel("Password:");
GridBagConstraints gbc_passTag = new GridBagConstraints();
gbc_passTag.insets = new Insets(0, 0, 0, 5);
gbc_passTag.anchor = GridBagConstraints.EAST;
gbc_passTag.gridx = 0;
gbc_passTag.gridy = 2;
panel.add(passTag, gbc_passTag);
txtOdds = new JTextField();
GridBagConstraints grdOdds = new GridBagConstraints();
grdOdds.fill = GridBagConstraints.HORIZONTAL;
grdOdds.gridx = 1;
grdOdds.gridy = 6;
txtOdds.setColumns(10);
txtOdds.addActionListener(this);
txtOdds.setText("49.5");
txtOdds.setEnabled(false);
panel.add(txtOdds, grdOdds);
JLabel oddsTag = new JLabel("Odds %:");
GridBagConstraints gbc_oddsTag = new GridBagConstraints();
gbc_oddsTag.insets = new Insets(0, 0, 0, 5);
gbc_oddsTag.anchor = GridBagConstraints.EAST;
gbc_oddsTag.gridx = 0;
gbc_oddsTag.gridy = 6;
panel.add(oddsTag, gbc_oddsTag);
txtMaxBet = new JTextField();
GridBagConstraints grdMaxBet = new GridBagConstraints();
grdMaxBet.fill = GridBagConstraints.HORIZONTAL;
grdMaxBet.gridx = 1;
grdMaxBet.gridy = 7;
txtMaxBet.setColumns(10);
txtMaxBet.setText("1");
txtMaxBet.setEnabled(false);
panel.add(txtMaxBet, grdMaxBet);
txtRollAmnt = new JTextField();
GridBagConstraints grdRollAmnt = new GridBagConstraints();
grdRollAmnt.fill = GridBagConstraints.HORIZONTAL;
grdRollAmnt.gridx = 1;
grdRollAmnt.gridy = 8;
txtRollAmnt.setColumns(10);
txtRollAmnt.setText("0=Infinite");
txtRollAmnt.setEnabled(false);
panel.add(txtRollAmnt, grdRollAmnt);
RollAmntCheck = new JCheckBox("Roll Then Quit:");
RollAmntCheck.setSelected(true);
GridBagConstraints grdRollAmntCheck = new GridBagConstraints();
grdRollAmntCheck.fill = GridBagConstraints.HORIZONTAL;
grdRollAmntCheck.gridx = 0;
grdRollAmntCheck.gridy = 8;
panel.add(RollAmntCheck, grdRollAmntCheck);
//This is the Combo Box
cmbCurrency = new JComboBox<String>(new String[]{"BTC","LTC","PPC","NMC","XPM","FTC","ANC","DOGE","NXT"});
GridBagConstraints gbc_list = new GridBagConstraints();
gbc_list.fill = GridBagConstraints.HORIZONTAL;
gbc_list.gridx = 1;
gbc_list.gridy = 9;
cmbCurrency.addActionListener(this);
cmbCurrency.setEnabled(false);
panel.add(cmbCurrency, gbc_list);
JLabel maxTag = new JLabel("MaxBet:");
GridBagConstraints gbc_maxTag = new GridBagConstraints();
gbc_maxTag.insets = new Insets(0, 0, 0, 5);
gbc_maxTag.anchor = GridBagConstraints.EAST;
gbc_maxTag.gridx = 0;
gbc_maxTag.gridy = 7;
panel.add(maxTag, gbc_maxTag);
JPanel panel_1 = new JPanel();
contentPane.add(panel_1, BorderLayout.SOUTH);
panel_1.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
btnDonate = new JButton("Login");
btnDonate.addActionListener(this);
panel_1.add(btnDonate);
btnHigh = new JButton("Roll High");
btnHigh.addActionListener(this);
btnHigh.setEnabled(false);
panel_1.add(btnHigh);
btnLow = new JButton("Roll Low");
btnLow.addActionListener(this);
btnLow.setEnabled(false);
panel_1.add(btnLow);
btnFloat = new JButton("Roll Float");
btnFloat.addActionListener(this);
btnFloat.setEnabled(false);
btnFloat.setVisible(false);
panel_1.add(btnFloat);
btnClearLog = new JButton("Clear Log");
btnClearLog.addActionListener(this);
panel_1.add(btnClearLog);
scrollCheck = new JCheckBox("Auto-Scroll");
scrollCheck.setSelected(true);
panel_1.add(scrollCheck);
scrollDisable = new JCheckBox("Disable Log");
scrollDisable.setSelected(false);
panel_1.add(scrollDisable);
btnClearLog.setToolTipText("Click here to clear the log!");
btnHigh.setToolTipText("Click here to Roll High!");
btnLow.setToolTipText("Click here to Roll Low!");
btnFloat.setToolTipText("Click here to Roll?");
scrollCheck.setToolTipText("Toggles the auto-scroll function of the log.");
RollAmntCheck.setToolTipText("Roll Amount then Quit");
txtMaxBet.setToolTipText("The dicebot will not bet above amount entered in.");
txtOdds.setToolTipText("What odds(%) will the dicebot be rolling?");
txtPassword.setToolTipText("Enter your peerbet account password.");
txtMinRemaining.setToolTipText("The bot will stop when account has less than this amount in bank.");
txtMultiplier.setToolTipText("What shall the bet be multiplied by upon loss?");
txtStartBid.setToolTipText("What amount should the bot start each bet at?");
txtUserName.setToolTipText("Enter your peerbet account username.");
lblBalTag.setToolTipText("Current amount of chosen currency shown here.");
cmbCurrency.setToolTipText("Choose the currency that the bot will be using to roll with.");
contentPane.add(textPane, BorderLayout.CENTER);
txtInfo = new JTextArea("All number formats must use a period(.)\nBot By: MichaelAdair and DalinSprocket\n");
txtInfo.setColumns(35);
txtInfo.setEnabled(false);
textPane = new JTextPane();
textPane.setBackground(Color.DARK_GRAY);
textPane.setEditable(false);
textPane.setMargin(null);
textPane.setContentType("text/html");
StyledDocument doc = textPane.getStyledDocument();
Style style = textPane.addStyle("Loss",null);
StyleConstants.setForeground(style, Color.red);
Style style2 = textPane.addStyle("Win",null);
StyleConstants.setForeground(style, Color.green);
pack();
}
#Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == cmbCurrency) {
if (cmbCurrency.getSelectedIndex() == 0){
txtStartBid.setText("0.00000010");
}else{
txtStartBid.setText("0.0001");
}
if(APIKey != null){
String balance = peerbetapi.get_balance(dicebot.APIKey);
JsonObject jsonObject = JsonObject.readFrom(balance);
if(jsonObject.get("status").asInt() == 1){
lblBalTag.setText(jsonObject.get("raffle_cur" + Integer.toString((cmbCurrency.getSelectedIndex() + 10))).asString());
}
}else{
lblBalTag.setText("[________________]");
}
}else if (e.getSource() == btnLow){
if(btnLow.getText() == "Roll Low"){
btnHigh.setText("Stop");
btnLow.setText("Stop On Win");
btnFloat.setEnabled(false);
Dicebotcode dbc = new Dicebotcode();
Dicebotcode.RollType = "low";
Dicebotcode.StopRollingOnWin = false;
Dicebotcode.StopRolling = false;
dbc.dbc();
}else{
// The EnableAllFields function will re-enable the buttons once its done.
btnLow.setText("Waiting...");
btnLow.setEnabled(false);
Dicebotcode.StopRollingOnWin = true;
}
}else if (e.getSource() == btnHigh){
if(btnHigh.getText() == "Roll High"){
btnHigh.setText("Stop");
btnLow.setText("Stop On Win");
btnFloat.setEnabled(false);
Dicebotcode dbc = new Dicebotcode();
Dicebotcode.RollType = "high";
Dicebotcode.StopRollingOnWin = false;
Dicebotcode.StopRolling = false;
dbc.dbc();
}else{
// The EnableAllFields function will re-enable the buttons once its done.
btnHigh.setText("Stopping...");
btnHigh.setEnabled(false);
btnLow.setEnabled(false);
Dicebotcode.StopRolling = true;
}
}else if (e.getSource() == btnFloat){
if(btnFloat.getText() == "Roll Float"){
btnHigh.setText("Stop");
btnLow.setText("Stop On Win");
btnFloat.setEnabled(false);
Dicebotcode dbc = new Dicebotcode();
Dicebotcode.RollType = "float";
Dicebotcode.StopRollingOnWin = false;
Dicebotcode.StopRolling = false;
dbc.dbc();
}else{
// The EnableAllFields function will re-enable the buttons once its done.
btnFloat.setText("Stopping...");
btnFloat.setEnabled(false);
Dicebotcode.StopRolling = true;
}
}else if (e.getSource() == btnClearLog){
txtInfo.setText("");
}else if (e.getSource() == btnDonate){
//donate d = new donate();
if(btnDonate.getText() == "Login"){
String reply = null;
try {
reply = peerbetapi.login(txtUserName.getText(), String.copyValueOf(txtPassword.getPassword()));
} catch (IOException e1) {
reply = "{\"status\":0, \"message\":\"An unknown error has occurred while attempting to login.\"}";
}
JsonObject json = JsonObject.readFrom(reply);
if(json.get("status").asInt() != 1){
txtInfo.append("Error: " + json.get("message").asString() + "\n");
txtInfo.setCaretPosition(txtInfo.getText().length());
}else{
APIKey = json.get("key").asString();
lblBalTag.setText(json.get("raffle_cur" + Integer.toString(cmbCurrency.getSelectedIndex() + 10)).asString());
btnDonate.setText("Donate");
userTag.setVisible(false);
txtUserName.setVisible(false);
passTag.setVisible(false);
txtPassword.setVisible(false);
txtStartBid.setEnabled(true);
txtMultiplier.setEnabled(true);
txtMinRemaining.setEnabled(true);
txtOdds.setEnabled(true);
txtMaxBet.setEnabled(true);
cmbCurrency.setEnabled(true);
btnHigh.setEnabled(true);
btnLow.setEnabled(true);
btnFloat.setEnabled(true);
txtInfo.append("Login successful!\n");
txtInfo.setCaretPosition(txtInfo.getText().length());
}
}else{
donate.showdonate();
}
}
}
}
Here is my error:
michaeladair#michaeladair:~/Desktop/dicebot/src$ java dicebotjava.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:1086)
at java.awt.Container.add(Container.java:966)
at dicebot.<init>(dicebot.java:298)
at dicebot$1.run(dicebot.java:44)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
The problem is here:
contentPane.add(textPane, BorderLayout.CENTER);
At this point, you haven't set up textPane. You set it up 4 lines later:
textPane = new JTextPane();
textPane.setBackground(Color.DARK_GRAY);
textPane.setEditable(false);
textPane.setMargin(null);
textPane.setContentType("text/html");
You need to add it to the pane after initializing.
Simple debugging would have fixed this problem for you. SO is not an appropriate place for "fix my code" questions.
Here is what's causing the error. You are adding textPane, before you create it. That's why a null pointer exception is thrown:
contentPane.add(textPane, BorderLayout.CENTER);
txtInfo = new JTextArea("All number formats must use a period(.)\nBot By: MichaelAdair and DalinSprocket\n");
txtInfo.setColumns(35);
txtInfo.setEnabled(false);
textPane = new JTextPane();
Create the textPane object first, then add it.
I have 10 combo boxes and one button. Is there an easy way to make it so you set all the values of each combo box and then press this button and it stores all the values of the combo boxes?
You first have to iterate through all your JComboBox elements and get their selected state (ie: getSelectedItem() or getSelectedIndex()). Once you have determined what selection has been made, you can save the selections in a number of places to be read later, dependent on your needs:
You can save the states into a file that can be read at a later date
You can save the information in the local registry via Java Preferences API
If it is a network application, you can save the information in an SQL database
Sure! I'd propose to write yourself a new class which has both things:
class comboTrack{
private JComboBox box;
private Object value;
public comboTrack(JComboBox box){
this.box = box;
this.value = box.getSelectedItem();
}
public void update(){
this.value = box.getSelectedItem();
}
public Object getValue(){
return value;
}
}
Then, use these instead of normal Boxes. Like this:
//List of boxes (class variable):
ArrayList<comboTrack> boxes;
//Inside the button click method:
for(comboTrack box : boxes){
box.update();
}
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;
public class ComboBoxValues extends JFrame {
private JPanel contentPane;
JComboBox comboBox;
JComboBox comboBox_1;
JComboBox comboBox_2;
JComboBox comboBox_3;
JComboBox comboBox_4;
JComboBox comboBox_5;
JComboBox comboBox_6;
JComboBox comboBox_7;
JComboBox comboBox_8;
JComboBox comboBox_9;
private JTextArea textArea;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ComboBoxValues frame = new ComboBoxValues();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public ComboBoxValues() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
GridBagLayout gbl_contentPane = new GridBagLayout();
gbl_contentPane.columnWidths = new int[]{0, 0, 0};
gbl_contentPane.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
gbl_contentPane.columnWeights = new double[]{1.0, 0.0, Double.MIN_VALUE};
gbl_contentPane.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, Double.MIN_VALUE};
contentPane.setLayout(gbl_contentPane);
comboBox = new JComboBox();
GridBagConstraints gbc_comboBox = new GridBagConstraints();
gbc_comboBox.insets = new Insets(0, 0, 5, 5);
gbc_comboBox.fill = GridBagConstraints.HORIZONTAL;
gbc_comboBox.gridx = 0;
gbc_comboBox.gridy = 0;
contentPane.add(comboBox, gbc_comboBox);
comboBox.addItem("comboBox_0 val 1");
comboBox.addItem("comboBox_0 val 2");
comboBox_1 = new JComboBox();
GridBagConstraints gbc_comboBox_1 = new GridBagConstraints();
gbc_comboBox_1.insets = new Insets(0, 0, 5, 0);
gbc_comboBox_1.fill = GridBagConstraints.HORIZONTAL;
gbc_comboBox_1.gridx = 1;
gbc_comboBox_1.gridy = 0;
contentPane.add(comboBox_1, gbc_comboBox_1);
comboBox_1.addItem("comboBox_1 val 1");
comboBox_1.addItem("comboBox_1 val 2");
comboBox_2 = new JComboBox();
GridBagConstraints gbc_comboBox_2 = new GridBagConstraints();
gbc_comboBox_2.insets = new Insets(0, 0, 5, 5);
gbc_comboBox_2.fill = GridBagConstraints.HORIZONTAL;
gbc_comboBox_2.gridx = 0;
gbc_comboBox_2.gridy = 1;
contentPane.add(comboBox_2, gbc_comboBox_2);
comboBox_2.addItem("comboBox_2 val 1");
comboBox_2.addItem("comboBox_2 val 2");
comboBox_3 = new JComboBox();
GridBagConstraints gbc_comboBox_3 = new GridBagConstraints();
gbc_comboBox_3.insets = new Insets(0, 0, 5, 0);
gbc_comboBox_3.fill = GridBagConstraints.HORIZONTAL;
gbc_comboBox_3.gridx = 1;
gbc_comboBox_3.gridy = 1;
contentPane.add(comboBox_3, gbc_comboBox_3);
comboBox_3.addItem("comboBox_3 val 1");
comboBox_3.addItem("comboBox_3 val 2");
comboBox_4 = new JComboBox();
GridBagConstraints gbc_comboBox_4 = new GridBagConstraints();
gbc_comboBox_4.insets = new Insets(0, 0, 5, 5);
gbc_comboBox_4.fill = GridBagConstraints.HORIZONTAL;
gbc_comboBox_4.gridx = 0;
gbc_comboBox_4.gridy = 2;
contentPane.add(comboBox_4, gbc_comboBox_4);
comboBox_4.addItem("comboBox_4 val 1");
comboBox_4.addItem("comboBox_4 val 2");
comboBox_5 = new JComboBox();
GridBagConstraints gbc_comboBox_5 = new GridBagConstraints();
gbc_comboBox_5.insets = new Insets(0, 0, 5, 0);
gbc_comboBox_5.fill = GridBagConstraints.HORIZONTAL;
gbc_comboBox_5.gridx = 1;
gbc_comboBox_5.gridy = 2;
contentPane.add(comboBox_5, gbc_comboBox_5);
comboBox_5.addItem("comboBox_5 val 1");
comboBox_5.addItem("comboBox_5 val 2");
comboBox_6 = new JComboBox();
GridBagConstraints gbc_comboBox_6 = new GridBagConstraints();
gbc_comboBox_6.insets = new Insets(0, 0, 5, 5);
gbc_comboBox_6.fill = GridBagConstraints.HORIZONTAL;
gbc_comboBox_6.gridx = 0;
gbc_comboBox_6.gridy = 3;
contentPane.add(comboBox_6, gbc_comboBox_6);
comboBox_6.addItem("comboBox_6 val 1");
comboBox_6.addItem("comboBox_6 val 2");
comboBox_7 = new JComboBox();
GridBagConstraints gbc_comboBox_7 = new GridBagConstraints();
gbc_comboBox_7.insets = new Insets(0, 0, 5, 0);
gbc_comboBox_7.fill = GridBagConstraints.HORIZONTAL;
gbc_comboBox_7.gridx = 1;
gbc_comboBox_7.gridy = 3;
contentPane.add(comboBox_7, gbc_comboBox_7);
comboBox_7.addItem("comboBox_7 val 1");
comboBox_7.addItem("comboBox_7 val 2");
comboBox_8 = new JComboBox();
GridBagConstraints gbc_comboBox_8 = new GridBagConstraints();
gbc_comboBox_8.insets = new Insets(0, 0, 5, 5);
gbc_comboBox_8.fill = GridBagConstraints.HORIZONTAL;
gbc_comboBox_8.gridx = 0;
gbc_comboBox_8.gridy = 4;
contentPane.add(comboBox_8, gbc_comboBox_8);
comboBox_8.addItem("comboBox_8 val 1");
comboBox_8.addItem("comboBox_8 val 2");
comboBox_9 = new JComboBox();
GridBagConstraints gbc_comboBox_9 = new GridBagConstraints();
gbc_comboBox_9.insets = new Insets(0, 0, 5, 0);
gbc_comboBox_9.fill = GridBagConstraints.HORIZONTAL;
gbc_comboBox_9.gridx = 1;
gbc_comboBox_9.gridy = 4;
contentPane.add(comboBox_9, gbc_comboBox_9);
comboBox_9.addItem("comboBox_9 val 1");
comboBox_9.addItem("comboBox_9 val 2");
JButton btnGetValues = new JButton("Get Values");
GridBagConstraints gbc_btnGetValues = new GridBagConstraints();
gbc_btnGetValues.insets = new Insets(0, 0, 5, 0);
gbc_btnGetValues.gridx = 1;
gbc_btnGetValues.gridy = 5;
contentPane.add(btnGetValues, gbc_btnGetValues);
textArea = new JTextArea();
GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
gbc_lblNewLabel.gridheight = 2;
gbc_lblNewLabel.gridwidth = 2;
gbc_lblNewLabel.insets = new Insets(0, 0, 5, 0);
gbc_lblNewLabel.gridx = 0;
gbc_lblNewLabel.gridy = 6;
contentPane.add(textArea, gbc_lblNewLabel);
textArea.setColumns(30);
textArea.setRows(5);
pack();
btnGetValues.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textArea.setText(comboBox.getSelectedItem().toString()+","+
comboBox_1.getSelectedItem().toString()+","+
comboBox_2.getSelectedItem().toString()+"\n"+
comboBox_3.getSelectedItem().toString()+","+
comboBox_4.getSelectedItem().toString()+","+
comboBox_5.getSelectedItem().toString()+"\n"+
comboBox_6.getSelectedItem().toString()+","+
comboBox_7.getSelectedItem().toString()+","+
comboBox_8.getSelectedItem().toString()+"\n"+
comboBox_9.getSelectedItem().toString());
}
});
}
}
I am new to java swing, recently I try to create a swing app to format text.
When I click the maximum button, the text panel's length does not resize, and the middle panel takes large space.
And seems setResizable(false) does not work
Code
public class MainFrame extends JFrame {
private static final long serialVersionUID = 7553142908344084288L;
private JTextArea fromTextArea;
private JTextArea toTextArea;
public MainFrame() {
super("jFormatter");
Panel mainPanel = new Panel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.X_AXIS));
setContentPane(mainPanel);
fromTextArea = createTextArea();
lines.setBorder(BorderFactory.createMatteBorder(0, 1, 0, 1, Color.LIGHT_GRAY));
lines.setEditable(false);
Font f = new Font(Font.SANS_SERIF, Font.PLAIN, 16);
lines.setFont(f);
JScrollPane fromTextAreaScrollPanel = new JScrollPane(fromTextArea);
fromTextAreaScrollPanel.setBorder(BorderFactory.createEmptyBorder(15, 5, 15, 5));
fromTextAreaScrollPanel.getViewport().add(fromTextArea);
fromTextAreaScrollPanel.setRowHeaderView(lines);
mainPanel.add(fromTextAreaScrollPanel);
// control panel
mainPanel.add(createCtrlPanel());
toTextArea = createTextArea();
mainPanel.add(createTextAreaPanel(toTextArea));
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setResizable(false);
setVisible(true);
setLocationRelativeTo(null);
}
private JPanel createCtrlPanel() {
final JComboBox jComboBox = new JComboBox(formatters.keySet().toArray());
jComboBox.setBorder(BorderFactory.createTitledBorder("Text Format"));
JButton fmtButton = new JButton("Format >>");
JPanel ctrPanel = new JPanel(new GridBagLayout());
ctrPanel.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5));
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.fill = GridBagConstraints.HORIZONTAL;
ctrPanel.add(jComboBox, gbc);
ctrPanel.add(Box.createRigidArea(new Dimension(50, 15)), gbc);
//gbc.fill = GridBagConstraints.NONE;
ctrPanel.add(fmtButton, gbc);
return ctrPanel;
}
private JScrollPane createTextAreaPanel(JTextArea textArea) {
JScrollPane fromTextAreaScrollPanel = new JScrollPane(textArea);
//fromTextAreaScrollPanel.setPreferredSize(new Dimension(300, 300));
fromTextAreaScrollPanel.setBorder(BorderFactory.createEmptyBorder(15, 5, 15, 5));
return fromTextAreaScrollPanel;
}
private JTextArea createTextArea() {
JTextArea textArea = new JTextArea(20, 40);
Font f = new Font(Font.SANS_SERIF, Font.PLAIN, 16);
textArea.setFont(f);
//fromTextArea.setLineWrap(true);
//textArea.setBackground(Color.LIGHT_GRAY);
textArea.setMargin(new Insets(0, 10, 0, 10));
return textArea;
}
public static void main(String[] args) {
new MainFrame();
}
}
result:
You should probably use BorderLayout or GridBagLayout for this. BoxLayout just lays out components one after the other at their preferred size. It doesn't make any attempt to resize the components or make them fill their parent.
Try to make a layout like this:
Code:
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;
public class Test extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Test frame = new Test();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Test() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
GridBagLayout gbl_contentPane = new GridBagLayout();
gbl_contentPane.columnWidths = new int[]{0, 0, 0, 0};
gbl_contentPane.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
gbl_contentPane.columnWeights = new double[]{0.0, 0.0, 1.0, Double.MIN_VALUE};
gbl_contentPane.rowWeights = new double[]{1.0, 0.0, Double.MIN_VALUE};
contentPane.setLayout(gbl_contentPane);
JScrollPane scrollPane = new JScrollPane();
GridBagConstraints gbc_scrollPane = new GridBagConstraints();
gbc_scrollPane.gridheight = 2;
gbc_scrollPane.insets = new Insets(0, 0, 0, 5);
gbc_scrollPane.fill = GridBagConstraints.BOTH;
gbc_scrollPane.gridx = 0;
gbc_scrollPane.gridy = 0;
gbc_scrollPane.weightx=1;
contentPane.add(scrollPane, gbc_scrollPane);
JTextArea textArea = new JTextArea();
scrollPane.setViewportView(textArea);
JPanel panel = new JPanel();
GridBagConstraints gbc_panel = new GridBagConstraints();
gbc_panel.gridheight = 2;
gbc_panel.insets = new Insets(0, 0, 5, 5);
//gbc_panel.fill = GridBagConstraints.BOTH;
gbc_panel.gridx = 1;
gbc_panel.gridy = 0;
contentPane.add(panel, gbc_panel);
GridBagLayout gbl_panel = new GridBagLayout();
gbl_panel.columnWidths = new int[]{0, 0, 0, 0};
gbl_panel.rowHeights = new int[]{0, 0, 0, 0, 0, 0};
gbl_panel.columnWeights = new double[]{0.0, 0.0, 1.0, Double.MIN_VALUE};
gbl_panel.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
panel.setLayout(gbl_panel);
JComboBox comboBox = new JComboBox();
GridBagConstraints gbc_comboBox = new GridBagConstraints();
gbc_comboBox.insets = new Insets(0, 0, 5, 0);
gbc_comboBox.fill = GridBagConstraints.HORIZONTAL;
gbc_comboBox.gridx = 2;
gbc_comboBox.gridy = 0;
gbc_comboBox.weightx=0.0;
panel.add(comboBox, gbc_comboBox);
JButton btnNewButton = new JButton(">>");
GridBagConstraints gbc_btnNewButton = new GridBagConstraints();
gbc_btnNewButton.insets = new Insets(0, 0, 5, 0);
gbc_btnNewButton.gridx = 2;
gbc_btnNewButton.gridy = 1;
panel.add(btnNewButton, gbc_btnNewButton);
JScrollPane scrollPane_1 = new JScrollPane();
GridBagConstraints gbc_scrollPane_1 = new GridBagConstraints();
gbc_scrollPane_1.gridheight = 2;
gbc_scrollPane_1.fill = GridBagConstraints.BOTH;
gbc_scrollPane_1.gridx = 2;
gbc_scrollPane_1.gridy = 0;
gbc_scrollPane_1.weightx=1;
contentPane.add(scrollPane_1, gbc_scrollPane_1);
JTextArea textArea_1 = new JTextArea();
scrollPane_1.setViewportView(textArea_1);
pack();
}
}
Hi
I'm working on a JavaSwing application but there's a problem with... I don't know exactly but I think it's maybe a (re)paint-problem :S - anyway here's my code:
MAIN:
public class QickSort {
protected static ArrayList<String> input;
private static File file = new File("C:/Users/save.txt");
public static void main(String[] args) throws Exception {
BufferedReader reader;
String line = null;
try {
input = new ArrayList<String>();
reader = new BufferedReader(new FileReader(file));
while ((line = reader.readLine()) != null) {
input.add(line);
}
reader.close();
} catch (Exception ex) {
System.out.println("Can't load file on path.. - " + ex);
ex.printStackTrace();
}
System.out.println(input.size());
JFrame.setDefaultLookAndFeelDecorated(false);
MasterWin win = new MasterWin(input);
}
}
UI:
public class MasterWin {
private JFrame frame;
private JTextField txtFieldPath;
private JButton btnBrowse;
private JButton btnAddPathTo;
private JLabel lblChosenFolderpaths;
private JButton btnRemove;
private JButton btnNext;
protected static ImageIcon icon = new ImageIcon(MasterWin.class.getResource("/View/logo_sml.gif"));
private JScrollPane scrollPane;
private JList linkList;
private List<String> test;
/**
* Create the application.
*/
public MasterWin(ArrayList<String> fileInput) {
test = fileInput;
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setIconImage(icon.getImage());
frame.setTitle("QickSort - Start");
frame.setResizable(false);
frame.setBounds(100, 100, 645, 480);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[]{480, 127, 0};
gridBagLayout.rowHeights = new int[]{135, 60, 0, 0, 0, 115, 0};
gridBagLayout.columnWeights = new double[]{1.0, 0.0, Double.MIN_VALUE};
gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
frame.getContentPane().setLayout(gridBagLayout);
frame.setVisible(true);
JLabel logo = new JLabel("");
logo.setIcon(new ImageIcon(MasterWin.class.getResource("/View/Logo.gif")));
GridBagConstraints gbc_logo = new GridBagConstraints();
gbc_logo.fill = GridBagConstraints.HORIZONTAL;
gbc_logo.gridwidth = 2;
gbc_logo.insets = new Insets(0, 0, 5, 0);
gbc_logo.anchor = GridBagConstraints.SOUTH;
gbc_logo.gridx = 0;
gbc_logo.gridy = 0;
frame.getContentPane().add(logo, gbc_logo);
JLabel lblChosePathYou = new JLabel("Choose paths you want to use:");
GridBagConstraints gbc_lblChosePathYou = new GridBagConstraints();
gbc_lblChosePathYou.anchor = GridBagConstraints.SOUTHWEST;
gbc_lblChosePathYou.insets = new Insets(0, 60, 5, 5);
gbc_lblChosePathYou.gridx = 0;
gbc_lblChosePathYou.gridy = 1;
frame.getContentPane().add(lblChosePathYou, gbc_lblChosePathYou);
txtFieldPath = new JTextField();
txtFieldPath.setEditable(false);
GridBagConstraints gbc_txtFieldPath = new GridBagConstraints();
gbc_txtFieldPath.fill = GridBagConstraints.HORIZONTAL;
gbc_txtFieldPath.insets = new Insets(0, 60, 5, 5);
gbc_txtFieldPath.gridx = 0;
gbc_txtFieldPath.gridy = 2;
frame.getContentPane().add(txtFieldPath, gbc_txtFieldPath);
txtFieldPath.setColumns(10);
btnBrowse = new JButton("Browse...");
btnBrowse.setMinimumSize(new Dimension(89, 25));
btnBrowse.setMaximumSize(new Dimension(89, 25));
GridBagConstraints gbc_btnBrowse = new GridBagConstraints();
gbc_btnBrowse.anchor = GridBagConstraints.WEST;
gbc_btnBrowse.insets = new Insets(0, 10, 5, 0);
gbc_btnBrowse.gridx = 1;
gbc_btnBrowse.gridy = 2;
frame.getContentPane().add(btnBrowse, gbc_btnBrowse);
lblChosenFolderpaths = new JLabel("Chosen folderpaths:");
GridBagConstraints gbc_lblChosenFolderpaths = new GridBagConstraints();
gbc_lblChosenFolderpaths.anchor = GridBagConstraints.SOUTHWEST;
gbc_lblChosenFolderpaths.insets = new Insets(0, 60, 5, 5);
gbc_lblChosenFolderpaths.gridx = 0;
gbc_lblChosenFolderpaths.gridy = 3;
frame.getContentPane().add(lblChosenFolderpaths, gbc_lblChosenFolderpaths);
btnAddPathTo = new JButton("Add to list");
GridBagConstraints gbc_btnAddPathTo = new GridBagConstraints();
gbc_btnAddPathTo.anchor = GridBagConstraints.WEST;
gbc_btnAddPathTo.insets = new Insets(5, 10, 5, 0);
gbc_btnAddPathTo.gridx = 1;
gbc_btnAddPathTo.gridy = 3;
frame.getContentPane().add(btnAddPathTo, gbc_btnAddPathTo);
btnRemove = new JButton("Remove");
btnRemove.setToolTipText("Delete selected path");
btnRemove.setPreferredSize(new Dimension(89, 25));
btnRemove.setMinimumSize(new Dimension(89, 25));
btnRemove.setMaximumSize(new Dimension(89, 25));
GridBagConstraints gbc_btnRemove = new GridBagConstraints();
gbc_btnRemove.anchor = GridBagConstraints.NORTHWEST;
gbc_btnRemove.insets = new Insets(5, 10, 5, 0);
gbc_btnRemove.gridx = 1;
gbc_btnRemove.gridy = 4;
frame.getContentPane().add(btnRemove, gbc_btnRemove);
btnNext = new JButton("Accept");
btnNext.setPreferredSize(new Dimension(89, 25));
btnNext.setMinimumSize(new Dimension(89, 25));
btnNext.setMaximumSize(new Dimension(89, 25));
GridBagConstraints gbc_btnNext = new GridBagConstraints();
gbc_btnNext.anchor = GridBagConstraints.SOUTHWEST;
gbc_btnNext.insets = new Insets(0, 10, 25, 0);
gbc_btnNext.gridx = 1;
gbc_btnNext.gridy = 5;
frame.getContentPane().add(btnNext, gbc_btnNext);
scrollPane = new JScrollPane();
GridBagConstraints gbc_scrollPane = new GridBagConstraints();
gbc_scrollPane.gridheight = 2;
gbc_scrollPane.insets = new Insets(0, 60, 25, 5);
gbc_scrollPane.fill = GridBagConstraints.BOTH;
gbc_scrollPane.gridx = 0;
gbc_scrollPane.gridy = 4;
frame.getContentPane().add(scrollPane, gbc_scrollPane);
//JList I copy the array
linkList = new JList(test.toArray());
scrollPane.setViewportView(linkList);
}
}
The Problem:
It's really strange! Sometimes the text is shown on my JList - but if I start the program once again there's just a empty ScrollPane without the JList or the inputs.
Its more or less random that the text appears.
I tried a various kinds of Array(List)s, to impl. - with AbstractModel() oder just toArray(). Always same result..
Does someone know this problem?
Change:
protected static ArrayList<String> input;
To:
protected static final ArrayList<String> input = new ArrayList<String>();
Remove the assignment to input in main.
Wrap the calls to swing code in an invokeLater:
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame.setDefaultLookAndFeelDecorated(false);
MasterWin win = new MasterWin(input);
};
});
After setting your list as the viewport view you should call invalidate() on the JList which will tell the underlying Swing graphics system that it needs to check the component as well as any possibly affected components whether or not these require updating.
Do not call repaint() because that will trigger a repaint of the component as it is, it will not ensure that other affected components are properly updated as well -- and because this JList is wrapped in a JScrollPane it will not behave as expected.
user268396's answer about layout validation is correct, but you should never need to be in this situation in the first place. Your layout is invalidated because you're adding components after you've made your JFrame visible. Simply by moving the call to frame.setVisible(true) to the end of the initialize() method, you should see the problem goes away, because the layout is now finalized and it will validate and render correctly first time.
I solved this issue:
I kicked out my Scrollpane and used only a JList. But that didn't work either.
I call now updateUI() after I create the JList - now it works.