adding a label to a text field - java

Hi I am trying to create an interface consisting of a JComboBox and a JTextField. I have sorted out the code to add a label to the JComboBox but I am having trouble adding a label to the text field. Any help would be appreciated.
import javax.swing. *;
import java.awt.event. *;
import java.awt.FlowLayout;
import java.lang.Math;
public class AreaFrame3 extends JFrame
{
public static void main(``String[]args)
{
//Create array containing shapes
String[] shapes ={"(no shape selected)","Circle","Equilateral Triangle","Square"};
//Use combobox to create drop down menu
JComboBox comboBox=new JComboBox(shapes);
JPanel panel1 = new JPanel(new FlowLayout()); //set frame layout
JLabel label1 = new JLabel("Select shape:");
panel1.add(label1);
panel1.add(comboBox);
JTextField text = new JTextField(10); //create text field
JFrame frame=new JFrame("Area Calculator Window");//create a JFrame to put combobox
frame.setLayout(new FlowLayout()); //set layout
frame.add(panel1);
frame.add(text);
JButton button = new JButton("GO"); //create GO button
frame.add(button);
//set default close operation for JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//set JFrame ssize
frame.setSize(400,250);
//make JFrame visible. So we can see it
frame.setVisible(true);
}
}

Here is one way to do it. Simply put all the widgets in your panel1 in appropriate order.
In the long run this is probably not very much maintainable and you would want to have a better LayoutManager than FlowLayout, but if you just want to learn Swing, this may be a good start. If you feel that FlowLayout is not good enough, take a look at the LayoutManager tutorial. My personal favourites are: BorderLayout and GridBagLayout. MigLayout may also be a good one, but I have never used it and it is not part of the JVM.
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class AreaFrame3 {
protected void initUI() {
// Create array containing shapes
String[] shapes = { "(no shape selected)", "Circle", "Equilateral Triangle", "Square" };
// Use combobox to create drop down menu
JComboBox comboBox = new JComboBox(shapes);
JLabel label1 = new JLabel("Select shape:");
JPanel panel1 = new JPanel(new FlowLayout()); // set frame layout
JLabel label2 = new JLabel("Text label:");
JTextField text = new JTextField(10); // create text field
panel1.add(label1);
panel1.add(comboBox);
panel1.add(label2);
panel1.add(text);
JFrame frame = new JFrame("Area Calculator Window");// create a JFrame to put combobox
frame.setLayout(new FlowLayout()); // set layout
frame.add(panel1);
JButton button = new JButton("GO"); // create GO button
frame.add(button);
// set default close operation for JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
// make JFrame visible. So we can see it
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new AreaFrame3().initUI();
}
});
}
}

Related

Problem with buttons exceeding the Panel (JAVA)

I'm having a problem with a layout I'm trying to do in java. I have 2 panels in a 800x600 frame. The first panel "gamePanel" is (600x600) and the second "menuPanel" is (200x600).
In the menuPanel there are 4 buttons that I tried to organize as a single column of 4 rows using gridLayout(which partially worked). The buttons appear to be in place but when hovering on them they expand occupying the other panel (gamePanel). I tried placing them using setBounds but they directly disappear.
This is how it works before hovering the buttons.
After hovering 2 buttons, but all 4 are displayed the same way
Here is the code:
public class Layout {
Point point = new Point();
public Layout() {
JFrame window = new JFrame();
ImageIcon icon = new ImageIcon("images/icon.jpg");
//JFRAME
window.setSize(800,600);
window.setLocationRelativeTo(null);
window.setTitle("Arkanoid");
window.setUndecorated(true);
window.setIconImage(icon.getImage());
//PANELS
JPanel gamePanel = new JPanel();
gamePanel.setBackground(Color.RED);
gamePanel.setBounds(0, 0, 600, 600);
JPanel menuPanel = new JPanel(new GridLayout(4,1));
menuPanel.setBackground(Color.BLACK);
menuPanel.setBounds(600,0,200,600);
menuPanel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
window.add(gamePanel);
window.add(menuPanel);
//Buttons
JButton closeButton = new JButton("Close Me");
closeButton.addActionListener(e -> System.exit(0));
menuPanel.add(closeButton);
JButton playButton = new JButton("Play");
menuPanel.add(playButton);
JButton Button1 = new JButton("Test1");
menuPanel.add(Button1);
JButton Button2 = new JButton("Test2");
menuPanel.add(Button2);
//Labels
//SHOW
window.setVisible(true);
}
}
Oracle has a helpful tutorial, Creating a GUI With Swing. Skip the Learning Swing with the NetBeans IDE section. Pay close attention to the Laying Out Components Within a Container section.
A JFrame has a default BorderLayout, which I used to place the two JPanels.
I added a main method so I could run the GUI. I commented out the icon code, which doesn't make any sense for an undecorated JFrame. I moved the setLocationRelativeTo method to after the pack method, so the JFrame is actually centered.
Here's the complete runnable code.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Point;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class ExampleLayout {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new ExampleLayout();
}
});
}
Point point = new Point();
public ExampleLayout() {
JFrame window = new JFrame();
// ImageIcon icon = new ImageIcon("images/icon.jpg");
// JFRAME
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setTitle("Arkanoid");
window.setUndecorated(true);
// window.setIconImage(icon.getImage());
// PANELS
JPanel gamePanel = new JPanel();
gamePanel.setBackground(Color.RED);
gamePanel.setPreferredSize(new Dimension(600, 600));
JPanel menuPanel = new JPanel(new GridLayout(0, 1));
menuPanel.setBackground(Color.BLACK);
menuPanel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
menuPanel.setPreferredSize(new Dimension(200, 600));
window.add(gamePanel, BorderLayout.CENTER);
window.add(menuPanel, BorderLayout.EAST);
// Buttons
JButton closeButton = new JButton("Close Me");
closeButton.addActionListener(e -> System.exit(0));
menuPanel.add(closeButton);
JButton playButton = new JButton("Play");
menuPanel.add(playButton);
JButton Button1 = new JButton("Test1");
menuPanel.add(Button1);
JButton Button2 = new JButton("Test2");
menuPanel.add(Button2);
// Labels
// SHOW
window.pack();
window.setLocationRelativeTo(null);
window.setVisible(true);
}
}

How to use layout managers when building a GUI with JFrame and JPanel?

I am trying to build a GUI that looks something like this:
Main idea is to have buttons on the side and a table displaying results.
Here is the code I have so far:
public class GUI {
private JButton usdJpyButton = new JButton("USD/JPY");
private JButton usdGbpButton = new JButton("USD/GBP");
public void mainScreen(){
JFrame frame = new JFrame("Window");
frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
frame.setSize(900,600);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
GridLayout layout = new GridLayout(0,4);
frame.setLayout(layout);
JPanel sidePanel = new JPanel(new GridBagLayout());
sidePanel.setBackground(Color.black);
GridBagConstraints cons = new GridBagConstraints();
cons.insets = new Insets(50,0,0,0);
cons.gridy = 1;
cons.anchor = GridBagConstraints.NORTH;
sidePanel.add(usdJpyButton);
cons.gridy = 2;
sidePanel.add(usdGbpButton);
frame.add(sidePanel);
frame.setVisible(true);
}
The buttons are not aligned and I am not sure which layout manager I should use for the best results. Also each button will have an action listener to a different table so do I need to create seperate JPanels for each table??
I am not sure which layout manager I should use for the best results.
Rarely do you ever use a single layout manager. Generally you will nest panels each using a different layout manager to achieve your desired effect.
Read the section from the Swing tutorial on Layout Managers for working examples of each layout manager.
In this case you would probably need two panels, one for the buttons and one for the panels to display when you click on the button.
So for the buttons you might create a panel using BorderLayout. Then you create a second panel using a GridLayout. You add your buttons to the panel with the GridLayout. Then you add this panel to the PAGE_START of the BorderLayout. Then you add this panel to the LINE_START of the BorderLayout used by the content pane of the frame.
Then for the panel to display each table you would use a CardLayout. Then you create a separate panel for each currency table and add it to the CardLayout. You would add this panel to the CENTER of the BorderLayout of the content pane.
Then in the ActionListener of your buttons you swap the panel in the CardLayout based on the button that was clicked.
Again, the tutorial link I provided has working examples of the BorderLayout, GridLayout and CardLayout.
I would recommend MigLayout manager.
It is not particularly difficult to do it with this manager.
package com.zetcode;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import net.miginfocom.swing.MigLayout;
public class InstaMetrixEx extends JFrame implements ActionListener {
public InstaMetrixEx() {
initUI();
}
private void initUI() {
JButton btn1 = new JButton("Client Overview");
JButton btn2 = new JButton("Seo Reports");
JButton btn3 = new JButton("Social media reports");
JButton btn4 = new JButton("Campaigns");
JButton btn5 = new JButton("Webmaster tools");
JButton btn6 = new JButton("Dashboard");
JButton btn7 = new JButton("Tasks");
JComboBox combo1 = new JComboBox();
combo1.addItem("Bulk actions");
JButton btn8 = new JButton("Submit");
JTable table = new JTable(20, 7);
JScrollPane spane = new JScrollPane(table);
createLayout(btn1, combo1, btn8, btn2, btn3, btn4,
btn5, btn6, btn7, spane );
setTitle("InstaMetrixEx");
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private void createLayout(JComponent... arg) {
setLayout(new MigLayout());
add(arg[0], "sgx");
add(arg[1], "gapx 10lp, split 2");
add(arg[2], "wrap");
add(arg[3], "split 6, aligny top, flowy, sgx");
add(arg[4], "sgx");
add(arg[5], "sgx");
add(arg[6], "sgx");
add(arg[7], "sgx");
add(arg[8], "sgx");
add(arg[9], "gapx 10lp, spanx, wrap, push, grow");
pack();
}
#Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(this, "Button clicked",
"Information", JOptionPane.INFORMATION_MESSAGE);
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
InstaMetrixEx ex = new InstaMetrixEx();
ex.setVisible(true);
});
}
}
Screenshot:

Switch between different sized JPanels

To keep things short, I know how to switch between different JPanels using CardLayout and only using one JFrame but I want to know how, if it is possible, to have different sized JPanels occupying the JFrame. CardLayout uses the largest of the panels but I was wondering if there was any way to suppress that or override so that I could add different JPanels to a JFrame that were different sizes. Here is my code:
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class View {
//Views
private JFrame frame;
private JPanel container;
private JPanel panel1;
private JPanel panel2;
private CardLayout layout;
private final int WIDTH = 1280;
private final int HEIGHT = 720;
public View(){
init();
frame = new JFrame();
frame.setResizable(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.add(container);
frame.pack();
frame.setVisible(true);
}
private void init(){
JButton button = new JButton();
layout = new CardLayout();
container = new JPanel(layout);
panel1 = new JPanel();
panel2 = new JPanel();
panel1.setBackground(Color.WHITE);
panel1.setPreferredSize(new Dimension(500, 500));
panel1.add(button);
panel2.setBackground(Color.BLACK);
panel2.setPreferredSize(new Dimension(500, 500));
container.add(panel1, "panel1");
container.add(panel2, "panel2");
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
layout.show(container, "panel2");
}
});
}
public static void main(String [] args){
View view = new View();
}
}
I put the JButton in the init method because I need it for future use and wanted to make sure now that it could be done.
For a quick solution try two separate JFrames, or resize the frame to the size of the panel on button press.
The only way it seems to work with the cardLayout is by setting the preferred size of the container panel after the button is clicked.
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
layout.show(container, "panel2");
container.setPreferredSize(new Dimension(100, 100));
frame.pack();
}
});
It's not a very great solution but it's pretty simple.
If a login window is what you need I would definitely recommend using 2 frames, just seems more logical.
You can't use a CardLayout. So you need to manage the visible panel yourself.
The logic would be something like:
JPanel content = (JPanel)frame.getContentPane();
content.removeAll();
content.add( theOtherPanel );
frame.pack();
So the idea is you only have one panel added to the content pane at a time and you need to swap it by doing the remove and add before you pack the frame so the frame is displayed at the size of each panel.
The question is why do you want to do this? Users don't like to see the size of the frame continually changing.

BorderLayout(); buttons not showing up

Hey guys my buttons and textarea will not display on JFrame when compiled, i have tried everything and searched this site but no luck. Any help would be greatly appreciated. Due to them not letting me post without more detail i am just adding this part so i can hit the submit button.
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class DataManager extends JFrame {
private String students[] = {"John Smith","Ken Hanson","Michael Li","John Andersen","Fiona Harris","Angela Lim","Bob London","Sydney Shield","Tina Gillard",
"Ross Brinns","Scott Cairns","Grant Peterson","David Power","Joshua Kane","Alan Newton","Frady Morgan","Quinn Perth"};
private int english[] = {80,52,71,61,39,62,31,46,60,26,77,40,58,38,94,90,97};
private int maths[] = {60,45,77,90,45,55,66,87,31,42,65,55,80,71,51,55,95};
private int total[];
private JButton sortNameButton;
private JButton sortTotalButton;
private JTextField searchTextField;
private JButton statisticsButton;
private JButton exitButton;
private JTextArea infoTextArea;
private JPanel jPan;
public DataManager() {
super("Data Manager ");
jPan = new JPanel();
sortNameButton = new JButton("Sort By Name");
sortTotalButton = new JButton("Sort By Total");
searchTextField = new JTextField("Search");
statisticsButton = new JButton("Statistics");
exitButton = new JButton("Exit");
infoTextArea = new JTextArea();
setLayout(new BorderLayout());
jPan.add(sortNameButton, BorderLayout.NORTH);
jPan.add(sortTotalButton, BorderLayout.NORTH);
jPan.add(searchTextField, BorderLayout.NORTH);
jPan.add(statisticsButton, BorderLayout.NORTH);
jPan.add(exitButton, BorderLayout.NORTH);
jPan.add(infoTextArea, BorderLayout.CENTER);
}
public static void main(String[] args) {
DataManager frame = new DataManager();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800,600);
frame.setVisible(true);
} // End of main method.
} // End of DataManager class
You add your JButtons to the jPan JPanel but never add the jPan to anything -- it must be added to your JFrame, to this to be seen.
jPan.add(sortNameButton);
jPan.add(sortTotalButton);
jPan.add(searchTextField);
jPan.add(statisticsButton);
jPan.add(exitButton);
jPan.add(infoTextArea);
add(jPan); // don't forget this! ************
Note other problems:
You set the JFrame's layout to BorderLayout -- it's already using BorderLayout
You add components to your jPan JPanel with BorderLayout constants, but it's not using a BorderLayout.
If it were, many buttons would not be seen since many are added to the same BorderLayout position and will cover the previous component added there.
In other words, read the tutorials as you're making wrong assumptions.
Better would be something like:
// setLayout(new BorderLayout());
jPan.setLayout(new BorderLayout());
JPanel northPanel = new JPanel(); // **** to hold buttons
northPanel.add(sortNameButton);
northPanel.add(sortTotalButton);
northPanel.add(searchTextField);
northPanel.add(statisticsButton);
northPanel.add(exitButton);
jPan.add(northPanel, BorderLayout.PAGE_START);
jPan.add(infoTextArea, BorderLayout.CENTER);

how to repaint a jpanel with button click and also switch between 2 panels

I am trying to repaint a panel with a button click, but its not happening. i tried using many methods like setEnabled(true), setVisible(true), repaint(), but none of these are working. And also i want to switch between two panels, like, if i click the "Total Vote" button, one panel should display, when i click on "Departmentwise Vote", another panel should display in the same place.
please help. Thanks in advance.
here i m providing the code :
public class RSummary extends JFrame implements ActionListener
{
JFrame rframe = new JFrame();
JPanel panel1, panel2, panel3, panel4, panel5;
JTable table;
JScrollPane scroll;
JSplitPane splitPane;
JButton butx;
public RSummary()
{
rframe.setSize(550,300);
rframe.setLocationRelativeTo(null);
setSize(550,300);
rframe.setTitle("Summary Report");
/* Total Vote & Department wise vote buttons */
panel1= new JPanel();
JButton but1 = new JButton("TOTAL VOTE");
JButton but2 = new JButton("DEPTARTMENT WISE VOTE");
but1.addActionListener(this);
panel1.add(but1);
panel1.add(but2);
/* Report contents according to button */
panel2 = new JPanel();
String[] columnNames = {"Name", "Department","Phno","LUID","Select"};
DefaultTableModel model1 = new DefaultTableModel();
model1.setColumnIdentifiers(columnNames);
table = new JTable();
table.setModel(model1);
table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
table.setFillsViewportHeight(true);
scroll = new JScrollPane(table);
panel2.add(scroll);
panel2.setVisible(false);
/* close button */
panel3 = new JPanel();
JButton but4= new JButton("CLOSE");
but4.addActionListener(this);
panel3.add(but4);
/* Page heading */
panel4 = new JPanel();
JLabel lab =new JLabel("VOTE SUMMARY REPORT");
panel4.add(lab);
/* dummy panel */
panel5 = new JPanel();
JButton butx = new JButton("Hello butx");
panel5.add(butx);
splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, panel1, panel2);
rframe.add(splitPane);
rframe.add(panel3,BorderLayout.SOUTH);
rframe.add(panel4,BorderLayout.NORTH);
rframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
rframe.setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
String action=ae.getActionCommand();
if(action == "CLOSE")
{
rframe.setVisible(false);
}
if(action == "TOTAL VOTE")
{
panel2.setVisible(true); //this code is not working, i want the solution here.
}
}
public static void main(String args[]) throws ClassNotFoundException, SQLException
{
new RSummary();
}
}
The first problem is you're comparing the object references of your String values and not there contents...
if(action == "TOTAL VOTE")
Because of the way Strings are managed by Java, it is very unlikely that these will ever be equal.
String comparison in Java is done using String#equals
if ("TOTAL VOTE".equals(action))
Your second problem may be related to the fact the setVisible may not be invalidating the container hierarchy, which would tell the layout framework that the containers need to be updated.
You have two solutions, the first would be to call revalidate on the parent container, the second, and better, would be to use a CardLayout, which is designed to just what you are trying to do...
Take a look at How to use Card Layout for more details
Update after running the Code
You have a series of compounding issues...
You never set the action command of any of the buttons, so when the actionPerformed event is raised, the action is actually null
RSummary extends from JFrame, yet you create a second JFrame called rframe. This is very confusing and could potentially lead to more problems if you're not dealing with the correct frame.
Start by setting the actionCommand property of your buttons...
JButton but1 = new JButton("TOTAL VOTE");
but1.setActionCommand("TOTAL VOTE");
Then remove extends JFrame from RSummary, as it is doing nothing but adding to the confusion and adds no benefit
Setting a frame invisible will not "close" it. Instead use JFrame#dispose
Finally, making a component visible while it's part of split pane won't resize the split divider. Once you've made the corrections, you will need to manually move the divider.
You may consider placing a empty panel into the split pane and using a CardLayout, add the other components to it, switching them in and out using the CardLayout
Updated with modified code
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class RSummary extends JFrame implements ActionListener {
JFrame rframe = new JFrame();
JPanel panel1, panel2, panel3, panel4, panel5;
JTable table;
JScrollPane scroll;
JSplitPane splitPane;
JButton butx;
public RSummary() {
rframe.setSize(550, 300);
rframe.setLocationRelativeTo(null);
setSize(550, 300);
rframe.setTitle("Summary Report");
/* Total Vote & Department wise vote buttons */
panel1 = new JPanel();
JButton but1 = new JButton("TOTAL VOTE");
but1.setActionCommand("TOTAL VOTE");
JButton but2 = new JButton("DEPTARTMENT WISE VOTE");
but1.addActionListener(this);
panel1.add(but1);
panel1.add(but2);
/* Report contents according to button */
panel2 = new JPanel();
String[] columnNames = {"Name", "Department", "Phno", "LUID", "Select"};
DefaultTableModel model1 = new DefaultTableModel();
model1.setColumnIdentifiers(columnNames);
table = new JTable();
table.setModel(model1);
table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
table.setFillsViewportHeight(true);
scroll = new JScrollPane(table);
panel2.add(scroll);
panel2.setVisible(false);
/* close button */
panel3 = new JPanel();
JButton but4 = new JButton("CLOSE");
but4.addActionListener(this);
panel3.add(but4);
/* Page heading */
panel4 = new JPanel();
JLabel lab = new JLabel("VOTE SUMMARY REPORT");
panel4.add(lab);
/* dummy panel */
panel5 = new JPanel();
JButton butx = new JButton("Hello butx");
panel5.add(butx);
splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, panel1, panel2);
rframe.add(splitPane);
rframe.add(panel3, BorderLayout.SOUTH);
rframe.add(panel4, BorderLayout.NORTH);
rframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
rframe.setVisible(true);
}
public void actionPerformed(ActionEvent ae) {
String action = ae.getActionCommand();
if ("CLOSE".equals(action)) {
rframe.dispose();
}
if ("TOTAL VOTE".equals(action)) {
panel2.setVisible(true); //this code is not working, i want the solution here.
panel2.getParent().revalidate();
}
}
public static void main(String args[]) throws ClassNotFoundException, SQLException {
new RSummary();
}
}
To switch panels you can do:
rframe.remove (panelToRemove);
rframe.add(panelToShow);
Is this the answer to your question or why do you want to repaint?
Geosearchef
UPDATE:
To summarize:
MadProgrammer is right, you have to replace if(action == "TOTAL VOTE") by if(action.equals("TOTAL VOTE")) (same for CLOSE)
and you have to add an actionCommand:
JButton but1 = new JButton("TOTAL VOTE");
but1.addActionListener(this);
but1.setActionCommand("TOTAL VOTE");
To change panels:
if (action.equals("TOTAL VOTE")) {
try{rframe.remove(panel1;)}catch(Exception e){}//clear window, maybe there is a method
try{rframe.remove(panel2;)}catch(Exception e){}
try{rframe.remove(panel3;)}catch(Exception e){}
rframe.add(panel2);//the panel you want to show
}
Of course, you have to get references to the panels to actionPerformed(ActionEvent ae).

Categories