Switching from one Jtable to another Jtable every x seconds - java

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

Related

JScrollPane scroll only appears at bottom

I have this JFrame that has a JScrollPane and a JTextArea inside it, it works okay but the scroll button only appears at the bottom, I want it to show at the top as default. How can I change this?
public class frameMyPasswords extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
frameMyPasswords frame = new frameMyPasswords();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public frameMyPasswords() {
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
//scrollPane.getViewport().setViewPosition( new Point(0, 0) );
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.getVerticalScrollBar().setValue(0);
scrollPane.setBounds(10, 45, 414, 194);
contentPane.add(scrollPane);
JTextArea txtField = new JTextArea();
scrollPane.setViewportView(txtField);
txtField.setLineWrap(true);
txtField.setEditable(false);
txtField.setToolTipText("");
txtField.setText("LONG TEXT");
}
}

how to set input text onscreen swing keyboard to system caret?

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) {
}
});
}
}

Connecting 2 Jframes with JUNG

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

Why JPanel.focusGaind and Lost don't work?

Please take a look at the following code (I've missed the imports purposely)
public class MainFrame extends JFrame {
private JPanel contentPane;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainFrame frame = new MainFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public MainFrame() {
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);
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
tabbedPane.setBounds(10, 11, 414, 240);
contentPane.add(tabbedPane);
JPanel panel = new JPanel();
panel.addFocusListener(new FocusListener() {
#Override
public void focusLost(FocusEvent arg0) {
System.out.println("lost");
// I want to do something here, if I reach here!
}
#Override
public void focusGained(FocusEvent arg0) {
System.out.println("gained");
// I want to do something here, if I reach here!
}
});
tabbedPane.addTab("New tab", null, panel, null);
JButton button = new JButton("New button");
panel.add(button);
JPanel panel_1 = new JPanel();
tabbedPane.addTab("New tab", null, panel_1, null);
JPanel panel_2 = new JPanel();
tabbedPane.addTab("New tab", null, panel_2, null);
}
}
I've created this class to test it and then add the onFocusListener in my main code, but it's not working the way I expect. Please tell what's wrong or is this the right EvenetListener at all?
JPanels are not focusable by default. If you ever wanted to use a FocusListener on them, you'd first have to change this property via setFocusable(true).
But even if you do this, a FocusListener is not what you want.
Instead I'd look to listen to the JTabbedPane's model for changes. It uses a SingleSelectionModel, and you can add a ChangeListener to this model, listen for changes, check the component that is currently being displayed and if your component, react.
You are using setBounds and null layouts, something that you will want to avoid doing if you are planning on creating and maintaining anything more than a toy Swing program.
Edit
For example:
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.*;
import javax.swing.event.*;
#SuppressWarnings("serial")
public class MainPanel extends JPanel {
private static final int PREF_W = 450;
private static final int PREF_H = 300;
private static final int GAP = 5;
private static final int TAB_COUNT = 5;
private JTabbedPane tabbedPane = new JTabbedPane();
public MainPanel() {
for (int i = 0; i < TAB_COUNT; i++) {
JPanel panel = new JPanel();
panel.add(new JButton("Button " + (i + 1)));
panel.setName("Panel " + (i + 1));
tabbedPane.add(panel.getName(), panel);
}
setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
setLayout(new BorderLayout());
add(tabbedPane, BorderLayout.CENTER);
tabbedPane.getModel().addChangeListener(new ChangeListener() {
#Override
public void stateChanged(ChangeEvent evt) {
Component component = tabbedPane.getSelectedComponent();
System.out.println("Component Selected: " + component.getName());
}
});
}
#Override
public Dimension getPreferredSize() {
return new Dimension(PREF_W, PREF_H);
}
private static void createAndShowGui() {
MainPanel mainPanel = new MainPanel();
JFrame frame = new JFrame("MainPanel");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
JPanel is a lightweight container and it is not a Actionable component so it does not get focus events. It lets you add focus listener because of swing component hierarchy. In Order to get tab selected events you need to use JTabbedPane#addChangeListener.
Hope this helps.

Code runs multiple times

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);

Categories