i want to write any text or key from swing onscreen keyboard to any caret location like browser notepad office word. i get some example layout, but dont know how to work to system caret. please teach me how it work.
this example code
public class keyboard extends JFrame {
private JPanel contentPane;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
keyboard frame = new keyboard();
frame.setVisible(true);
frame.setAlwaysOnTop(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public keyboard() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
contentPane.setLayout(new GridLayout(3, 3));
for(int i = 1; i <= 9; i++) {
createButton(Integer.toString(i));
}
pack();
}
private void createButton(String label)
{
JButton btn = new JButton(label);
btn.setFocusPainted(false);
btn.setPreferredSize(new Dimension(100, 100));
contentPane.add(btn);
}
public void actionPerformed(ActionEvent e)
{
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
}
});
}
}
Related
I have created 2 JFrames and tying to link two JFrames Windows together. But i couldnt get the content in the 2nd Jframe. Can anyone help me with this?
My first Frame:
public class Main extends JFrame {
private JPanel contentPane;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main frame = new Main();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Main() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnOpenTheJung = new JButton("Open the JUNG window");
btnOpenTheJung.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
**JungLearning1 frame1 = new JungLearning1();
frame1.setVisible(true);
frame1.setSize(600, 400);**
}
});
btnOpenTheJung.setBounds(172, 99, 145, 34);
contentPane.add(btnOpenTheJung);
}}
My second Frame:
public class JungLearning1 extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
DirectedSparseGraph<String, String> g = new DirectedSparseGraph<String, String>();
g.addVertex("Vertex1");
g.addVertex("Vertex2");
g.addVertex("Vertex3");
g.addEdge("Edge1", "Vertex1", "Vertex2");
g.addEdge("Edge2", "Vertex1", "Vertex3");
g.addEdge("Edge3", "Vertex3", "Vertex1");
VisualizationImageServer<String, String> vs = new VisualizationImageServer<String, String>(
new CircleLayout<String, String>(g), new Dimension(
200, 200));
**JFrame frame1 = new JFrame();
frame1.getContentPane().add(vs);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.pack();
frame1.setVisible(true);**
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public JungLearning1() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
}}
I want to display the 2nd frame along with the content when clicking the butten from the first window
I have a simple gui, that is suppose to shows table(JTable). How to make it switch to table1(JTable) after x amount of seconds, and then back to table after x amount of seconds and have that run in a loop. I know setting the other table is nothing more than putting this line in:
scrollPane.setViewportView(table1);
but how to switch from one to the other and back periodically. Guess I have to use the Timer but need some help with this one.thanks
Here is the simple example code:
public class t extends JFrame {
private JPanel contentPane;
private JTable table;
private JTable table1;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
t frame = new t();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public t() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new BorderLayout(0, 0));
JScrollPane scrollPane = new JScrollPane();
contentPane.add(scrollPane, BorderLayout.CENTER);
table = new JTable(10,10);
table1 = new JTable(10,5);
scrollPane.setViewportView(table);
}
}
THE FINAL RESULT:
public class t extends JFrame {
private JPanel contentPane;
private JTable table;
private JTable table1;
private Timer timer;
private Timer timer1;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
t frame = new t();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public t() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new BorderLayout(0, 0));
JScrollPane scrollPane = new JScrollPane();
contentPane.add(scrollPane, BorderLayout.CENTER);
table = new JTable(10,10);
table1 = new JTable(10,5);
scrollPane.setViewportView(table);
ActionListener action = new ActionListener()
{
#Override
public void actionPerformed(ActionEvent event)
{
timer.stop();
scrollPane.setViewportView(table1);
timer1.start();
}
};
ActionListener action1 = new ActionListener()
{
#Override
public void actionPerformed(ActionEvent event)
{
timer1.stop();
scrollPane.setViewportView(table);
timer.start();
}
};
timer = new Timer(3000, action);
timer.start();
timer1 = new Timer(3000, action1);
}
}
here is the working example of what i wanted.thanks everyone for help
when i execute it the first jframe open then there are many jbuttons, I have only given a work or you can say the actionlistner e.It opens the second jframe that is about the same as the first jframe.But when I press the jbutton I have given the work it does not open any other jframe and it is also not finding the void A.
Here is my code:
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
public class GUI_window {
JFrame MF;
JFrame MFA;
JFrame ASF;
JButton ASB = new JButton("THE AREA...");
JTextField JTFASI = new JTextField("");
JLabel JLASR = new JLabel("Please type length of 1 side ");
JButton AS = new JButton("AREA OF A SQUARE");
JButton AR = new JButton("AREA OF A RECTANGLE");
JButton AT = new JButton("AREA OF A TRIANGLE");
JButton LR = new JButton("LENGTH OF A RECTANGLE WHEN YOU KNOW THE BREADTH AND THE AREA");
JButton BR = new JButton("BREATH OF A RECTANGLE WHEN YOU KNOW THE LENGTH AND THE AREA");
JButton OS = new JButton("LENGTH OF 1 SIDE OF A SQUARE WHEN YOU KNOW AND THE AREA");
JButton LT = new JButton("LENGTH OF 1 SIDE OF A TRIANGLE WHEN YOU KNOW LENGTH OF 2 SIDES AND THE AREA");
JButton B = new JButton("BACK");
JLabel JLA = new JLabel("Please tell what you want to know in the topic 'AREA'");
JLabel JLl = new JLabel("Please choose want to know what you ");
JButton F = new JButton("FUNDAMENTAL OPRATIONS");
JButton TA = new JButton("TABLES");
JButton T = new JButton("TEMPERATURE");
JButton E = new JButton("EQUATIONS");
JButton PAR = new JButton("POWER AND ROOTS");
JButton A = new JButton("AREA");
JButton P = new JButton("PERIMETER");
JButton C = new JButton("CIRCUMFERENCE");
JButton EXIT = new JButton("EXIT");
public GUI_window() {
MmF();
EXIT.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
A.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new A();
MF.setVisible(false);
}
});
P.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
MF.setVisible(false);
}
});
T.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
MF.setVisible(false);
}
});
TA.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
MF.setVisible(false);
}
});
F.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
MF.setVisible(false);
}
});
C.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
MF.setVisible(false);
}
});
E.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
MF.setVisible(false);
}
});
PAR.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
MF.setVisible(false);
}
});
}
void MmF() {
MF = new JFrame("MAIN MENU");
JPanel contentPane = (JPanel) MF.getContentPane();
contentPane.setLayout(new BorderLayout(8, 8));
contentPane.setBorder(new EmptyBorder(10, 10, 10, 10));
contentPane.setLayout(new GridLayout(4, 4));
JPanel buttonPanel = new JPanel(new GridLayout(4, 4));
JTextField JTF = new JTextField("");
contentPane.add(JLl, BorderLayout.NORTH);
contentPane.add(F);
contentPane.add(TA);
contentPane.add(T);
contentPane.add(E);
contentPane.add(PAR);
contentPane.add(P);
contentPane.add(A);
contentPane.add(C);
contentPane.add(EXIT);
MF.pack();
MF.setVisible(true);
}
void A() {
AmF();
B.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new GUI_window();
MFA.dispose();
}
});
AS.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new ASa();
}
});
AT.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
AR.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
OS.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
LR.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
LT.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
BR.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
}
void AmF() {
MFA = new JFrame("AREA");
JPanel contentPane = (JPanel) MFA.getContentPane();
contentPane.setLayout(new BorderLayout(8, 8));
contentPane.setBorder(new EmptyBorder(10, 10, 10, 10));
contentPane.setLayout(new GridLayout(4, 4));
JPanel buttonPanel = new JPanel(new GridLayout(4, 4));
contentPane.add(JLA, BorderLayout.NORTH);
contentPane.add(AS);
contentPane.add(AR);
contentPane.add(AT);
contentPane.add(LR);
contentPane.add(BR);
contentPane.add(OS);
contentPane.add(LT);
contentPane.add(B);
MFA.pack();
MFA.setVisible(true);
}
void ASS() {
AmFas();
ASB.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JTFASI.setText(Maths.fforAS());
}
});
}
void AmFas() {
ASF = new JFrame("AREA");
JPanel contentPane = (JPanel) ASF.getContentPane();
contentPane.setLayout(new BorderLayout(8, 8));
contentPane.setBorder(new EmptyBorder(10, 10, 10, 10));
contentPane.setLayout(new GridLayout(4, 4));
JPanel buttonPanel = new JPanel(new GridLayout(4, 4));
contentPane.add(JLASR, BorderLayout.NORTH);
contentPane.add(JTFASI, BorderLayout.NORTH);
contentPane.add(ASB);
ASF.pack();
ASF.setVisible(true);
}
}
I'm attempting to implement a changelistener on a JSlider. I've tried two separate methods neither has worked. The commented out section is the first attempt. The one that's implemented now would probably be the better suited for my purposes. Can anyone point out what is going wrong with this:
public class MixWindow extends JFrame implements ChangeListener{
private JPanel contentPane;
public static int uniA [] = new int [512];
ChangeListener sizeAction;
int level = 0;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MixWindow frame = new MixWindow();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
initUni();
}
public MixWindow() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 800, 600);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JSlider slider = new JSlider(0,255);
slider.setOrientation(SwingConstants.VERTICAL);
slider.setBounds(66, 275, 72, 140);
slider.setPaintTicks(true);
slider.setMajorTickSpacing(20);
slider.setValue(uniA[0]);
//slider.addChangeListener(sizeAction);
contentPane.add(slider);
final JLabel label = new JLabel("");
label.setBounds(66, 262, 61, 16);
contentPane.add(label);
/*sizeAction=new ChangeListener()
{
public void stateChanged (ChangeEvent event)
{
System.out.println("This is getting silly");
JSlider slider=(JSlider)event.getSource();
level=slider.getValue();
uniA[0] = level;
String temp = String.valueOf(level);
label.setText(temp);
}
};*/
}
public static void initUni(){
for(int i = 0; i < uniA.length; i++){
uniA[i] = 0;
}
}
#Override
public void stateChanged(ChangeEvent arg0) {
// TODO Auto-generated method stub
System.out.println("Stuff has changed");
Object source = arg0.getSource();
System.out.println(arg0 + " has Changed");
}
}
The reason the ChangeListener isn't working in your 1st approach, is that your listener reference sizeAction is null when you register the listener.
slider.addChangeListener(sizeAction);
While this won't throw an exception, it won't register the listener when it is instantiated.
Simply alllow this line to appear after you define the listener and it will start working.
If you wish to use your other ChangeListener implementation instead you can use:
slider.addChangeListener(this);
Here's and example code where the code i'm executing runs multiple times.
First class:
public class Test2 extends JFrame {
public static int asd = 0;
private JPanel contentPane;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Test2 frame = new Test2();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Test2() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JButton btnShowtest = new JButton("ShowTest2");
btnShowtest.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Test1 a1 = new Test1();
a1.setVisible(true);
dispose();
}
});
contentPane.add(btnShowtest, BorderLayout.CENTER);
}
}
When button is pressed this one opens:
public class Test1 extends JFrame {
public static int ab = 1;
private JPanel contentPane;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Test1 frame = new Test1();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Test1() {
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JButton btnrun = new JButton("runt");
btnrun.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Test3.error();
}
});
contentPane.add(btnrun, BorderLayout.CENTER);
}
}
When button is pressed it executes this code:
public class Test3 {
public static void error(){
JOptionPane.showMessageDialog(null, "error");
Test2.asd += 1;
System.err.print(Test2.asd);
final Frame[] frames = Frame.getFrames();
for (Frame f : frames){
f.dispose();
Test2 newtet = new Test2();
newtet.setVisible(true);
}
}
}
Now every time I press the buttons again it executes Test3.error(); multiple times.
For example if I press the buttons 10 times then Test3.error(); runs 10 times.
I'm guessing it's a simple fix but I can't figure it out.
Edit:
When i press the btnrun button it runs the "Test3.error()" Code, then closes all frames and creates another main frame.
When i get back to btnrun and press it again, it runs "Test3.error()" 2 times instead of once. 2 JOptionePanes and creates new mainframe twice.
If i do it again it runs "Test3.error()" 3 times and so on it keeps doing that.
What i want is for it to always run "Test3.error()" just once but for some reason it doesn't.
Another example:
public class Frame1 extends JFrame {
private JPanel contentPane;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Frame1 frame = new Frame1();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Frame1() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JButton btnRun = new JButton("Run");
btnRun.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Run.run();
}
});
contentPane.add(btnRun, BorderLayout.CENTER);
}
}
And the code that runs:
public class Run {
public static void run(){
final Frame[] frames = Frame.getFrames();
for (Frame f : frames){
f.dispose();
Frame1 newtet = new Frame1();
newtet.setVisible(true);
}
}
}
Same problem.
Everytime i press the run button, it executes the code as many times as i've pressed it in the past.
Press button once it diposes Frame1 and recreates Frame1.
Press it again and it executes the code 2 times in a row.
(disposes Frame1, creates Frame1, diposes Frame1 and creates Frame1)
Yes, you've added an action listener here:
JButton btnrun = new JButton("runt");
btnrun.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Test3.error();
}
});
That means "When the button is clicked, call Test3.error()." Were you expecting the action listener to be removed automatically after executing once?
EDIT: Okay, I think I've worked out what's going on. This is the problem:
final Frame[] frames = Frame.getFrames();
for (Frame f : frames){
f.dispose();
Test2 newtet = new Test2();
newtet.setVisible(true);
}
For every existing frame, you're disposing that frame and creating a new Test2(). So if you had 3 frames on the screen (a Test1, a Test2 and a Test3) when you click the button, you'll end up with three new Test2 frames. I suspect you didn't mean to do that. Did you mean this instead?
for (Frame f : Frame.getFrames()){
f.dispose();
}
Test2 newtet = new Test2();
newtet.setVisible(true);