As i am new to java swing, i am finding a little difficulty in integrating the JFileChooser with JList. My goal is to select a file from the dialog-box(JFileChooser) and click 'add' so that it gets added to the JList automatically and the same mechanism with 'remove'. I tried going through a few tutorials and a few hints but it dint work. It would be really great if any of you could help me with this step.
Thanks in advance..!!
package examples;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JSplitPane;
//import javax.swing.SwingConstants;
class SplitPane extends JFrame
{
private static final long serialVersionUID = 1L;
private JSplitPane splitPaneV;
private JSplitPane splitPaneH;
private JLayeredPane panel1;
private JPanel panel2;
private JPanel panel3;
private JButton add;
private JButton remove;
private JScrollBar scrollBar;
private JList list;
public SplitPane()
{
setTitle("AdditionalLoaderInformation");
setBackground(Color.blue);
JPanel topPanel = new JPanel();
topPanel.setLayout(new BorderLayout());
topPanel.setPreferredSize(new Dimension(700, 500));
getContentPane().add(topPanel);
// Create the panels
createPanel1();
createPanel2();
createPanel3();
// Create a splitter pane
splitPaneV = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
topPanel.add(splitPaneV, BorderLayout.CENTER);
splitPaneH = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
splitPaneH.setLeftComponent(panel1);
splitPaneH.setRightComponent(panel2);
splitPaneV.setLeftComponent(splitPaneH);
splitPaneV.setRightComponent(panel3);
scrollBar = new JScrollBar();
scrollBar.setOrientation(JScrollBar.HORIZONTAL);
panel3.add(scrollBar, BorderLayout.SOUTH);
list = new JList();
panel3.add(list, BorderLayout.CENTER);
}
public void createPanel1()
{
panel1 = new JLayeredPane();
panel1.setLayout(new BorderLayout());
}
public void createPanel2()
{
panel2 = new JPanel();
add = new JButton("ADD");
final JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
add.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
************************************
}
});
panel2.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
panel2.add(add);
remove = new JButton("REMOVE");
remove.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
removeActionPerformed(e);
}
private void removeActionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
});
panel2.add(remove);
}
public void createPanel3()
{
panel3 = new JPanel();
panel3.setLayout(new BorderLayout());
panel3.setPreferredSize(new Dimension(400, 100));
panel3.setMinimumSize(new Dimension(100, 50));
final JFileChooser fileChooser = new JFileChooser();
fileChooser.setMultiSelectionEnabled(true);
//fileChooser.showOpenDialog(fileChooser);
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
fileChooser .setDialogTitle("OPEN");
panel3.add(fileChooser, BorderLayout.NORTH);
//fileChooser.addActionListener(new ActionListener()
// {
// public void actionPerformed(ActionEvent e)
//{
// }
//});
}
public static void main(String args[]) {
// Create an instance of the test application
SplitPane mainFrame = new SplitPane();
mainFrame.pack();
mainFrame.setVisible(true);
}
}
When you get a new file name in your chooser's action listener, shown here, add it to (or remove it from) the list's models, as shown in this example.
Addendum: To display the file's content in the JList, you'll need to create a suitable renderer using one of the text components.
Read the JFileChooser tutorial
If the above step is not sufficient, take a look at the class javadoc of JFileChooser and note the APPROVE_OPTION and the getSelectedFile method. This should allow you to obtain the file
Read the JList tutorial
If the above step is not sufficient, take a look at the available API of JList and ListModel, and more in particular the default implementation DefaultListModel which contains add and remove methods
Related
My Java code below is trying to import a photo to place on new JLabel(new ImageIcon("")); using File Chooser. I have no experience with file Chooser but I want to select a image and then have the image place on the jLabel. I don't know what to put in action event e that will help me achieve this goal.
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class back extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
public int act = 0;
public back(){
setTitle("Question");
JPanel Panel1 = new JPanel();
Panel1.setLayout(new BorderLayout());
JPanel Panel2 = new JPanel();
Panel2.setLayout(new GridLayout(3,1));
JLabel myButton1 = new JLabel(new ImageIcon(""));
JButton myButton2 = new JButton("2:Select Image");
myButton2.addActionListener(this);
Panel2.add(myButton1);
Panel2.add(myButton2);
Panel1.add(Panel2,BorderLayout.CENTER);
add(Panel1);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 500);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
SomeImageType image = findImage();
displayImage(image, Panel1);
}
public static void main(String[] args) {
new back();
}
}
I'm trying to figure out how to position my buttons in the center right position. I added what Ive done so far and I'll add a drawing of how I want it to be.
I'm trying to understand how to determine the position I want in Swing, can't really understand the advantages of each layout.
My code so far:
package Game;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.Timer;
public class MainWindow extends JFrame implements ActionListener {
private JButton exit;
private JButton start_Game;
private ImageIcon puzzleBackground;
// private JLabel back_Label;
// private GridBagConstraints grid = new GridBagConstraints();
private JPanel menu;
public MainWindow()
{
super("Welcome");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setSize(450,300);
setLocationRelativeTo(null);
this.setLayout(new FlowLayout(FlowLayout.RIGHT));
menu = new JPanel();
menu.setLayout(new BorderLayout());
//setResizable(false);
//===== Background =====
puzzleBackground = new ImageIcon("MyBackground.jpg");
setContentPane(new JLabel(puzzleBackground));
exit = new JButton("Exit");
menu.add(exit, BorderLayout.CENTER);
exit.addActionListener(this);
start_Game = new JButton("Start to play");
menu.add(start_Game, BorderLayout.SOUTH);
exit.addActionListener(this);
start_Game.addActionListener(this);
//
// back_Label = new JLabel(puzzleBackground);
// back_Label.setLayout(new BorderLayout());
//===== Buttons =====
// back_Label.add(exit,BorderLayout.CENTER);
//
// back_Label.add(start_Game,BorderLayout.EAST);
//
add(menu);
setVisible(true);
}
public static void main(String args[])
{
MainWindow a = new MainWindow();
}
#Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == exit)
{
System.exit(0);
}
else
{
//open start up window.
}
}
}
A better way to add a BG image is to use a custom painted JPanel. Then set the layout of the panel and add other panels or components to it. Note that here the buttons are not appearing largely because they are being added to a JLabel.
Here is an alternative that works along the same lines, with the red panel being the panel which custom paints the background image and the menu panel being set to transparent (look for the opaque method).
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.border.*;
public class MainWindow extends JFrame {
private JPanel menu;
private JPanel contentPane = new JPanel(new BorderLayout(4,4));
public MainWindow() {
super("Welcome");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
//setSize(450, 300); // use pack() instead
setContentPane(contentPane);
contentPane.setBorder(new EmptyBorder(8,8,8,8));
contentPane.setBackground(Color.RED);
contentPane.add(new JLabel(new ImageIcon(
new BufferedImage(400,200,BufferedImage.TYPE_INT_RGB))));
menu = new JPanel(new GridLayout(0,1,10,10));
menu.add(new JButton("Exit"), BorderLayout.CENTER);
menu.add(new JButton("Start to play"), BorderLayout.SOUTH);
JPanel menuCenterPanel = new JPanel(new GridBagLayout());
menuCenterPanel.add(menu);
add(menuCenterPanel, BorderLayout.LINE_END);
pack();
setLocationRelativeTo(null); // do AFTER pack()
setMinimumSize(getSize());
setVisible(true);
}
public static void main(String args[]) {
MainWindow a = new MainWindow();
}
}
So, your basic problem boils down the following lines...
this.setLayout(new BorderLayout());
//...
//===== Background =====
puzzleBackground = new ImageIcon("MyBackground.jpg");
setContentPane(new JLabel(puzzleBackground));
Can you tell me what the layout manager in use actually is now? Wrong. The layout manager is now null, because JLabel doesn't actually have a default layout manager.
So, the "simple" answer would be to move the setLayout call to below the setContentPane call, but this would be a short sighted answer, as JLabel calculates it's preferred based on the icon and text properties only, not it's contents of child components.
A better solution would be to do something demonstrated in How to set a background picture in JPanel (see the second example)
This means that if the image is smaller then the required space, the components will disappear off the screen.
I went through and cleaned up the code slightly, only with the intention of getting the layout to work
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainWindow extends JFrame implements ActionListener {
private JButton exit;
private JButton start_Game;
private JPanel menu;
public MainWindow() {
super("Welcome");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setLocationRelativeTo(null);
this.setLayout(new BorderLayout());
menu = new JPanel();
menu.setLayout(new GridBagLayout());
exit = new JButton("Exit");
exit.addActionListener(this);
start_Game = new JButton("Start to play");
exit.addActionListener(this);
start_Game.addActionListener(this);
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 1;
gbc.fill = gbc.HORIZONTAL;
gbc.gridwidth = GridBagConstraints.REMAINDER;
menu.add(exit, gbc);
menu.add(start_Game, gbc);
// This is just a filler, it can be removed, but it helps prove the point
add(new JPanel() {
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
});
add(menu, BorderLayout.EAST);
pack();
setVisible(true);
}
public static void main(String args[]) {
MainWindow a = new MainWindow();
}
#Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == exit) {
System.exit(0);
} else {
//open start up window.
}
}
}
I'd also like to point out that extending directly from JFrame is also short sighted, it's locking you into a single use container and you're not actually adding any new functionality to the class.
Example of better structure...
The following is a simple example of a possibly better structure. It's missing the concept of a "controller", which controls stuff and "model" which maintains the state information which is used by the UI to display "stuff", but gives a starting point
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Main {
public static void main(String args[]) {
new Main();
}
public Main() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Welcome");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new MainPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class MainPane extends JPanel {
public MainPane() {
setLayout(new BorderLayout());
// This is just a filler, it can be removed, but it helps prove the point
add(new JPanel() {
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
});
add(new MenuPane(), BorderLayout.EAST);
}
}
public class MenuPane extends JPanel {
private JButton exit;
private JButton start_Game;
private JPanel menu;
public MenuPane() {
menu = new JPanel();
menu.setLayout(new GridBagLayout());
ActionHandler actionHandler = new ActionHandler();
exit = new JButton("Exit");
exit.addActionListener(actionHandler);
start_Game = new JButton("Start to play");
start_Game.addActionListener(actionHandler);
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 1;
gbc.fill = gbc.HORIZONTAL;
gbc.gridwidth = GridBagConstraints.REMAINDER;
menu.add(exit, gbc);
menu.add(start_Game, gbc);
}
public class ActionHandler implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == exit) {
System.exit(0);
} else {
//open start up window.
// This should be used to notifiy a controller class
// that some new action needs to take place, the controller
// is then responsible for making it happen
}
}
}
}
}
Doing UI in Java is not advised, but ignoring that.
You get (calculate) the height and width of the screen. Then start drawing buttons depending on that. Drawing a button on screens 50% of pixel value width and 50% of pixel value of height will center the button.
Simply crate buttons with variable location that is calculated from main screen px size and place them where ever you want.
so what i want to do is update a JTable when a button is pressed. Im using a DefaultTableModel as a source.
Table class:
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import java.awt.Dimension;
public class TableDemo extends JPanel {
String[] columnNames = {"Tipo","Cantidad"};
DefaultTableModel dtm = new DefaultTableModel(null,columnNames);
final JTable table = new JTable(dtm);
public TableDemo() {
table.setPreferredScrollableViewportSize(new Dimension(500, 700));
table.setFillsViewportHeight(true);
JScrollPane scrollPane = new JScrollPane(table);
add(scrollPane);
}
public void addRow(Object [] row)
{
dtm.addRow(row);
}
}
the form class:
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextField;
public class MyForm extends JFrame {
JPanel panel;
JLabel label;
JTextField txtName;
JTextField txtSurname;
JButton btnAccept;
TableDemo tb = new TableDemo();
public MyForm(){
initControls();
}
private void initControls() {
setTitle("Form Example");
setSize(600, 400);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setLocationRelativeTo(null);
panel = new JPanel();
panel.setLayout(null);
label = new JLabel("Welcome to Swing");
label.setBounds(50, 10, 200, 30);
txtName = new JTextField();
txtName.setBounds(50, 50, 200, 30);
txtSurname = new JTextField();
txtSurname.setBounds(270, 50, 200, 30);
btnAccept = new JButton("Add");
btnAccept.setBounds(120, 100, 150, 30);
onAcceptClick();
TableDemo tablePanel = new TableDemo();
panel.add(label);
panel.add(txtName);
panel.add(txtSurname);
panel.add(btnAccept);
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
tablePanel,panel );
splitPane.setDividerLocation(205);
splitPane.setEnabled(false);
getContentPane().add(splitPane);
}
private void onAcceptClick() {
btnAccept.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
tb.addRow(new Object[]{"taza",txtName.getText()});
}
});
public static void main(String[] args) {
//Initilizer init = new Initilizer();
Runnable init = new Runnable() {
#Override
public void run() {
MyForm form = new MyForm();
form.setVisible(true);
}
};
SwingUtilities.invokeLater(init);
}
}
As you can see i call the addRow function but in never updates in the JTable, what am i missing? Thnx
You're creating two TableDemo objects -- one you add to the GUI, tablePanel, the other you add a row to, tb;
// ...
TableDemo tb = new TableDemo(); // TableDemo #1. Never displayed
private void initControls() {
// ...
TableDemo tablePanel = new TableDemo(); // TableDemo #2
// ...
// TableDemo #2 is here added to the GUI
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, tablePanel,panel );
// ...
}
btnAccept.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
// here you add a row to the non-displayed TableDemo #1
tb.addRow(new Object[]{"taza",txtName.getText()});
}
});
Solution: Don't do this (obviously)! Use only one TableDemo instance, display it and make changes to it. So get rid of the tablePanel variable and only use the tb variable.
Other problems:
You're using null layouts. While null layouts and setBounds() might seem to Swing newbies like the easiest and best way to create complex GUI's, the more Swing GUI'S you create the more serious difficulties you will run into when using them. They won't resize your components when the GUI resizes, they are a royal witch to enhance or maintain, they fail completely when placed in scrollpanes, they look gawd-awful when viewed on all platforms or screen resolutions that are different from the original one.
I have a MainPanel which uses the Gridlayout. Consequently I have created four JPanel classes for the: NORTH, EAST, CENTER and EAST layouts respectively. I then add all four to my MainPanel.
However, on my WEST panel I use another grid layout to store JButtons and JTextFields. I want to constantly update my JTextFields as they display a value (that changes when a button on another panel is clicked). How do I allow that value to be changed when the JFrame is running?
I tried using paintComponent, but it keeps on adding multiple copies of the same JTextField after each other, as I add it in my paintComponent method. If I remove the add method the values won't update.
Action works well to encapsulate such functionality. In the example below, a number of text fields listen for an ActionEvent received from a single Update button. The common UpdateHandler is derived from AbstractAction.
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
/** #see http://stackoverflow.com/a/14947144/230513 */
public class Test {
private JButton button = new JButton("Update");
private void display() {
JFrame f = new JFrame("Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(createPanel(button), BorderLayout.NORTH);
f.add(createPanel(button), BorderLayout.WEST);
f.add(createPanel(button), BorderLayout.EAST);
f.add(createPanel(button), BorderLayout.SOUTH);
JPanel p = new JPanel();
p.add(button);
f.add(p, BorderLayout.CENTER);
f.getRootPane().setDefaultButton(button);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
private static JPanel createPanel(JButton b) {
JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER));
final JTextField text = new JTextField();
b.addActionListener(new UpdateHandler(text));
panel.add(text);
return panel;
}
private static class UpdateHandler extends AbstractAction {
private JTextField text;
private DateFormat df = new SimpleDateFormat("HH:mm:ss.SSS");
public UpdateHandler(JTextField t) {
super("update");
t.setText(df.format(new Date()));
this.text = t;
}
#Override
public void actionPerformed(ActionEvent e) {
text.setText(df.format(new Date()));
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new Test().display();
}
});
}
}
I'm fairly new to GUI. I'm trying to make it so that depending on which radio button is selected, a JLabel changes its value. For example, if "id" is selected, it'll display "http://steamcommunity.com/id/" and if "profile" is selected, it'll display "http://steamcommunity.com/profiles/". I have some code up and running and it's nearly complete:
package sgt;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class RadioButtonPrompt extends JPanel
implements ActionListener {
private static final long serialVersionUID = 1L;
static String idString = "ID";
static String profileString ="Profile";
static String type = idString;
public RadioButtonPrompt() {
super(new BorderLayout());
// Create radio buttons.
JRadioButton idButton = new JRadioButton(idString, true);
idButton.setMnemonic(KeyEvent.VK_I);
idButton.setActionCommand(idString);
JRadioButton profileButton = new JRadioButton(profileString);
profileButton.setMnemonic(KeyEvent.VK_P);
profileButton.setActionCommand(profileString);
// Group radio buttons.
ButtonGroup group = new ButtonGroup();
group.add(idButton);
group.add(profileButton);
idButton.addActionListener(this);
profileButton.addActionListener(this);
JPanel radioPanel = new JPanel(new GridLayout(0, 1));
radioPanel.add(idButton);
radioPanel.add(profileButton);
JPanel textPanel = new JPanel ();
JLabel URL = new JLabel(setJLabelValue());
JTextField text = new JTextField("sampletextfield");
text.setPreferredSize(new Dimension(100, 20));
textPanel.add(URL);
textPanel.add(text);
JPanel buttonPanel = new JPanel(new GridLayout(1, 0));
JButton submit = new JButton("Submit");
submit.setMnemonic(KeyEvent.VK_S);
buttonPanel.add(submit);
add(radioPanel, BorderLayout.LINE_START);
add(textPanel, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.PAGE_END);
setBorder(BorderFactory.createCompoundBorder());
}
private String setJLabelValue() {
if (type.equals("ID")) {
return "http://steamcommunity.com/id/";
}
return "http://steamcommunity.com/profiles/";
}
public void actionPerformed(ActionEvent e) {
// Returns either "Profile" or "ID"
type = ((JRadioButton)e.getSource()).getText();
System.out.println(type);
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("Steam Game Tracker");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JComponent newContentPane = new RadioButtonPrompt();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
// Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
Take a look at this SO thread.
in actionPerformed() you need to textpanel.setText() to whatever you want based on which button was clicked. I'm guessing at the method name, haven't done any UI stuff with Java for a while.