|
It looks as though you have too many containers. Try simplifying your setup by adding one element at a time to see where it's going wrong. Start with the tab, then add a panel to the tab, then a single item to the panel etc.
Veni, vidi, abiit domum
|
|
|
|
|
Here is how I see it can be simplified ,and still the problem exists.
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.UIManager;
public class Mainframe extends JFrame
{
private static final long serialVersionUID = 1L;
JTabbedPane tabbedPane = new JTabbedPane();
JPanel jp1 = new JPanel();
JPanel Firstpanel = new JPanel();
static JFrame frame = new JFrame("Create Company");
JButton button1 = new JButton("Button 1");
JButton button2 = new JButton("Button 2");
JButton button3 = new JButton("Button 3");
JButton button4 = new JButton("Button 4");
JButton button5 = new JButton("Button 5");
private Mainframe()
{
BufferedImage myPicture = null;
try
{
myPicture = ImageIO.read(new File("FINAL.jpg"));
}
catch (IOException e1)
{
e1.printStackTrace();
}
JLabel lbl = new JLabel(new ImageIcon(myPicture));
frame.add(tabbedPane);
jp1.add(Firstpanel,BorderLayout.SOUTH);
tabbedPane.addTab("New Employer",jp1);
Firstpanel.add(button1);
Firstpanel.add(button2);
Firstpanel.add(button3);
}
public static void main(String[] args)
{
frame.setPreferredSize(new Dimension(1500, 1210));
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.pack();
frame.setResizable(true);
frame.setVisible(true);
new Mainframe();
}
}
|
|
|
|
|
|
Yes now I know my mistake,thank you
|
|
|
|
|
You need to tell the panel to use a border layout like:
jp1.setLayout(new BorderLayout());
jp1.add(Firstpanel,BorderLayout.SOUTH);
tabbedPane.addTab("New Employer",jp1);
Firstpanel.add(button1);
Firstpanel.add(button2);
Firstpanel.add(button3);
jp1.add(Secondpanel, BorderLayout.NORTH);
Secondpanel.add(button4);
Secondpanel.add(button5);
Veni, vidi, abiit domum
|
|
|
|
|
Thank you for your help
|
|
|
|
|
Hi All,
I am developing an ADF Mobile app in JDeveloper that should connect to Sql Server DB. I included a driver file in C:\Oracle\Middleware\jdeveloper\jdev\lib\sqljdbc4.jar but I am still getting error when I import java.sql.DriverManager.I even included sqljdbc4.jar file in project properties. Please help me solve this problem. Thanks.
|
|
|
|
|
AMADIAR wrote: Please help me solve this problem. Solve what problem, you have not explained what the error is?
Veni, vidi, abiit domum
|
|
|
|
|
Hello,
Please explain the error you are getting.
Regards,
Prasad P. Khandekar
Knowledge exists, man only discovers it.
|
|
|
|
|
I am writing a small data driven application which takes data from an input and throw an event throughout the program that is subscribed to the data event listener. Currently, I have the small test program setup as an infinite loop to see if there are any memory leaks. Testing has shown that there is a massive memory leak and I don't know what to do. I have the program attached:
What is happening is when the event is handled, it is not properly disposed of. Instead, it is thrown onto the stack while a new event is serviced. I thought sleeping the current event would dispose of the last event but...nope! I am out of ideas on how to properly service and dispose of events quickly using this form. Any ideas would be helpful to figure this small technical issue out. Thanks - Ryan
The Main loop which has the data items and the main value observer.
import java.util.*;
public class CMain {
private final static int TIMEOUT = 250;
private static CValue Value;
private static CData AddFive;
private static CData AddTwo;
private static CData AddTen;
private static CData AddSeven;
private static CData MaxValueReached;
public static void main(String[] args) {
Value = new CValue();
AddFive = new CData();
AddFive.setItsAction(new CAction()
{
@Override
public void doAction() throws InterruptedException {
Thread.sleep(TIMEOUT);
if (Value.getItsValue() <= 20)
{
Value.setItsValue(Value.getItsValue() + 5);
}
}
});
Value.addObserver(AddFive);
AddTwo = new CData();
AddTwo.setItsAction(new CAction()
{
public void doAction() throws InterruptedException{
Thread.sleep(TIMEOUT);
if (Value.getItsValue() > 20 && Value.getItsValue() <= 40)
{
Value.setItsValue(Value.getItsValue() + 2);
}
}
});
Value.addObserver(AddTwo);
AddTen = new CData();
AddTen.setItsAction(new CAction(){
public void doAction() throws InterruptedException{
Thread.sleep(TIMEOUT);
if (Value.getItsValue() > 40 && Value.getItsValue() <= 80)
{
Value.setItsValue(Value.getItsValue() + 10);
}
}
});
Value.addObserver(AddTen);
AddSeven = new CData();
AddSeven.setItsAction(new CAction(){
public void doAction() throws InterruptedException{
Thread.sleep(TIMEOUT);
if (Value.getItsValue() > 80)
{
Value.setItsValue(Value.getItsValue() + 7);
}
}
});
Value.addObserver(AddSeven);
MaxValueReached = new CData();
MaxValueReached.setItsAction(new CAction()
{
public void doAction() throws InterruptedException{
Thread.sleep(TIMEOUT);
if (Value.getItsValue() >= 100)
{
Value.setItsValue(0);
System.out.print("Value Reset back to 0\n");
}
}
});
Value.addObserver(MaxValueReached);
Value.startBehavior();
while (true);
}
}
The CValue class
import java.util.Observable;
public class CValue extends Observable
{
private double itsValue = 0;
public double getItsValue() {
return itsValue;
}
public void setItsValue(double itsValue) {
if (this.itsValue != itsValue)
{
this.itsValue = itsValue;
System.out.print("Value set to " + itsValue + "\n");
System.out.print(false);
setChanged();
notifyObservers();
}
}
public void startBehavior()
{
setChanged();
notifyObservers();
}
}
The CData Class
import java.util.Observable;
import java.util.Observer;
public class CData extends Observable implements Observer
{
public CData()
{
}
@Override
public void update(Observable arg0, Object arg1) {
if (itsAction != null)
{
try {
this.itsAction.doAction();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private CAction itsAction = null;
public CAction getItsAction() {
return itsAction;
}
public void setItsAction(CAction itsAction) {
this.itsAction = itsAction;
}
}
And to explain what this CAction thing is:
public interface CAction {
public void doAction() throws InterruptedException;
}
|
|
|
|
|
I think you failed to understand the publisher-subscriber framework. Publisher has many subscriber .i.e one to many relation ship with subscriber.Publisher post the new event(if any), and all the subscriber which have the subscription, will catch the event. So this is pretty much concept. In java , publisher as Observable and subscriber as Observer. Now, in your app you create an Observable (CValue). Then you create Observer which is again an Observable(CData extends Observable) and register this with the Observable, means you register an Observable with another Observable. So fixed this things before moving forward
Regards
Shubhashish
|
|
|
|
|
Shubhashish, thank you for taking the time to analyze and reply to my original message. I am learning new concepts in Java to become a better programmer.
I have taken you comments into serious consideration and updated the code. The class headers for CValue and CData are as follows:
public class CValue extends Observable
public class CData implements Observer
I would execute the test program for a minute to two minutes before its crashes out. What I am observing during execution is this:
CMain.doAction()
CValue.setItsValue(double)
CValue.notifyObservers()
CData.update(Observerable,Object)
CMain.doAction
...
Repeat until thread crash
It seems the events are not being closed completely after the servicing of the observer. I know I am creating a lot of events being generated quickly but isn't there a mechanism which would close these events or am I not closing them down properly myself?
Ryan
|
|
|
|
|
|
I have been using java2s for all my questions on this problem.
Updated Code: (Use the original posted code for classes not posted here)
public class CData implements Observer
{
public CData()
{
}
@Override
public void update(Observable arg0, Object arg1) {
if (itsAction != null)
{
try {
this.itsAction.doAction();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private CAction itsAction = null;
public CAction getItsAction() {
return itsAction;
}
public void setItsAction(CAction itsAction) {
this.itsAction = itsAction;
}
}
public class CValue extends Observable
{
private double itsValue = 0;
public double getItsValue() {
return itsValue;
}
public void setItsValue(double itsValue) {
if (this.itsValue != itsValue)
{
this.itsValue = itsValue;
System.out.print("Value set to " + itsValue + "\n");
setChanged();
notifyObservers();
}
}
public void startBehavior()
{
setChanged();
notifyObservers();
}
}
Observation: When I execute setChanged and then execute notifyObservers, the test program does a re-calculation on the new data. If the re-calculation doesn't change the value, then no new notifications are required. If there is a change in data from the re-calculation then the process is started all over again. I made a small modification to CMain to stop the notifications after a period of time and restart the re-calculation process again. This is eliminating the memory leak completely.
From these new revelations, attached is my new CMain code
public class CMain {
private final static int TIMEOUT = 5;
private static CValue Value;
private static CData AddFive;
private static CData AddTwo;
private static CData AddTen;
private static CData AddSeven;
private static CData MaxValueReached;
public static void main(String[] args) {
Value = new CValue();
AddFive = new CData();
AddFive.setItsAction(new CAction()
{
@Override
public void doAction() throws InterruptedException {
Thread.sleep(TIMEOUT);
if (Value.getItsValue() <= 20)
{
Value.setItsValue(Value.getItsValue() + 5);
}
}
});
Value.addObserver(AddFive);
AddTwo = new CData();
AddTwo.setItsAction(new CAction()
{
public void doAction() throws InterruptedException{
Thread.sleep(TIMEOUT);
if (Value.getItsValue() > 20 && Value.getItsValue() <= 40)
{
Value.setItsValue(Value.getItsValue() + 2);
}
}
});
Value.addObserver(AddTwo);
AddTen = new CData();
AddTen.setItsAction(new CAction(){
public void doAction() throws InterruptedException{
Thread.sleep(TIMEOUT);
if (Value.getItsValue() > 40 && Value.getItsValue() <= 80)
{
Value.setItsValue(Value.getItsValue() + 10);
}
}
});
Value.addObserver(AddTen);
AddSeven = new CData();
AddSeven.setItsAction(new CAction(){
public void doAction() throws InterruptedException{
Thread.sleep(TIMEOUT);
if (Value.getItsValue() > 80 && Value.getItsValue() <= 100)
{
Value.setItsValue(Value.getItsValue() + 7);
}
}
});
Value.addObserver(AddSeven);
MaxValueReached = new CData();
MaxValueReached.setItsAction(new CAction()
{
public void doAction() throws InterruptedException{
Thread.sleep(TIMEOUT);
if (Value.getItsValue() >= 100)
{
System.out.print("Maximum Value Reached\n");
}
}
});
Value.addObserver(MaxValueReached);
Value.startBehavior();
while (true)
{
if (Value.getItsValue() >= 100)
{
Value.setItsValue(0);
}
}
}
}
Again, thanks for the feedback
|
|
|
|
|
few things that you have to know. Role of the Observer is to notify each subscriber if any changes happen.
If you see in your code there is a recursive call exist. In your main class , at the end, you are setting value in Observer. So Observer value changed and it convey the notify message to the subscriber.Again If you see in doAction() ,you can see that each Subscriber receive the notify message and again set the Observer value with new one. There for this action again update the Observer value and again Observer doing the above thing and thus fall in recursive call.
Regards
Shubhashish
|
|
|
|
|
Where "Observer" is actually "Observable" as you got it right in your first post, since "Observer" and "Subscriber" are basically the same thing.
|
|
|
|
|
Hi,
I want to implement SVM in Java. I have implemented svm in Matlab before but I am new to Java and I found a library LIBSVM which I can use. Is there any other library that I could use?
Thanks
|
|
|
|
|
Presumably what you want is to USE it in your code rather than "implement" (code yourself).
In that case google is the place to start.
|
|
|
|
|
What are the basic deferences between Get & Set methods in Java?
|
|
|
|
|
this sounds like homework questions to me
MCAD
---
|
|
|
|
|
The same as they are in any language, and a read of the documantation or a search[^] would easily find the answer for you.
Veni, vidi, abiit domum
|
|
|
|
|
In Java, one typically tries to avoid having all data structures set as public.
So if it is set to private, getters are a way to access that data value, as the variable may be private, but the getter is public.
Same with setters, if you are trying to modify the value of the variable through another class, and if the variable is private to one class, then a public setter method can modify that value effectively.
|
|
|
|
|
Hey guys,
I'm trying to learn java and I've got stuck on this problem.
I made a program with GUI that when I input on a text field and press enter it will display the value that was entered on a separate JOptionPane dialog box.
I'm trying to combine 2 inputs before displaying them but I cant find the code that will do the trick, here is the code I think I need to change:
if (eventHand.getSource()==item1)
string = String.format("Field 1: %s",eventHand.getActionCommand());
if (eventHand.getSource()==item2)
string = String.format("Field 2: %s",eventHand.getActionCommand());
JOptionPane.showMessageDialog(null,"" +string);
the value is stored on the variable named string, I'm looking for a way store the 2 value on each variable first then display it on the same messagebox.
Thanks,
|
|
|
|
|
Something like:
if (eventHand.getSource()==item1) {
string.concat("Field 1: ");
string.concat(eventHand.getActionCommand());
string.concat(", Field 2: ");
string.concat(item2.Text);
}
if (eventHand.getSource()==item2) {
string.concat("Field 1: ");
string.concat(item1.Text);
string.concat(", Field 2: ");
string.concat(eventHand.getActionCommand());
}
JOptionPane.showMessageDialog(null, string);
Veni, vidi, abiit domum
|
|
|
|
|
Hi,
"Text" part of this (item2.Text); is not working but I think I can handle this already.
also adding a dedicated button also helps me to think it in an organized way.
Thanks,
|
|
|
|