I'm trying to create a complete menubar using multidimensional ararys.
So far I have this code:
private JMenuBar menuBar = new JMenuBar();
private JMenuItem[][] menuItem = new JMenuItem[5][5];
private String[] menuBarItemNames = {"File", "Edit", "Database", "View", "Help"};
private String[] menuBarFileItemNames = {"Save", "Refresh", "Next", "Previous","Exit"};
view() {
setJMenuBar(menuBar);
for(int u = 0; u < menuItem.length; u++){
menuItem[u][0] = new JMenu(menuBarItemNames[u]);
for(int t = 0; t < menuBarFileItemNames.length; t++){
//Code to add 'File' child items to the 'File' MenuBarItem
}
menuBar.add(menuItem[u][0]);
}
I'm struggling to figure out how to add the menuBarFileItems to the File menu.
I have this code to add to the second for loop:
menuItem[0][t] = new JMenuItem(menuBarFileItemNames[t]);
but it just causes the first item on the menuBar to be replaced by 'Save'.
Any ideas?
Also, is it not possible to have private JMenuItem[][] menuItem = new JMenuItem[5][]; so that I don't have to set the size of each menuBar item, e.g. File = 5 items, Edit = 5 items etc
Thanks
Try it:
...
menuItem[u][0] = new JMenu(menuBarItemNames[u]);
for(int t = 0; t < menuBarFileItemNames.length; t++){
menuItem[0][t].add(new JMenuItem(menuBarFileItemNames[t]));
}
...
Att: This is only for "FILE" menu item (on zero position).
I think what you want is this:
private void view()
{
setJMenuBar(menuBar);
for (int u = 0; u < menuItem.length; u++)
{
menuItem[u][0] = new JMenu(menuBarItemNames[u]);
if (u == 0)
{
for (int t = 1; t <= menuBarFileItemNames.length; t++)
{
// Code to add 'File' child items to the 'File' MenuBarItem
menuItem[0][t] = new JMenuItem(menuBarFileItemNames[t-1]);
menuItem[0][0].add(menuItem[0][t]);
}
}
menuBar.add(menuItem[u][0]);
}
}
If you're replacing the first item on the menu bar, then you're adding to the menu bar, not the menu item.
And no, Java needs to know the sizes of both dimensions of the array you're declaring, you can't leave one of them open.
Related
I am using an HorizontalBarChart I want to draw a Label (the name of the vendor) inside or in the middle of the Bar, in the BarDaset there is something called setLabel but it's not working.
Here is my code:
private BarDataSet createLineChart(String storeName, List<String> listofcompanies){
ArrayList<BarEntry> entries= new ArrayList<BarEntry>();
for (int j = 0; j < listofcompanies.size(); j++) {
entries.add(new BarEntry(Float.parseFloat(listofcompanies.get(j)),j));
}
Random rd = new Random();
setComp1 = new BarDataSet(entries,storeName);
setComp1.setColor(Color.argb(255,rd.nextInt(256),rd.nextInt(256),rd.nextInt(256)));
setComp1.setDrawValues(true);
setComp1.setLabel(storeName);
setComp1.setHighlightEnabled(true);
setComp1.setDrawValues(true);
// LineData data =new LineData(labels,dataset);
return setComp1;
}
Try to use:
chart.getXAxis().setPosition(XAxisPosition.BOTTOM_INSIDE);
Or
chart.getXAxis().setPosition(XAxisPosition.TOP_INSIDE);
This will solve your problem.
I have a questions about using array
I have a method that creates JButton for a JPanel. I used JButton[] to store 7 buttons in it.
The problem is those buttons must be setEnable(false) initially. they are only enabled when openButton is clicked.
I would like to create another method that just take all elements from JButton[] and set them Enable. How can I do that?
private JButton filterButtons() {
//set layout for buttons
setLayout(new BorderLayout());
//Array to store description of buttons
final String[] filterNames = {myEdgeDetect.getDescription(),
myEdgeHighlight.getDescription(),
myFlipHorizontal.getDescription(),
myFlipVeritcal.getDescription(),
myGrayscale.getDescription(),
mySharpen.getDescription(),
mySoften.getDescription()};
final JButton[] filterButtons = new JButton[filterNames.length];
//This panel store buttons on north
final JPanel buttonPanel = new JPanel();
//Start to print buttons
for (int i = 0; i < filterNames.length; i++) {
filterButtons[i] = new JButton(filterNames[i]);
filterButtons[i].setEnabled(false);
filterButtons[i].addActionListener(null);
filterButtons[i].setActionCommand(" " + i);
buttonPanel.add(filterButtons[i]);
}
If, rather than defining filterNames and filterButtons inside the method you made them fields of the class, then it would be easy to write a second method that iterates through the filterButtons and enables them.
Class Whatever {
String[] filterNames;
JButton[] filterButtons;
private JButton filterButtons() {
//set layout for buttons
setLayout(new BorderLayout());
//Array to store description of buttons
filterNames = {myEdgeDetect.getDescription(),
myEdgeHighlight.getDescription(),
myFlipHorizontal.getDescription(),
myFlipVeritcal.getDescription(),
myGrayscale.getDescription(),
mySharpen.getDescription(),
mySoften.getDescription()};
filterButtons = new JButton[filterNames.length];
//This panel store buttons on north
final JPanel buttonPanel = new JPanel();
//Start to print buttons
for (int i = 0; i < filterNames.length; i++) {
filterButtons[i] = new JButton(filterNames[i]);
filterButtons[i].setEnabled(false);
filterButtons[i].addActionListener(null);
filterButtons[i].setActionCommand(" " + i);
buttonPanel.add(filterButtons[i]);
}
}
void enableButtons() {
for (int i = 0; i < filterButtons.length; i++) {
filterButtons[i].setEnabled(true);
}
}
}
I have a little Problem here. I'm currently working at a GUI to organise my series, that im watching/have watched. In this part I wanted a frame to open with all the different genres in JCheckBox'es, that should return a boolean-array, where each index represents the genre of this index and the value if it JCheckBox was clicked.
I decied to do this woth a JOptionPane, because the JOptioPane.showInputPane()-funktion offers to give an Object-array as parameter, that will be displayed in the JOptionPane. To this point, my code is working perfectly:
String[] genres = IO.getGenres();
JCheckBox[] check = new JCheckBox[genres.length];
for(int i = 0; i < genres.length; i++)
check[i] = new JCheckBox(genres[i]);
boolean[] ret = new boolean[genres.length];
int answer = JOptionPane.showConfirmDialog(null, new Object[]{"Choose gernes:", check}, "Genres" , JOptionPane.OK_CANCEL_OPTION);
if(answer == JOptionPane.OK_OPTION)
{
for(int i = 0; ; i++)
ret[i] = check[i].isSelected();
}else if(answer == JOptionPane.CANCEL_OPTION || answer == JOptionPane.ERROR_MESSAGE)
{
for(int i = 0; i < genres.length; i++)
ret[i] = false;
}
return ret;
The problem is that the JOptionPane grows to large, with to many genres. So how can I make a scrollable list of JCheckBox'es, that is displayed in the JOptionPane? Or is there a better way to work around?
You can simply layout your check boxes in a panel and transfer a wrapping scroll pane to your JOptionPane. Something like this:
JPanel layoutPanel = new JPanel(new GridLayout(genres.length, 1));
for (JCheckBox c : check) {
layoutPanel.add(c);
}
JScrollPane scroller = new JScrollPane(layoutPanel);
scroller.setPreferredSize(new Dimension(300, 500));
int answer = JOptionPane.showConfirmDialog(null, scroller, "Genres" , JOptionPane.OK_CANCEL_OPTION);
The code below is supposed to create and object instance for a specific type (say color) JButton I want to represent in a grid. When I iterate through the for-loop to add the buttons to the jframe it adds nothing. But if I add a single instance variable it will add that. Anybody have an idea?
public class Grid {
protected JButton [][] board;
private JButton player;
private JButton openCell;
private JButton wall;
private JButton closedCell;
public Grid(String [] args) { // args unused
// Instantiation
board = new JButton [6][6];
layout = new String [6][6];
blueCell = new JButton("BLUE CELL");
redCell = new JButton("RED CELL");
greenCell = new JButton("GREEN CELL");
whiteCell = new JButton("WHITE CELL");
// Configuration (add actions later)
// Decoration
blueCell.setBackground(Color.blue);
redCell.setBackground(Color.red);
greenCell.setBackground(Color.green);
whiteCell.setBackground(Color.white);
for (int rows = 0; rows < 6; rows++) {
for (int cols = 0; cols < 6; cols++) {
if ((layout[rows][cols]).equals('*')) {
board[rows][cols] = blueCell;
}
else if ((layout[rows][cols]).equals('.')) {
board[rows][cols] = redCell;
}
else if ((layout[rows][cols]).equals('x')) {
board[rows][cols] = greenCell;
}
else {
board[rows][cols] = whiteCell;
}
}
}
JFrame game = new JFrame();
game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
game.setLayout(new GridLayout (6, 6));
game.setSize(500, 500);
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board.length; j++) {
if ((board[i][j]).equals(blueCell)) {
grid.add(blueCell);
}
else if ((board[i][j]).equals(redCell)) {
grid.add(redCell);
}
else if ((board[i][j]).equals(greenCell)) {
grid.add(greenCell);
}
else {
grid.add(whiteCell);
}
}
}
grid.setVisible(true);
} // end of constructor
} // end of Grid class
You can add a component to your GUI only once. If you add it to another component, it will be removed from the previous component. You're trying to add the same JButtons several times, and that won't work. Instead you're going to have to create more JButtons. Consider having your buttons share Actions which is allowed.
If you need more help, consider posting compilable code (your current code is not), a small runnable, compilable program that demonstrates your problem, in other words, an sscce.
Edit
You comment:
But don't these count as instances of a JButton not the same JButton? (I don't understand what your answer meant...)
Think of it mathematically... how many JButtons do you create in your code above? Well, this is easy to figure, exactly 4:
blueCell = new JButton("BLUE CELL");
redCell = new JButton("RED CELL");
greenCell = new JButton("GREEN CELL");
whiteCell = new JButton("WHITE CELL");
So, now ask yourself, how many JButtons are you trying to display in your GUI with these four JButtons? If it's four, then you're possibly OK (as long as you use each button), but if it's more, then you're in trouble. From your 6x6 grid, board = new JButton [6][6];, it looks like you're trying to display 36 JButtons, and if this is true, you've got problems.
But again, if still stuck, please consider creating and posting an sscce.
I am trying to create dynamic JButton and JLabel based on the number of URLs defined in the properties file.
What I have tried so far is:
AppResources.properties file
urls=http://google.com,http://stackoverflow.com,http://gmail.com
urlLabels=Internet Users,MPLS Users,Physical Access (Not Advised)
In my Java program I am reading the properties file and based on the comma separator splitting the string and now i need to generate the buttons and labels accordingly. Like the first URL Label --> first URL as Button and so on.
The way I have done this so far is:
String url = properties.getProperty("urls");
String urlLabel = properties.getProperty("urlLabels");
String[] jButton = url.split(",");
String[] jLabel = urlLabel.split(",");
for (int i = 0; i < jLabel.length; i++) {
JLabel labels = new JLabel(jLabel[i]);
panel.add(labels);
for (int j = 0; j < jButton.length; j++) {
JButton button = new JButton(jButton[j]);
panel.add(button);
}
}
But it prints the button three times for a label. How to fix this? Also how to write the action listeners for these buttons?
If you want to implement action listeners for your buttons, you can simply create and add a new ActionListener while creating the button.
Example :
for (int i = 0; i < jLabel.length; i++) {
final String str = jLabel[i];
JLabel labels = new JLabel(str);
panel.add(labels);
JButton button = new JButton(jButton[i]);
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(panel, str);
}
});
panel.add(button);
}
Remove the inner loop (j based).