When I run my code the JList shows up but it's really small, and when I try to change its size it won't change size. What am I doing that's wrong? A lot of the GUI formatting is new to me. Thanks.
public class Panel extends JPanel
{
//Fields
private Dimension dimensions;
private JButton button1;
private JList list;
private JScrollPane listScroller;
private String title;
private DefaultListModel model;
/**
* Constructor
*/
public Panel()
{
dimensions = new Dimension(400, 300);
setPreferredSize(dimensions);
setBackground(Color.WHITE);
setLayout(null); //Lets me lay my components out freely
button1 = new JButton("New Set");
button1.setBounds(new Rectangle(new Point(150, 250), button1.getPreferredSize()));
button1.addActionListener(new ButtonListener());
add(button1);
String[] titleArray ={title};
model = new DefaultListModel();
for(String title :titleArray)
{
model.addElement(title);
}
list = new JList(model);
list.setBounds(new Rectangle(new Point(200,200), list.getPreferredSize()));
add(list);
listScroller = new JScrollPane(list);
listScroller.setBounds(new Rectangle(new Point(200,200), listScroller.getPreferredSize()));
add(listScroller);
}
}
Related
I am using JCombobox and just below that there is a panel which contains JTextFields. Whenever i click on the dropdown (JCombobox) some portion of JTextField disappears itself. I don't know what to do. I am unable to post a pic of the problem here because i am a new user and my reputation is below 10.
public class MainClass {
public static void main(String args[]){
Form form = new Form();
int width = 400;
int height = 400;
form.setSize(width, height);
form.setVisible(true);
form.setTitle("Create Network");
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
form.setLocation((screenSize.width-width)/2, (screenSize.height-height)/2);
}
}
This is the form class
public class Form extends JFrame {
private JLabel ipAddress, networkTopo, numNodes;
private JTextField nodes;
private JButton addIp;
private JButton removeIp;
private JButton next, back;
private JPanel jPanel, jPanel1, jPanel2, commonPanel;
private JComboBox<String> dropDown;
private String[] topologies = { "Grid", "Diagnol Grid", "Bus", "Ring",
"Star" };
public Form() {
setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
setResizable(false);
this.getContentPane().setBackground(Color.WHITE);
// add(Box.createRigidArea(new Dimension(0,10)));
GridLayout commonPanelLayout = new GridLayout(0, 2);
commonPanelLayout.setVgap(10);
commonPanel = new JPanel(commonPanelLayout);
commonPanel.setVisible(true);
commonPanel.setAlignmentX(commonPanel.LEFT_ALIGNMENT);
commonPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
commonPanel.setBackground(Color.white);
numNodes = new JLabel("Number of Nodes");
commonPanel.add(numNodes);
nodes = new JTextField(20);
commonPanel.add(nodes);
networkTopo = new JLabel("Network Topology");
commonPanel.add(networkTopo);
dropDown = new JComboBox<String>(topologies);
dropDown.setBackground(Color.WHITE);
commonPanel.add(dropDown);
add(commonPanel);
add(Box.createRigidArea(new Dimension(0, 10)));
ipAddress = new JLabel("IP Addresses");
ipAddress.setAlignmentX(ipAddress.LEFT_ALIGNMENT);
ipAddress.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 0));
add(ipAddress);
add(Box.createRigidArea(new Dimension(0, 10)));
jPanel = new JPanel(new FlowLayout());
jPanel.setVisible(true);
jPanel.setAlignmentX(jPanel.LEFT_ALIGNMENT);
jPanel.setBackground(Color.WHITE);
// jPanel1.setAutoscrolls(true);
add(jPanel);
GridLayout layout = new GridLayout(0, 1);
jPanel1 = new JPanel();
layout.setVgap(10);
// jPanel1.setBackground(Color.WHITE);
jPanel1.setVisible(true);
JScrollPane scroll = new JScrollPane();
scroll.setViewportView(jPanel1);
scroll.setAutoscrolls(true);
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scroll.setPreferredSize(new Dimension(300, 150));
jPanel1.setPreferredSize(new Dimension(scroll.getPreferredSize().width,
Integer.MAX_VALUE));
jPanel.add(scroll);
jPanel2 = new JPanel(new GridLayout(0, 1, 10, 10));
jPanel2.setBackground(Color.WHITE);
jPanel2.setVisible(true);
jPanel.add(jPanel2);
addIp = new JButton("Add");
jPanel2.add(addIp);
removeIp = new JButton("Remove");
jPanel2.add(removeIp);
JPanel savePanel = new JPanel(new FlowLayout());
savePanel.setVisible(true);
savePanel.setAlignmentX(savePanel.LEFT_ALIGNMENT);
savePanel.setBackground(Color.white);
back = new JButton("Back");
back.setAlignmentX(next.LEFT_ALIGNMENT);
back.setEnabled(false);
savePanel.add(back);
next = new JButton("Next");
next.setAlignmentX(next.LEFT_ALIGNMENT);
savePanel.add(next);
add(savePanel);
AddIPEvent addIPEvent = new AddIPEvent();
addIp.addActionListener(addIPEvent);
RemoveIPEvent removeIPEvent = new RemoveIPEvent();
removeIp.addActionListener(removeIPEvent);
}
public class AddIPEvent implements ActionListener {
public void actionPerformed(ActionEvent e) {
JPanel subPanel = new JPanel(new FlowLayout());
// subPanel.setBackground(Color.WHITE);
subPanel.setVisible(true);
JCheckBox jCheckBox = new JCheckBox();
// jCheckBox.setBackground(Color.WHITE);
subPanel.add(jCheckBox);
JTextField ip = new JTextField(12);
subPanel.add(ip);
JTextField nodesInIp = new JTextField(6);
nodesInIp.setVisible(false);
subPanel.add(nodesInIp);
jPanel1.add(subPanel);
jPanel1.repaint();
jPanel1.revalidate();
}
}
public class RemoveIPEvent implements ActionListener {
public void removeIP(Container container) {
for (Component c : container.getComponents()) {
if (c instanceof JCheckBox) {
if (((JCheckBox) c).isSelected()) {
jPanel1.remove(container);
jPanel.revalidate();
jPanel1.repaint();
}
} else if (c instanceof Container) {
removeIP((Container) c);
}
}
}
public void actionPerformed(ActionEvent e) {
removeIP(jPanel1);
}
}
}
After Clicking on the Add button and then clicking on the JComboBox will make portion of JTextField dissapear. Could someone pls point out the mistake?
Whenever i click on the dropdown (JCombobox) some portion of JTextField disappears itself.
//jPanel1.setPreferredSize(new Dimension(scroll.getPreferredSize().width, Integer.MAX_VALUE));
Don't set the preferred size of components. The layout manager will determine the preferred size as components are added/removed. Then scrollbars will appear as required.
jPanel1.repaint();
jPanel1.revalidate();
The order should be:
jPanel1.revalidate();
jPanel1.repaint();
The revalidate() first invokes the layout manager before it is repainted.
//form.setSize(width, height);
form.pack();
Again, don't try to set the size. Let the layout managers do their jobs. The pack() will size the frame based on the sizes of the components added to the frame.
It was working before I added the 3D array of textfields, then nothing showed from all I have created. After removing them, still nothing shows up. How do I add a 3D array of JTextFields without ruining the whole layout?
I initialized them first:
int numOfPuzzles;
int puzzleSize;
JTabbedPane tabbedPane;
JPanel[] southPanel;
JPanel[] centerPanel;
JPanel[] sudokuPanel;
JPanel[] buttonsPanel;
JButton[] nextButton;
JButton[] prevButton;
ArrayList<int[][]> sudoku;
JTextField[][][] textFields;
public SudokuSolverUI(ArrayList<int[][]> sudoku){
this.sudoku = sudoku;
this.numOfPuzzles = numOfPuzzles;
this.puzzleSize = puzzleSize;
this.setTitle("Sudoku XY");
this.setVisible(true);
this.setSize(900, 700);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setComponents();
}
then at public void setComponents I instantiated the necessary components, added them to the panel, then:
public void setComponents(){
this.tabbedPane = new JTabbedPane();
centerPanel = new JPanel[this.numOfPuzzles];
sudokuPanel = new JPanel[this.numOfPuzzles];
buttonsPanel = new JPanel[this.numOfPuzzles];
southPanel = new JPanel[this.numOfPuzzles];
nextButton = new JButton[this.numOfPuzzles];
prevButton = new JButton[this.numOfPuzzles];
for(int i=0;i<this.numOfPuzzles;i++){
nextButton[i] = new JButton("Next");
nextButton[i].addActionListener(this);
prevButton[i] = new JButton("Prev");
prevButton[i].addActionListener(this);
centerPanel[i] = new JPanel();
sudokuPanel[i] = new JPanel();
buttonsPanel[i] = new JPanel();
southPanel[i] = new JPanel();
for(int j=0;j<this.sudoku.get(i).length;j++){
for(int k=0;k<this.sudoku.get(i).length;k++){
textFields[i][j][k] tf = new JTextField();
sudokuPanel[i].add(tf);
}
}
southPanel[i].setLayout(new FlowLayout());
southPanel[i].add(prevButton[i]);
southPanel[i].add(nextButton[i]);
centerPanel[i].setLayout(new BorderLayout());
centerPanel[i].add(southPanel[i], BorderLayout.SOUTH);
centerPanel[i].add(sudokuPanel[i], BorderLayout.CENTER);
centerPanel[i].add(buttonsPanel[i], BorderLayout.EAST);
centerPanel[i].setVisible(true);
centerPanel[i].validate();
tabbedPane.addTab("Puzzle #"+(i+1), centerPanel[i]);
this.getContentPane().add(tabbedPane);
}
}
nothing shows up. Please help.
If this is all you'll get a NullPointerException which you should be able to see at the console. You did not initialize numOfPuzzle but you use it as index in setComponents.
So I am trying to get a JFrame to display a JPanel that has 5 other JPanels in it. I dont have any syntax errors and all that displays is a very small screen. I have been at this all day and have yet to find a solution.
public class addressPanel extends JPanel {
private JTextField nameT;
private JTextField addressT;
private JTextField cityT;
private JTextField stateT;
private JTextField zipCodeT;
private JTextField phoneNumberT;
private JLabel Title;
private JLabel addressTitle;
private JLabel nameL;
private JLabel addressL;
private JLabel stateL;
private JLabel cityL;
private JLabel zipCodeL;
private JLabel phoneNumberL;
private JLabel orderType;
private JRadioButton takeOut;
private JRadioButton delivery;
private JButton clear;
private JButton submit;
private JPanel addressTextPanel;
private JPanel addressLabelPanel;
private JPanel orderTypePanel;
private JPanel titlePanel;
private JPanel buttonsPanel;
public JPanel addressTextPanel() {
nameT = new JTextField(1);
addressT = new JTextField(2);
cityT = new JTextField(3);
stateT = new JTextField(4);
zipCodeT = new JTextField(5);
phoneNumberT = new JTextField(6);
Font font = new Font(Font.SERIF, Font.PLAIN, 24);
nameT.setFont(font);
addressT.setFont(font);
cityT.setFont(font);
stateT.setFont(font);
zipCodeT.setFont(font);
phoneNumberT.setFont(font);
JPanel addressTextPanel = new JPanel();
addressTextPanel.setPreferredSize(new Dimension(125, 250));
addressTextPanel.setLayout(new BoxLayout(addressTextPanel, BoxLayout.Y_AXIS));
addressTextPanel.add(nameT);
addressTextPanel.add(addressT);
addressTextPanel.add(cityT);
addressTextPanel.add(stateT);
addressTextPanel.add(zipCodeT);
addressTextPanel.add(phoneNumberT);
return addressTextPanel;
}
public JPanel addressLabelPanel() {
nameL = new JLabel("Name:");
addressL = new JLabel("Address:");
cityL = new JLabel("City:");
zipCodeL = new JLabel("Zip Code:");
stateL = new JLabel("State:");
phoneNumberL = new JLabel("Phone Number:");
nameL.setFont(nameL.getFont().deriveFont(24.0f));
addressL.setFont(addressL.getFont().deriveFont(24.0f));
cityL.setFont(cityL.getFont().deriveFont(24.0f));
zipCodeL.setFont(zipCodeL.getFont().deriveFont(24.0f));
stateL.setFont(stateL.getFont().deriveFont(24.0f));
phoneNumberL.setFont(phoneNumberL.getFont().deriveFont(24.0f));
JPanel addressLabelPanel = new JPanel();
addressLabelPanel.setPreferredSize(new Dimension(125, 250));
addressLabelPanel.setLayout(new BoxLayout(addressLabelPanel, BoxLayout.Y_AXIS));
addressLabelPanel.add(nameL);
addressLabelPanel.add(addressL);
addressLabelPanel.add(cityL);
addressLabelPanel.add(stateL);
addressLabelPanel.add(zipCodeL);
addressLabelPanel.add(phoneNumberL);
return addressLabelPanel;
}
public JPanel orderTypePanel() {
orderType = new JLabel("Order Type:");
takeOut = new JRadioButton("Take Out");
delivery = new JRadioButton("Delivery");
orderType.setFont(takeOut.getFont().deriveFont(24.0f));
takeOut.setFont(takeOut.getFont().deriveFont(24.0f));
delivery.setFont(delivery.getFont().deriveFont(24.0f));
JPanel orderTypePanel = new JPanel();
orderTypePanel.setPreferredSize(new Dimension(250, 125));
orderTypePanel.setLayout(new BoxLayout(orderTypePanel, BoxLayout.Y_AXIS));
orderTypePanel.add(orderType);
orderTypePanel.add(takeOut);
orderTypePanel.add(delivery);
return orderTypePanel;
}
public JPanel titlePanel() {
Title = new JLabel("Pizza Order Form");
addressTitle = new JLabel("Address");
Title.setFont(Title.getFont().deriveFont(36.0f));
addressTitle.setFont(addressTitle.getFont().deriveFont(36.0f));
JPanel titlePanel = new JPanel();
titlePanel.setPreferredSize(new Dimension(500, 100));
titlePanel.setLayout(new BoxLayout(titlePanel, BoxLayout.Y_AXIS));
titlePanel.add(Title);
titlePanel.add(addressTitle);
return titlePanel;
}
public JPanel buttonsPanel() {
clear = new JButton("Clear");
submit = new JButton("Submit");
clear.setFont(clear.getFont().deriveFont(24.0f));
submit.setFont(submit.getFont().deriveFont(24.0f));
JPanel buttonsPanel = new JPanel();
buttonsPanel.setPreferredSize(new Dimension(500, 100));
buttonsPanel.setLayout(new BoxLayout(buttonsPanel, BoxLayout.X_AXIS));
buttonsPanel.add(clear);
buttonsPanel.add(submit);
return buttonsPanel;
}
public addressPanel() {
JPanel addressParent = new JPanel(new BorderLayout());
addressParent.add(new titlePanel(), BorderLayout.NORTH);
addressParent.add(new orderTypePanel(), BorderLayout.WEST);
addressParent.add(new addressLabelPanel(), BorderLayout.CENTER);
addressParent.add(new addressTextPanel(), BorderLayout.EAST);
addressParent.add(new buttonsPanel(), BorderLayout.SOUTH);
}
public static void main(String[] args) {
// Create Main Panel
JFrame frame = new JFrame("");
frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
frame.getContentPane().add(new addressPanel());
// Color background = new Color(238,233,191);
// frame.getContentPane().setBackground(background);
frame.pack();
frame.setVisible(true);
}
}
Read your code. The program creates a JFrame. It creates an instance of addressPanel (which should be named AddressPanel). ANd it adds this addressPanel instance to the frame conent pane.
Now what is added to the addressPanel? Nothing:
public addressPanel()
{
JPanel addressParent = new JPanel(new BorderLayout());
addressParent.add (new titlePanel(), BorderLayout.NORTH);
addressParent.add (new orderTypePanel(), BorderLayout.WEST);
addressParent.add (new addressLabelPanel(), BorderLayout.CENTER);
addressParent.add (new addressTextPanel(), BorderLayout.EAST);
addressParent.add (new buttonsPanel(), BorderLayout.SOUTH);
}
The constructor of addressPanel creates another panel (addressParent), adds plenty of things to this addressParent panel, but doesn't add anything to this, the addressPanel. So the addressPanel is empty.
Please respect the Java naming conventions to make your code readable. Classes start with an uppercase letter.
see my problem start form this piece of code i add all the addActionListener for the button
but when it come to the Radio button it use addItemListenet but i implements ActionListener only how i will implements ItemListener so i can set Law when ever the user Select sw form the radio button and click on add item~ it will add the item to the right array i made before
exitButton.addActionListener(new ButtonWatcher());
addButton.addActionListener(new ButtonWatcher());
copyButton.addActionListener(new ButtonWatcher());
showButton.addActionListener(new ButtonWatcher());
rButton.addItemListenet(new ButtonWatcher());
}
private class ButtonWatcher implements ActionListener{
public void actionPerformed(ActionEvent a){
Object buttonPressed=a.getSource();
if(buttonPressed.equals(exitButton))
{
System.exit(0);
}
if(buttonPressed.equals(addButton) && rButton1.isSelected())
{
//do the action
}
full code
package item;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
*
* #author isslam
*/
public class MyFrameMain extends JFrame{
Equipment newq = new Equipment();
private final JLabel iLabel;
private final JLabel nLabel;
private final JTextField iJTextField;
private final JTextField nJTextField;
private final JTextField swTextField;
private final JTextField hwTextField;
private final JLabel jItemCounter;
private final JTextArea reSoulte;
private final JButton addButton;
private final JButton showButton;
private final JButton copyButton;
private final JButton exitButton;
public MyFrameMain(String title){
setSize(500, 500);
setTitle(title);
setDefaultCloseOperation(MyFrameMain.EXIT_ON_CLOSE);
iJTextField = new JTextField();
nJTextField = new JTextField();
swTextField = new JTextField();
hwTextField = new JTextField();
nLabel = new JLabel("ID: ");
iLabel = new JLabel("Name: ");
jItemCounter = new JLabel("Number of current Item");
reSoulte = new JTextArea(15,20);
reSoulte.setEditable(false);
reSoulte.setText("Array is empty");
addButton = new JButton("Add an item into the Array");
showButton = new JButton("Show all items in the Array");
copyButton = new JButton("Copy Array into File");
exitButton = new JButton("Exite");
JRadioButton rButton1 = new JRadioButton("SW Version",false);
JRadioButton rButton2 = new JRadioButton("HW Type",false);
JRadioButton rButton3 = new JRadioButton("General",true);
ButtonGroup BGroup = new ButtonGroup();
BGroup.add(rButton1);
BGroup.add(rButton2);
BGroup.add(rButton3);
JPanel rbPanel = new JPanel(new GridLayout(5,1));
rbPanel.add(nLabel);
rbPanel.add(iLabel);
rbPanel.add(rButton1);
rbPanel.add(rButton2);
rbPanel.add(rButton3);
JPanel bpanel = new JPanel(new GridLayout(2,2));
bpanel.add(addButton);
bpanel.add(showButton);
bpanel.add(copyButton);
bpanel.add(exitButton);
JPanel jtfPanel = new JPanel(new GridLayout(5,1));
jtfPanel.add(iJTextField);
jtfPanel.add(nJTextField);
jtfPanel.add(swTextField);
jtfPanel.add(hwTextField);
jtfPanel.add(jItemCounter);
JPanel topPanel = new JPanel(new BorderLayout());
topPanel.add(rbPanel, BorderLayout.WEST);
topPanel.add(jtfPanel, BorderLayout.CENTER);
JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.add(bpanel, BorderLayout.SOUTH);
mainPanel.add(reSoulte, BorderLayout.CENTER);
mainPanel.add(topPanel, BorderLayout.NORTH);
Container pane = getContentPane();
pane.add(mainPanel);
exitButton.addActionListener(new ButtonWatcher());
addButton.addActionListener(new ButtonWatcher());
copyButton.addActionListener(new ButtonWatcher());
showButton.addActionListener(new ButtonWatcher());
//rButton.addItemListenet(new ButtonWatcher());
}
private class ButtonWatcher implements ActionListener{
public void actionPerformed(ActionEvent a){
Object buttonPressed=a.getSource();
if(buttonPressed.equals(exitButton))
{
System.exit(0);
}
if(buttonPressed.equals(addButton) && rButton1.isSelected())
{
//do the action
}
}
}
}
I'm not sure what array you want to fill but get the text with getText()
if(buttonPressed.equals(addButton) && rButton1.isSelected())
{
String s1 = iJTextField.getText();
String s2 = nJTextField.getText();
String s3 = swTextField.getText();
String s4 = hwTextField.getText();
// something with these strings
}
If any of the inputs are numbers and you want the numerical value, you need to parse.
Also, these need to be declared as class memebers. You have them declared in the constructor
JRadioButton rButton1 = new JRadioButton("SW Version",false);
JRadioButton rButton2 = new JRadioButton("HW Type",false);
JRadioButton rButton3 = new JRadioButton("General",true);
Declared in the constructor, they are not within the scope of the listener class
public class MyFrameMain extends JFrame{
private final JLabel iLabel;
private final JLabel nLabel;
private final JTextField iJTextField;
private final JTextField nJTextField;
private final JTextField swTextField;
private final JTextField hwTextField;
private final JLabel jItemCounter;
private final JTextArea reSoulte;
private final JButton addButton;
private final JButton showButton;
private final JButton copyButton;
private final JButton exitButton;
JRadioButton rButton1 = new JRadioButton("SW Version",false);
JRadioButton rButton2 = new JRadioButton("HW Type",false);
JRadioButton rButton3 = new JRadioButton("General",true);
public MyFrameMain(String title){
Also, doesn't really look like you need a listener for the radio button, since an event is not necessary. The JButton listens for an event, and in the actionPerformed, it checks if the radio button is selected. Therefore no need for the radio button to listen for any event, the JButton does that.
Try following code. I ahve added a List item and adding values from swTextField TextFiled to item when user select rButton1 and click on addButton button
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
/**
*
* #author isslam
*/
public class Test extends JFrame {
private final JLabel iLabel;
private final JLabel nLabel;
private final JTextField iJTextField;
private final JTextField nJTextField;
private final JTextField swTextField;
private final JTextField hwTextField;
private final JLabel jItemCounter;
private final JTextArea reSoulte;
private final JButton addButton;
private final JButton showButton;
private final JButton copyButton;
private final JButton exitButton;
JRadioButton rButton1;
java.util.List<String> item = new ArrayList<String>();
public Test(String title) {
setSize(500, 500);
setTitle(title);
setDefaultCloseOperation(Test.EXIT_ON_CLOSE);
iJTextField = new JTextField();
nJTextField = new JTextField();
swTextField = new JTextField();
hwTextField = new JTextField();
nLabel = new JLabel("ID: ");
iLabel = new JLabel("Name: ");
jItemCounter = new JLabel("Number of current Item");
reSoulte = new JTextArea(15, 20);
reSoulte.setEditable(false);
reSoulte.setText("Array is empty");
addButton = new JButton("Add an item into the Array");
showButton = new JButton("Show all items in the Array");
copyButton = new JButton("Copy Array into File");
exitButton = new JButton("Exite");
rButton1 = new JRadioButton("SW Version", false);
JRadioButton rButton2 = new JRadioButton("HW Type", false);
JRadioButton rButton3 = new JRadioButton("General", true);
ButtonGroup BGroup = new ButtonGroup();
BGroup.add(rButton1);
BGroup.add(rButton2);
BGroup.add(rButton3);
JPanel rbPanel = new JPanel(new GridLayout(5, 1));
rbPanel.add(nLabel);
rbPanel.add(iLabel);
rbPanel.add(rButton1);
rbPanel.add(rButton2);
rbPanel.add(rButton3);
JPanel bpanel = new JPanel(new GridLayout(2, 2));
bpanel.add(addButton);
bpanel.add(showButton);
bpanel.add(copyButton);
bpanel.add(exitButton);
JPanel jtfPanel = new JPanel(new GridLayout(5, 1));
jtfPanel.add(iJTextField);
jtfPanel.add(nJTextField);
jtfPanel.add(swTextField);
jtfPanel.add(hwTextField);
jtfPanel.add(jItemCounter);
JPanel topPanel = new JPanel(new BorderLayout());
topPanel.add(rbPanel, BorderLayout.WEST);
topPanel.add(jtfPanel, BorderLayout.CENTER);
JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.add(bpanel, BorderLayout.SOUTH);
mainPanel.add(reSoulte, BorderLayout.CENTER);
mainPanel.add(topPanel, BorderLayout.NORTH);
Container pane = getContentPane();
pane.add(mainPanel);
exitButton.addActionListener(new ButtonWatcher());
addButton.addActionListener(new ButtonWatcher());
copyButton.addActionListener(new ButtonWatcher());
showButton.addActionListener(new ButtonWatcher());
//rButton.addItemListenet(new ButtonWatcher());
}
private class ButtonWatcher implements ActionListener {
public void actionPerformed(ActionEvent a) {
Object buttonPressed = a.getSource();
if (buttonPressed.equals(exitButton)) {
System.exit(0);
}
if (buttonPressed.equals(addButton) && rButton1.isSelected()) {
item.add(swTextField.getText());
System.out.println(item);
}
}
}
public static void main(String args[]) {
Test t = new Test("Test");
t.setVisible(true);
}
}
Use ButtonGroup with the JRadioButton of your context.
Use jRadioButton.setActionCommand(String) to set their corresponding action name: for your context "SW Version" and anything such.
Make use of an ArrayList to add the item of your context. Try mapping each such array list using a HashMap<Key, Val> i.e., HashMap<String, ArrayList<Equipment>> where the "SW Version" or anything such name will be the key
Try adding listeners to each action button in-line using the means of anonymous class.
So a sample coding for add action would become depicting the usage(usefulness) of ButtonGroup:
addButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
String actionCommand = buttonGroup1.getSelection()
.getActionCommand();
// actionCommand = "SW Version"
map.get(actionCmmand).add(equipment);
}
});
Tutorial and reference:
How to use Radio Button, check the demo for ButtonGroup
ButtonGroup class
HashMap
I'm trying to update a JTable that pulls in data from an ArrayList. I have two frames in my program. The first frame is a JTable (AbstractTableModel) that displays the contents of the ArrayList. I click the "New" button on that frame to bring up the second window, which lets me add to the aforementioned ArrayList. When I click my "Save" button, the second window closes and the first is supposed to refresh with the new row. I don't have any syntactical errors in my code, and it looks conceptually right. I think the first place to start troubleshooting would be in the NoteCntl class. I'm under the impression that getNoteTableUI() should update the view with the new data when it's called, but I'm stumped as to what's going on. I'm new to the concept of Model View Controller, but I'd like to follow that as closely as possible.
Here is the Controller class:
public class NoteCntl {
private NoteTableModel theNoteTableModel = new NoteTableModel();;
private NoteTableUI theNoteTableUI;
private NoteDetailUI theNoteDetailUI;
public NoteCntl(){
theNoteTableUI = new NoteTableUI(this);
}
public NoteTableModel getNoteTableModel(){
return theNoteTableModel;
}
public void getNoteDetailUI(Note theNote){
if (theNoteDetailUI == null || theNote == null){
theNoteDetailUI = new NoteDetailUI(this,theNote);
}
else{
theNoteDetailUI.setVisible(true);
}
}
public NoteTableUI getNoteTableUI(){
theNoteTableModel.fireTableDataChanged(); //why doesn't this do anything?
theNoteTableUI.setVisible(true);
return theNoteTableUI;
}
public void deleteNote(int noteToDelete){
theNoteTableModel.removeRow(noteToDelete);
}
}
The First UI (Table):
public class NoteTableUI extends JFrame{
NoteTableModel noteModel;
NoteCntl theNoteCntl;
JPanel buttonPanel;
JPanel tablePanel;
JTable theNoteTable;
JScrollPane theScrollPane;
JButton backButton;
JButton deleteButton;
JButton editButton;
JButton newButton;
public NoteTableUI(NoteCntl theParentNoteCntl){
theNoteCntl = theParentNoteCntl;
this.initComponents();
this.setSize(400, 500);
this.setLocationRelativeTo(null);
this.setTitle("NoteTableUI");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void initComponents(){
buttonPanel = new JPanel();
tablePanel = new JPanel();
backButton = new JButton("Back");
newButton = new JButton("New");
newButton.addActionListener(new newButtonListener());
editButton = new JButton("Edit");
deleteButton = new JButton("Delete");
deleteButton.addActionListener(new deleteButtonListener());
noteModel = theNoteCntl.getNoteTableModel();
theNoteTable = new JTable(theNoteCntl.getNoteTableModel());
theScrollPane = new JScrollPane(theNoteTable);
theNoteTable.setFillsViewportHeight(true);
tablePanel.add(theScrollPane);
buttonPanel.add(backButton);
buttonPanel.add(deleteButton);
buttonPanel.add(editButton);
buttonPanel.add(newButton);
this.getContentPane().add(buttonPanel, BorderLayout.NORTH);
this.getContentPane().add(tablePanel, BorderLayout.CENTER);
}
public class deleteButtonListener implements ActionListener{
public void actionPerformed(ActionEvent event){
int selectedRow = theNoteTable.getSelectedRow();
if (selectedRow == -1){
System.out.println("No row selected");
}
else{
noteModel.removeRow(selectedRow);
}
revalidate();
repaint();
}
}
public class newButtonListener implements ActionListener{
public void actionPerformed(ActionEvent event){
NoteTableUI.this.setVisible(false);
NoteTableUI.this.theNoteCntl.getNoteDetailUI(null);
/*
NoteDetailCntl theNoteDetailCntl = new NoteDetailCntl();
lastRow++;
long newRow = lastRow;
noteModel.addRow(newRow, 0, "", "");
revalidate();
repaint();
*/
}
}
The 2nd UI (Detail editor)
public class NoteDetailUI extends JFrame{
private final int FRAME_WIDTH = 700;
private final int FRAME_HEIGHT = 500;
private final int FIELD_WIDTH = 10;
JButton saveButton;
JButton backButton;
JTextField idField;
JTextField dateField;
JTextField nameField;
JTextField descriptionField;
JTextArea noteDetail;
JLabel idLabel;
JLabel dateLabel;
JLabel nameLabel;
JLabel descriptionLabel;
JPanel buttonPanel;
JPanel textFieldPanel;
JPanel textAreaPanel;
JPanel mainPanel;
NoteTableModel theNoteTableModel;
NoteDetailCntl theNoteDetailCntl;
NoteCntl theNoteCntl;
Note theCurrentNote;
public NoteDetailUI(){
this.initComponents();
this.setSize(FRAME_WIDTH,FRAME_HEIGHT);
this.setLocationRelativeTo(null);
this.setTitle("NoteDetailUI");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public NoteDetailUI(NoteCntl parentNoteCntl, Note theSelectedNote){
theNoteCntl = parentNoteCntl;
theCurrentNote = theSelectedNote;
this.initComponents();
this.setSize(400,500);
this.setLocationRelativeTo(null);
this.setTitle("Note");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void initComponents(){
saveButton = new JButton("Save");
saveButton.addActionListener(new saveButtonListener());
backButton = new JButton("Back");
backButton.addActionListener(new backButtonListener());
idField = new JTextField(FIELD_WIDTH);
theNoteTableModel = new NoteTableModel();
idField.setText("10");
idField.setEditable(false);
dateField = new JTextField(FIELD_WIDTH);
dateField.setText("20131108");
nameField = new JTextField(FIELD_WIDTH);
nameField.setText("Untitled");
descriptionField = new JTextField(FIELD_WIDTH);
descriptionField.setText("not described");
idLabel = new JLabel("ID");
dateLabel = new JLabel("Date");
nameLabel = new JLabel("Name");
descriptionLabel = new JLabel("Description");
noteDetail = new JTextArea(25,60);
buttonPanel = new JPanel();
textFieldPanel = new JPanel();
textAreaPanel = new JPanel();
mainPanel = new JPanel(new BorderLayout());
buttonPanel.add(backButton);
buttonPanel.add(saveButton);
textFieldPanel.add(idLabel);
textFieldPanel.add(idField);
textFieldPanel.add(dateLabel);
textFieldPanel.add(dateField);
textFieldPanel.add(nameLabel);
textFieldPanel.add(nameField);
textFieldPanel.add(descriptionLabel);
textFieldPanel.add(descriptionField);
textAreaPanel.add(noteDetail);
mainPanel.add(buttonPanel, BorderLayout.SOUTH);
mainPanel.add(textFieldPanel, BorderLayout.NORTH);
mainPanel.add(textAreaPanel, BorderLayout.CENTER);
add(mainPanel);
}
public ArrayList<String> getNoteDetails(){
ArrayList<String> newData = new ArrayList<String>();
newData.add(idField.getText());
newData.add(dateField.getText());
newData.add(nameField.getText());
newData.add(descriptionField.getText());
return newData;
}
public class saveButtonListener implements ActionListener{
public void actionPerformed(ActionEvent event){
/*
* Access the noteTableData array in NoteTableModel
* Add the newData fields in order
*/
if(theCurrentNote == null){
int newNoteNumber = Integer.parseInt(NoteDetailUI.this.idField.getText());
int newNoteDate = Integer.parseInt(NoteDetailUI.this.dateField.getText());
String newNoteName = NoteDetailUI.this.nameField.getText();
String newNoteDescription = NoteDetailUI.this.descriptionField.getText();
NoteDetailUI.this.theCurrentNote = new EssayNote(newNoteNumber,newNoteDate,newNoteName,newNoteDescription);
NoteDetailUI.this.setVisible(false);
NoteDetailUI.this.dispose();
NoteDetailUI.this.theNoteCntl.getNoteTableUI();
}
else{
//if it's a current Note
}
//Refresh the JTable
}
}
public class backButtonListener implements ActionListener{
public void actionPerformed(ActionEvent event){
}
}
Thanks for all the help. I can provide the other classes if you want to just run the program and see what's happening, but I suspect it's either a problem with the fireTableDataChanged() call in the controller class or a problem with updating the contents of the ArrayList in the SaveButtonListener.