I am completely new to this.....
Well, actually I am having a JPanel having two panels(say upper and lower panel) upper panel is a JPanel and lower panel is a JTabbedPane..
inside the JtabbedPane I have two tabs. each tab contains a JPanel(having null layout) which has 1 or more components like jtextfield, jbutton, jscrollpane(with jtable) etc.............
the problem is that when i switch to any tab, i am not able to get the normal jpanel effect. the buttons cannot be clicked with mouse(can be clicked by pressing tab key for selecting the button n then pressing space key), the text field cannot be selected on mouse click(can be selected only by pressing tab key) etc.
so is there any solution for this ?? i want the panels inside the jtabbedpane to work as a normal jpanel would :) thank you :)
Without any MCVE it is hard to help, so i made a simple example app, that, i hope, fits on your app.
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainFrame extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private static MainFrame frame;
private MainFrame() {
this.setTitle("Focus on JTabbedPane");
this.setSize(new Dimension(800, 600));
this.getContentPane().add(initComponents());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private JPanel initComponents() {
JPanel panel = new JPanel(new BorderLayout(10, 10));
panel.add(new MyInfoPanel(), BorderLayout.NORTH);
panel.add(new MyTabPanel(), BorderLayout.CENTER);
return panel;
}
public static void createAndShowGui() {
frame = new MainFrame();
frame.setVisible(true);
}
}
The first JPanel with only a Infotext
import javax.swing.JLabel;
import javax.swing.JPanel;
public class MyInfoPanel extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
public MyInfoPanel() {
this.add(new JLabel("Some info text"));
}
}
The MyTabbedPanel
import javax.swing.JTabbedPane;
public class MyTabPanel extends JTabbedPane {
/**
*
*/
private static final long serialVersionUID = 1L;
public MyTabPanel() {
this.addTab("Tab 1", new FirstTabPanel());
this.addTab("Tab 2", new SecondTabPanel());
}
}
The two TabPanels
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class FirstTabPanel extends JPanel implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
JButton button;
JTextField tf;
public FirstTabPanel() {
tf = new JTextField("Textfield");
this.add(tf);
button = new JButton("Button");
button.addActionListener(this);
this.add(button);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(button)) {
tf.setText("Button on was clicked!");
}
}
}
The SecondTabPanel
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class SecondTabPanel extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
JTextArea area;
JTextField field;
public SecondTabPanel() {
this.setLayout(new BorderLayout(10, 10));
field = new JTextField();
this.add(field, BorderLayout.NORTH);
area = new JTextArea();
this.add(area, BorderLayout.CENTER);
field.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
area.setText(area.getText() + e.getKeyChar());
}
});
}
}
And start it
import javax.swing.SwingUtilities;
public class App {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
MainFrame.createAndShowGui();
}
});
}
}
Hope this mini app will help you.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.