JLabel Not Showing When resetting JFrame to setVisible(true) - jframe

I have created an Abstract Class for common things to be useful in my entire program
public interface Global {
static final String regexdate="^(((0[1-9]|[12][0-9]|3[01])[- /.](0[13578]|1[02])|(0[1-9]|[12][0-9]|30)[- /.](0[469]|11)|(0[1-9]|1\\d|2[0-8])[- /.]02)[- /.]\\d{4}|29[- /.]02[- /.](\\d{2}(0[48]|[2468][048]|[13579][26])|([02468][048]|[1359][26])00))$";
static final String regexemail="\\b[\\w\\.-]+#[\\w\\.-]+\\.\\w{2,4}+\\b$";
static final String regexalpha="\\b[a-zA-Z .]+\\b$";
static final String regexnumbersonly="\\b[^a-zA-Z ,.+-]*\\d\\b$";
static final String regexalphaonly="\\b[a-zA-Z]+\\b$";
//address would allow any character case insensitive and numbers (for house number) "-" space "," and "/"
static final String regexaddress="\\b[a-zA-Z\\s 0-9-,/\\n]*\\b$";
static final String regexalphanumeric="\\b[a-zA-Z0-9 .]+\\b$";
static final String regexisbn="\\b[0-9-]+\\b$";
static final String regexdecimals="\\b[^a-zA-Z ,+-]*\\d\\b$";
// single character datatype validation regex
static final String regexchar="^[.a-zA-Z\s]+$";
static LocalDate localDate = LocalDate.now();
static DayOfWeek dayofweek=localDate.getDayOfWeek();
static int dayofmonth=localDate.getDayOfMonth();
static Month monthofyear=localDate.getMonth();
static int year=localDate.getYear();
static String properday=dayofweek.toString().substring(0,1)+dayofweek.toString().substring(1,3).toLowerCase();
static String propermonth=monthofyear.toString().substring(0, 1)+monthofyear.toString().substring(1, 3).toLowerCase();
static String dayfull=properday+" "+Integer.valueOf(dayofmonth)+"-"+propermonth+"-"+Integer.valueOf(year).toString().substring(2,4);
static final Font myfont=new Font("Serg UI",Font.PLAIN,16);
static final Font myfontheader=new Font("Serg UI",Font.BOLD,20);
static final Font myfonttoget=new Font("serg UI",Font.CENTER_BASELINE,16);
static final Color mybackground= new Color(231, 231, 231);
public JLabel title=new JLabel("Library Management System",SwingConstants.LEFT);
public JLabel welcome=new JLabel ("Welcome :-",SwingConstants.LEFT);
public JLabel ctandd=new JLabel(dayfull);
}
Now I have two separate classes where i am using all of above as required
admintitle.setBounds(0,0,900,49);
title.setBounds(310, 10, 335,20);
welcome.setBounds(1, 0, 130, 20);
ctandd.setBounds(790, 5,225, 20);
totalmem.setBounds(1, 25,100, 20);
adminpanel.setBackground(new Color(1,3,45));
JLabel addvalue=new JLabel("10");
addvalue.setFont(myfont);
addvalue.setForeground(Color.WHITE);
addvalue.setSize(20, 20);
today.add(addvalue);
lmember.setAlignmentX(Component.LEFT_ALIGNMENT);
separator.setAlignmentX(Component.LEFT_ALIGNMENT);
addmem.setAlignmentX(Component.CENTER_ALIGNMENT);
modifymem.setAlignmentX(Component.CENTER_ALIGNMENT);
delmem.setAlignmentX(Component.CENTER_ALIGNMENT);
admintitle.add(title);
admintitle.add(welcome);
admintitle.add(ctandd);
admintitle.add(totalmem);
adminpanel.add(lmember);
adminpanel.add(separator);
adminpanel.add(addmem);
adminpanel.add(modifymem);
adminpanel.add(delmem);
adminpanel.add(separator1);
adminpanel.add(lbook);
adminpanel.add(separator2);
adminpanel.add(addbook);
adminpanel.add(modifyinven);
adminpanel.add(delbook);
adminpanel.add(separator3);
adminpanel.add(lrepo);
adminpanel.add(penaltyrepo);
adminpanel.add(separator4);
adminpanel.add(newrequest);
adminpanel.add(exit);
admindisplay.add(totaldue);
admindisplay.add(totalpenalty);
admindisplay.add(penaltyamt);
admindisplay.add(memadded);
admindisplay.add(booksadded);
admindisplay.add(bookslost);
admindisplay.add(booksoverdue);
admindisplay.add(memdiscontinued);
admindisplay.add(newbookrequest);
admindisplay.add(memhead);
admindisplay.add(separator5);
admindisplay.add(bookhead);
admindisplay.add(separator6);
admindisplay.add(summary);
admindisplay.add(today);
admindisplay.add(weekly);
admindisplay.add(monthly);
admindisplay.add(yearly);
admin.add(admintitle);
admin.add(adminpanel);
admin.add(admindisplay);
admin.setVisible(true);
System.out.println(admin.getComponentCount());
System.out.println(admin.getComponentZOrder(title));
System.out.println(admindisplay.getComponentCount());
System.out.println(adminpanel.getComponentCount());
System.out.println(admintitle.getComponentCount());
} // end of admin ui
from this class when I click on a button I navigate to another class
addmem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
// do login code
admin.setVisible(false);
uiaddmem.caddmemUI(admin);
}});
In another class I do something and upon user clicking back button I come back to this JFrame
back.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
modmember.setVisible(false);
admin.setVisible(true);
admin.toFront();
admin.validate();
System.out.println(admin.getComponentCount());
System.out.println(admin.getComponentZOrder(title));
System.out.println(admintitle.getComponentCount());
}});
Eveything works fine except that my title, welcome, total and ctandd just disappear from the first JFrame when I make admin.setVisible(true) as above.
As you can see I have done componentcount the numbers show that the three labels should be there also I individually tried sysout title.isDisplayable() title.isVisible()
title.isShowing() all of these are true but still the title, welcome, ctandd is not displayed when I return to my admin JFrame

Related

Same AjaxEventBehavior for multiple components

I add an image and a text component to a WebMarkupContainer as described here:
filter.add(newFilterLabel("textSub", customerText, filtervalue));
filter.add(newFilterImage("imgSub", filtervalue));
For each component, there is an AjaxEventBehavior doing different things. I would like to change it in a way that both do the same thing independent from which component has been clicked on.
private Component newFilterLabel(String id, IModel<String> customText,
final SourceFilterValue currentValue) {
final BBLabel label = new BBLabel(id, customText);
label.add(new AjaxEventBehavior("onclick") {
private static final long serialVersionUID = 1L;
#Override
protected void onEvent(AjaxRequestTarget target) {
doSomething(currentValue,filteredsources, target);
}
});
return label;
}
private Image newFilterImage(String id, final SourceFilterValue filterValue) {
final Image img = new Image(id, resources.getImage(EXPAND_ICON));
img.add(new AjaxEventBehavior("onclick") {
private static final long serialVersionUID = 1L;
#Override
protected void onEvent(AjaxRequestTarget target) {
doSomething(img);
}
});
return img;
}
Do you have any suggestions how to change it or any workaround? I use Wicket 1.5.8.
An AjaxBehavior can be bound to a single element only. Either add it to a parent in the hierarchy, or just let both behaviors call the same method:
#Override
protected void onEvent(AjaxRequestTarget target) {
doSomething(filterValue); //filterValue has to be final to be able to access it from the inner class
}

Google Web Toolkit - How to prevent duplication of the records?

I am new in GWT. I am trying to use the cell table to do this. Here is my questions:
Name Gender
Ali M
Abu M
Siti F
page 1
Name Gender
Siti F
Noor F
Ahmad F
page 2
I use simple pager to do the paging function. Everything is ok except next page.
When i click next page, siti record appear 2 times.
How to prevent the name Siti not appear in page 2? Below are my code:
private static class Contact{
private final String name;
private final String gender;
public Contact(String name, String gender){
this.name = name;
this.gender = gender;
}
}
private static final List<Contact> CONTACTS = Arrays.asList(
new Contact("Ali","M"),
new Contact("Abu","M"),
new Contact("Siti","F"),
new Contact("Noor","F"),
new Contact("Ahmad","M")
);
public void onModuleLoad(){
final CellTable<Contact> table = new CellTable<Contact>();
table.setPageSize(3);
TextColumn<Contact> nameColumn = new TextColumn<Contact>(){
#Override
public String getValue(Contact object) {
return object.name;
}
};
TextColumn<Contact> genderColumn = new TextColumn<Contact>(){
#Override
public String getValue(Contact object) {
return object.gender;
}
};
table.addColumn(nameColumn, "Name");
table.addColumn(genderColumn, "Gender");
AsyncDataProvider<Contact> provider = new AsyncDataProvider<Contact>(){
#Override
protected void onRangeChanged(HasData<Contact> display) {
int start = display.getVisibleRange().getStart();
int end = start + display.getVisibleRange().getLength();
end = end >= CONTACTS.size() ? CONTACTS.size() : end;
List<Contact> sub = CONTACTS.subList(start,end);
updateRowData(start,sub);
}
};
provider.addDataDisplay(table);
provider.updateRowCount(CONTACTS.size(), true);
SimplePager.Resources pagerResources = GWT.create(SimplePager.Resources.class);
SimplePager pager = new SimplePager(TextLocation.CENTER, pagerResources, false, 0, true);
pager.setDisplay(table);
Please help me to solve this problem. Thanks.
You have faced most probably the gwt last page problem, described in the linked questions:
GWT - celltable with simple pager issue
SimplePager row count is working incorrectly
The solution here is to set:
setRangeLimited(false)
and the last page is paged correctly, ie. it contains only Noor and Ahmad.
So in conclusion: actually no duplication is present here, but a bug on pagination in case of the last page. You will observe the same behavior with also other amounts of data, but on my view point it would be always a last page issue only.

Why isn't my JFrame displaying anything from my Jpanel?

I'm working on creating a 3 man's morris board, but nothing is being displayed on the frame. It's empty despite having added my JPanel. Everything is fine if I used board = new JPanel(new GridLayout()); and do the following, but I wouldn't be able to draw the lines that would draw the board. I've looked over it a few times but can't seem to find a problem.
public class Project5 extends JFrame {
public final static int FRAME_WIDTH = 600;
public final static int FRAME_HEIGHT = 600;
private JButton jb[] = new JButton[9];
private Board board = new Board();
Project5(){
for(int i = 0; i<9; i++){
jb[i] = new JButton();
board.add(jb[i]);
}
add(board);
}
public static void main(String[] args) {
JFrame frame = new Project5();
frame.setTitle("Three Man's Morris");
frame.setSize(Project5.FRAME_WIDTH,Project5.FRAME_HEIGHT);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class Board extends JPanel{
public Board(){
super();
setLayout(new GridLayout(3,0,Project5.FRAME_WIDTH,Project5.FRAME_HEIGHT));
}
#Override
public void paintComponents(Graphics g){
super.paintComponents(g);
g.drawLine(0, Project5.FRAME_WIDTH, 0, Project5.FRAME_HEIGHT);
g.drawLine(0, 0, 0, Project5.FRAME_HEIGHT);
g.drawLine(0,Project5.FRAME_WIDTH,0,0);
g.drawLine(0, Project5.FRAME_HEIGHT, Project5.FRAME_WIDTH, Project5.FRAME_HEIGHT);
g.drawLine(Project5.FRAME_WIDTH, 0, 0, Project5.FRAME_HEIGHT);
g.drawLine(Project5.FRAME_WIDTH,0,Project5.FRAME_WIDTH,Project5.FRAME_HEIGHT);
}
}
The problem is in your GridLayout() parameters :
GridLayout(rows,cols,horizontal_gap,vertical_gap)
in your case, both gaps are 600 (FRAME_WIDTH, FRAME_HEIGHT) !
The buttons are displayed, but they are outside the panel, try to lower the gap,
i.e. : setLayout(new GridLayout(3,0,0,0));
You should see the buttons.

Maximum length of JTextField and only accepts numbers?

I want the user to input a maximum of 8 numbers as it is a field for Mobile number.
This is my JTextField.
txtMobile = new JTextField();
txtMobile.setColumns(10);
txtMobile.setBounds(235, 345, 145, 25);
add(txtMobile);
While we're at it, how do I check for invalid characters like >> '^%$* in a JTextField?
1)Maximum Length
2)Accepts only numbers
3)Check for invalid characters
4)Check if it's a valid email address
Please help :D
You could use a JFormattedField, check out How to Use Formatted Text Fields, but they tend not to restrict the user from entering what ever they want, but instead does a post validation of the value to see if meets the needs of the format you sepcify
You could use a DocumentFilter instead, which would allow you to filter the input in real time.
Take a look at Implementing a Document Filter and MDP's Weblog for examples
Look at this sample example it will only accepts numbers as an input, as an answer to your second requirement.
public class InputInteger
{
private JTextField tField;
private JLabel label=new JLabel();
private MyDocumentFilter documentFilter;
private void displayGUI()
{
JFrame frame = new JFrame("Input Integer Example");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel contentPane = new JPanel();
contentPane.setBorder(
BorderFactory.createEmptyBorder(5, 5, 5, 5));
tField = new JTextField(10);
((AbstractDocument)tField.getDocument()).setDocumentFilter(
new MyDocumentFilter());
contentPane.add(tField);
contentPane.add(label);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args)
{
Runnable runnable = new Runnable()
{
#Override
public void run()
{
new InputInteger().displayGUI();
}
};
EventQueue.invokeLater(runnable);
}
}
class MyDocumentFilter extends DocumentFilter{
private static final long serialVersionUID = 1L;
#Override
public void insertString(FilterBypass fb, int off
, String str, AttributeSet attr)
throws BadLocationException
{
// remove non-digits
fb.insertString(off, str.replaceAll("\\D++", ""), attr);
}
#Override
public void replace(FilterBypass fb, int off
, int len, String str, AttributeSet attr)
throws BadLocationException
{
// remove non-digits
fb.replace(off, len, str.replaceAll("\\D++", ""), attr);
}
}

GWT-Editors and sub-editors

I'm trying to run an example of Editors with sub-editors.
When flushing the parent the value of child editor is null.
The classes are Person and Address.
The main editor is:
// editor fields
public TextField firstname;
public TextField lastname;
public NumberField<Integer> id;
public AddressEditor address = new AddressEditor();
public PersonEditor(Person p){
asWidget();
}
#Override
public Widget asWidget() {
setWidth(400);
setBodyStyle("padding: 5px;");
setHeaderVisible(false);
VerticalLayoutContainer c = new VerticalLayoutContainer();
id = new NumberField<Integer>(new IntegerPropertyEditor());
// id.setName("id");
id.setFormat(NumberFormat.getFormat("0.00"));
id.setAllowNegative(false);
c.add(new FieldLabel(id, "id"), new VerticalLayoutData(1, -1));
firstname = new TextField();
// firstname.setName("firstname");
c.add(new FieldLabel(firstname, "firstname"), new VerticalLayoutData(1, -1));
lastname = new TextField();
lastname.setName("lastname");
c.add(new FieldLabel(lastname, "lastname"), new VerticalLayoutData(1, -1));
c.add(address);
add(c);
return this;
The sub-editor:
public class AddressEditor extends Composite implements Editor<Address> {
private AddressProperties props = GWT.create(AddressProperties.class);
private ListStore<Address> store = new ListStore<Address>(props.key());
ComboBox<Address> address;
public AddressEditor() {
for(int i = 0; i < 5; i ++)
store.add(new Address("city" + i));
address = new ComboBox<Address>(store, props.nameLabel());
address.setAllowBlank(false);
address.setForceSelection(true);
address.setTriggerAction(TriggerAction.ALL);
initWidget(address);
}
And this is where the Driver is created:
private HorizontalPanel hp;
private Person googleContact;
PersonDriver driver = GWT.create(PersonDriver.class);
public void onModuleLoad() {
hp = new HorizontalPanel();
hp.setSpacing(10);
googleContact = new Person();
PersonEditor pe = new PersonEditor(googleContact);
driver.initialize(pe);
driver.edit(googleContact);
TextButton save = new TextButton("Save");
save.addSelectHandler(new SelectHandler() {
#Override
public void onSelect(SelectEvent event) {
googleContact = driver.flush();
System.out.println(googleContact.getFirstname() + ", " + googleContact.getAddress().getCity());
if (driver.hasErrors()) {
new MessageBox("Please correct the errors before saving.").show();
return;
}
}
});
The value of googleContact.getFirstname() is filled but googleContact.getAddress() is always null.
What I'm missing?
The AddressEditor needs to map to the Address model - presently, it doesn't seem to, unless Address only has one getter/setter, called getAddress() and setAddress(Address), which wouldn't really make a lot of sense.
If you want just a ComboBox<Address> (which implements Editor<Address> already), consider putting that combo in the PersonEditor directly. Otherwise, you'll need to add #Path("") to the AddressEditor.address field, to indicate that it should be directly editing the value itself, and not a sub property (i.e. person.getAddress().getAddress()).
Another way to build an address editor would be to list each of the properties of the Address type in the AddressEditor. This is what the driver is expecting by default, so it is confused when it sees a field called 'address'.
Two quick thoughts on the code itself: there is no need to pass a person into the PersonEditor - thats the job of the driver itself. Second, your editor fields do not need to be public, they just can't be private.